| Copyright | Thodoris Papakonstantinou 2017-2026 |
|---|---|
| License | LGPL-3 |
| Maintainer | dev@tpapak.com |
| Stability | experimental |
| Portability | POSIX |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Data.Graph.AdjacencyList.PushRelabel.Internal
Description
Internal definitions for the Tide push-pull-relabel max-flow algorithm.
This module defines:
ResidualGraph— the mutable state threaded through each tide iteration, containing vertex heights, excesses, edge flows, and the set of overflowing vertices grouped by level.ResidualVertexandResidualEdge— per-vertex and per-edge state.NeighborsMap— anIntMap-based adjacency structure that maps each vertex to its forward and reverse neighbors with O(log V) edge-index lookup (replacing the original O(log E)Map Edge Intlookup).- Primitive operations:
push,pull,updateHeight,updateExcess,updateEdge,residualDistances.
The topologyChanged flag tracks whether any edge crossed a saturation
boundary (became saturated or unsaturated) during push/pull. When the
flag is False, the next tide can skip globalRelabel — an optimization
that yields 1.25--1.61x speedup in practice.
Synopsis
- data Network = Network {
- graph :: !Graph
- source :: Vertex
- sink :: Vertex
- capacities :: Capacities
- flow :: Capacities
- type Capacity = Rational
- type Capacities = Map Edge Capacity
- type Flow = Capacity
- data ResidualGraph = ResidualGraph {
- network :: !Network
- netVertices :: !ResidualVertices
- netEdges :: !ResidualEdges
- netNeighborsMap :: !NeighborsMap
- overflowing :: !Overflowing
- steps :: !Int
- topologyChanged :: !Bool
- data ResidualVertex = ResidualVertex !Vertex !Level !Height !Excess
- type ResidualVertices = IntMap ResidualVertex
- data ResidualEdge = ResidualEdge Edge Capacity Flow
- type ResidualEdges = IntMap ResidualEdge
- type NeighborsMap = IntMap (IntMap Int, IntMap Int)
- type Overflowing = IntMap IntSet
- type Height = Int
- type Excess = Capacity
- type Level = Int
- initializeResidualGraph :: Network -> ResidualGraph
- level :: ResidualGraph -> Vertex -> Level
- excess :: ResidualGraph -> Vertex -> Excess
- height :: ResidualGraph -> Vertex -> Height
- edgeCapacity :: ResidualGraph -> Edge -> Capacity
- edgeFlow :: ResidualGraph -> Edge -> Flow
- resEdgeIndex :: NeighborsMap -> Edge -> Maybe Int
- netFlow :: ResidualGraph -> Flow
- inflow :: ResidualGraph -> Vertex -> Flow
- outflow :: ResidualGraph -> Vertex -> Flow
- sourceEdgesCapacity :: Network -> Capacity
- push :: ResidualGraph -> Edge -> Maybe ResidualGraph
- pull :: ResidualGraph -> Edge -> Maybe ResidualGraph
- updateHeight :: ResidualGraph -> Vertex -> Height -> ResidualGraph
- updateExcess :: ResidualGraph -> Vertex -> Excess -> ResidualGraph
- updateEdge :: ResidualGraph -> Edge -> Flow -> ResidualGraph
- getOverflowing :: IntMap ResidualVertex -> IntSet
- networkFromResidual :: ResidualGraph -> Network
- residualDistances :: ResidualGraph -> (IntMap Int, IntMap Int)
- stCut :: ResidualGraph -> ([Vertex], [Vertex])
Re-exports from Network
A flow network: a directed graph with a source, sink, edge capacities, and edge flows.
Construct a Network with zero initial flows and pass it to
pushRelabel to compute the
maximum flow.
Constructors
| Network | |
Fields
| |
type Capacity = Rational Source #
Edge capacity. Uses Rational for exact arithmetic, ensuring the
Tide algorithm terminates correctly for arbitrary capacity values.
Residual graph types
data ResidualGraph Source #
The residual graph: the complete mutable state of the Tide algorithm.
Threaded through each tide iteration. Contains the underlying network, per-vertex and per-edge state, the neighbor map for O(log V) edge lookup, overflowing vertex sets, step counter, and the topology-change flag.
Constructors
| ResidualGraph | |
Fields
| |
Instances
| Show ResidualGraph Source # | |
Defined in Data.Graph.AdjacencyList.PushRelabel.Internal Methods showsPrec :: Int -> ResidualGraph -> ShowS # show :: ResidualGraph -> String # showList :: [ResidualGraph] -> ShowS # | |
| Eq ResidualGraph Source # | |
Defined in Data.Graph.AdjacencyList.PushRelabel.Internal Methods (==) :: ResidualGraph -> ResidualGraph -> Bool # (/=) :: ResidualGraph -> ResidualGraph -> Bool # | |
data ResidualVertex Source #
Per-vertex state in the residual graph.
ResidualVertex v l h x stores:
v— vertex identifierl— level (BFS distance from source in original graph, constant)h— height (updated byglobalRelabeleach tide)x— excess flow (updated by push/pull operations)
Constructors
| ResidualVertex !Vertex !Level !Height !Excess |
Instances
| Show ResidualVertex Source # | |
Defined in Data.Graph.AdjacencyList.PushRelabel.Internal Methods showsPrec :: Int -> ResidualVertex -> ShowS # show :: ResidualVertex -> String # showList :: [ResidualVertex] -> ShowS # | |
| Eq ResidualVertex Source # | |
Defined in Data.Graph.AdjacencyList.PushRelabel.Internal Methods (==) :: ResidualVertex -> ResidualVertex -> Bool # (/=) :: ResidualVertex -> ResidualVertex -> Bool # | |
type ResidualVertices = IntMap ResidualVertex Source #
Map from vertex id to its ResidualVertex state.
data ResidualEdge Source #
Per-edge state: original edge, capacity, and current flow (preflow).
ResidualEdge e c f: edge e with capacity c and flow f.
A forward residual edge exists when f < c; a backward residual edge
exists when f > 0.
Constructors
| ResidualEdge Edge Capacity Flow |
Instances
| Show ResidualEdge Source # | |
Defined in Data.Graph.AdjacencyList.PushRelabel.Internal Methods showsPrec :: Int -> ResidualEdge -> ShowS # show :: ResidualEdge -> String # showList :: [ResidualEdge] -> ShowS # | |
| Eq ResidualEdge Source # | |
type ResidualEdges = IntMap ResidualEdge Source #
Map from edge index to its ResidualEdge state.
type NeighborsMap = IntMap (IntMap Int, IntMap Int) Source #
For each vertex, maps forward neighbors and reverse neighbors
to their edge indices in the graph's EdgeMap.
NeighborsMap ! v = (fwdMap, revMap) where:
fwdMap ! w= index of edge(v, w)(forward neighbor)revMap ! u= index of edge(u, v)(reverse neighbor)
This provides O(log degree) edge-index lookup, replacing the original
O(log E) lookup via Map Edge Int.
type Overflowing = IntMap IntSet Source #
Overflowing vertices grouped by level. Keys are levels (BFS distance from source); values are sets of vertices at that level with positive excess.
This structure determines the iteration order for globalPush (ascending level = left fold) and globalPull (descending level = right fold).
Vertex property types
Vertex height in the push-relabel framework.
For source-side vertices: height = |V| + distance_from_source.
For sink-side vertices: height = distance_from_sink.
type Excess = Capacity Source #
Vertex excess: inflow - outflow. Positive excess means the vertex
is overflowing and needs to push or pull flow.
Level: the shortest-path distance from the source in the original (not residual) graph. Constant throughout the algorithm. Determines the ordering of vertices in globalPush (left fold, ascending) and globalPull (right fold, descending).
Initialization
initializeResidualGraph :: Network -> ResidualGraph Source #
Build the initial ResidualGraph from a Network.
Saturates all edges leaving the source (setting their flow equal to
capacity), sets the source height to |V|, and initializes the
overflowing set with all vertices that received flow from the source.
The topologyChanged flag is set to True so the first tide always
runs globalRelabel.
Vertex property accessors
level :: ResidualGraph -> Vertex -> Level Source #
Level of a vertex (shortest distance from source in original graph).
Edge property accessors
edgeCapacity :: ResidualGraph -> Edge -> Capacity Source #
Capacity of an edge.
resEdgeIndex :: NeighborsMap -> Edge -> Maybe Int Source #
O(log degree) edge index lookup via NeighborsMap.
Looks up the edge index of (u, v) by finding v in the forward
neighbor map of u. Returns Nothing if the edge does not exist.
Flow queries
netFlow :: ResidualGraph -> Flow Source #
Net flow into the sink. This is the current flow value of the network. At termination, this equals the maximum flow.
inflow :: ResidualGraph -> Vertex -> Flow Source #
Total flow into a vertex (sum of flows on incoming edges).
outflow :: ResidualGraph -> Vertex -> Flow Source #
Total flow out of a vertex (sum of flows on outgoing edges).
sourceEdgesCapacity :: Network -> Capacity Source #
Total capacity of all edges leaving the source. This is an upper bound on the maximum flow.
Push and pull operations
push :: ResidualGraph -> Edge -> Maybe ResidualGraph Source #
Push flow along a forward edge (u, v).
Preconditions (checked, returns Nothing if not met):
height(u) = height(v) + 1(flow goes downhill)- Residual capacity
c - f > 0(edge is not saturated) excess(u) > 0(source vertex has excess to push)
Pushes min(excess(u), c - f) units of flow.
Updates the topologyChanged flag if the edge becomes saturated.
pull :: ResidualGraph -> Edge -> Maybe ResidualGraph Source #
Pull flow along a reverse edge (u, v).
This is the dual of push: it decreases flow on edge (u, v) by moving
excess from v back to u.
Preconditions (checked, returns Nothing if not met):
height(v) = height(u) + 1(pull goes uphill in the forward direction)flow(u, v) > 0(there is flow to pull back)excess(v) > 0(pulling vertex has excess)
Pulls min(excess(v), flow) units.
Updates the topologyChanged flag if the edge becomes zero-flow.
State updates
updateHeight :: ResidualGraph -> Vertex -> Height -> ResidualGraph Source #
Update the height of a vertex. Source and sink heights are never modified.
updateExcess :: ResidualGraph -> Vertex -> Excess -> ResidualGraph Source #
Update the excess of a vertex and maintain the overflowing index.
When excess transitions between zero and non-zero, the vertex is
added to or removed from the Overflowing map at its level.
Source and sink are excluded from the overflowing set.
updateEdge :: ResidualGraph -> Edge -> Flow -> ResidualGraph Source #
Update the flow on an edge and track topology changes.
A topology change occurs when a forward residual edge appears or
disappears (flow crosses the capacity boundary) or a backward residual
edge appears or disappears (flow crosses zero).
The topologyChanged flag is set to True (OR-ed) if such a change occurs.
Overflowing vertex tracking
getOverflowing :: IntMap ResidualVertex -> IntSet Source #
Collect all vertices with positive excess.
Network reconstruction
networkFromResidual :: ResidualGraph -> Network Source #
Reconstruct the Network with final edge flows from the residual graph.
Called when the algorithm terminates.
Residual BFS (for globalRelabel)
residualDistances :: ResidualGraph -> (IntMap Int, IntMap Int) Source #
Compute distances from source and sink in the residual graph via BFS.
Returns (sourceDists, sinkDists) where:
sourceDists:IntMapfrom vertex to BFS distance from source (traversing edges with residual capacity > 0 in reverse, and edges with flow > 0 forward)sinkDists:IntMapfrom vertex to BFS distance from sink (traversing edges with residual capacity > 0 forward, and edges with flow > 0 in reverse)
Used by globalRelabel to set vertex heights:
source-side vertices get height = |V| + dist_from_source,
sink-side vertices get height = dist_from_sink.
Min-cut
stCut :: ResidualGraph -> ([Vertex], [Vertex]) Source #
Compute the source-sink minimum cut from the residual graph.
Returns (S, T) where S is the set of vertices reachable from the
source in the residual graph (excluding source and sink) and T is
the complement. By the max-flow min-cut theorem, the total capacity
of edges crossing from S to T equals the maximum flow.