Network Flow Problems
Master the theory and algorithms for solving network flow problems, a powerful class of graph algorithms with applications in transportation, resource allocation, bipartite matching, and more.
Module Contents
Introduction to Network Flow
Network flow problems involve finding the optimal flow of resources through a network, represented as a directed graph with capacities on edges. These problems have numerous real-world applications in transportation, resource allocation, scheduling, and more.
In a flow network, we have:
- Source (s): The node where flow originates
- Sink (t): The node where flow terminates
- Capacity: Each edge has a maximum capacity it can carry
- Flow: The amount of resource flowing through each edge
The fundamental constraints in network flow problems are:
- Capacity constraint: Flow on an edge cannot exceed its capacity
- Conservation of flow: Incoming flow equals outgoing flow at each node (except source and sink)
Interactive Network Flow Visualization
Experiment with network flow algorithms to better understand how they find the maximum flow in a network. The visualization below demonstrates the Ford-Fulkerson algorithm step by step.
Network Flow Algorithms
The Ford-Fulkerson algorithm computes the maximum flow in a flow network. It works by finding augmenting paths from source to sink and incrementing the flow along these paths.
Explanation:
Current Path:
Maximum Flow
The maximum flow problem involves finding the maximum amount of flow that can be sent from source to sink while respecting capacity constraints.
Ford-Fulkerson Algorithm
The Ford-Fulkerson algorithm is a greedy approach for finding the maximum flow:
- Initialize flow on all edges to 0
- While there exists an augmenting path from source to sink:
- Find an augmenting path (a path with available capacity)
- Determine the bottleneck capacity along this path
- Augment the flow along this path by the bottleneck capacity
Edmonds-Karp Algorithm
Edmonds-Karp is an implementation of Ford-Fulkerson that uses BFS to find augmenting paths, guaranteeing a time complexity of O(VE²).
Dinic's Algorithm
Dinic's algorithm improves upon Edmonds-Karp by using level graphs and multiple augmenting paths in each phase, achieving O(V²E) time complexity.
def dinic_max_flow(graph, source, sink):
# Implementation of Dinic's algorithm
# Returns the maximum flow from source to sink
Minimum Cut
A cut in a flow network is a partition of vertices into two disjoint subsets S and T, where source s ∈ S and sink t ∈ T. The capacity of a cut is the sum of capacities of edges going from S to T.
The minimum cut problem seeks to find a cut with minimum capacity. According to the Max-Flow Min-Cut Theorem, the maximum flow equals the minimum cut capacity.
After computing the maximum flow, the minimum cut can be found by:
- Perform a DFS/BFS from the source in the residual graph
- Vertices reachable from the source form set S, the rest form set T
- The edges from S to T form the minimum cut
Applications of minimum cut include:
- Network reliability analysis
- Image segmentation
- Clustering algorithms
Bipartite Matching
Bipartite matching is a classic application of network flow. In a bipartite graph with vertex sets X and Y, a matching is a set of edges with no common vertices.
To solve the maximum bipartite matching problem using network flow:
- Create a flow network with a source s and sink t
- Add edges from s to all vertices in X with capacity 1
- Add edges from all vertices in Y to t with capacity 1
- Add edges from X to Y as in the original bipartite graph, with capacity 1
- Find the maximum flow from s to t
The maximum flow value equals the size of the maximum matching, and the edges with flow 1 from X to Y form the matching.
Applications
- Job assignment problems
- Resource allocation
- Stable marriage problem
Minimum Cost Flow
The minimum cost flow problem extends the maximum flow problem by assigning costs to edges. The goal is to find a flow that satisfies demand/supply constraints while minimizing the total cost.
Key algorithms for solving minimum cost flow include:
- Cycle-Canceling Algorithm: Iteratively find and cancel negative-cost cycles in the residual network
- Successive Shortest Path Algorithm: Repeatedly augment flow along the shortest path from source to sink
- Cost-Scaling Algorithm: Uses scaling techniques to achieve better time complexity
Applications of minimum cost flow include:
- Transportation and logistics optimization
- Network routing with costs
- Supply chain management
Real-World Applications
Network flow algorithms have numerous practical applications:
- Transportation Networks: Optimizing the flow of vehicles, goods, or data
- Project Selection: Choosing projects with dependencies and budget constraints
- Image Segmentation: Using min-cut to separate foreground from background
- Baseball Elimination: Determining if a team can still win a championship
- Airline Scheduling: Assigning crews to flights
- Network Security: Finding vulnerabilities in computer networks
Many seemingly unrelated problems can be transformed into network flow problems, making these algorithms extremely versatile.
Practice Problems
Maximum Bipartite Matching
Given a bipartite graph, find the maximum number of matches possible.
Minimum Cost to Supply Water
Find the minimum cost to supply water to all houses in a village.
Network Reliability
Find the minimum number of edges to remove to disconnect two nodes in a network.
Project Selection
Select a subset of projects to maximize profit while respecting dependencies.