Maximum flow and bipartite matching
The maximum flow problem involves finding a flow through a network connecting a source to a sink node which is also the maximum possible. Applications of this problem are manifold from network circulation to traffic control.
The Ford-Fulkerson algorithm is commonly used to calculate the maximum flow on a given graph although a variant called Edmonds-Karp ensures the maximum flow is computed in \( O(V E^2) \) (or \( O(V^2) \) in case an adjacency matrix is used).
Given the following graph
the maximum flow through the network is constrained by the minimum cut passing through the edges directly linked to the sink node. Therefore the maximum flow on the network is 5.
Edmonds-Karp uses breadth-first search as follows
find all paths from source to sink with BFS
for each path
find the maximum flow that can flow through that path
augment the path and increase the total flow
Augmenting a path means saturating its capacity \( c \) with a running flow \( f_p \le c \). The following code implements Edmonds-Karp with an adjacency list on the graph of the previous image
Bipartite Matching
A bipartite graph is a graph whose vertices can be divided into two independent sets such that every edge \( (u,v) \) either \( u \) belongs to the first one and \( v \) to the second one or vice versa. A bipartite graph satisfies the graph coloring condition, i.e. has no odd-length cycles. Bipartite matching is the problem of finding a subgraph in a bipartite graph where no two edges share an endpoint.
An example is the following graph
each edge has a weight of 1 although different weights could also be used to indicate the fitness of a particular node of the left set for a node in the right set (e.g. job fitness / skills / affinity). The same maximum flow algorithm can also be used to find the bipartite matching graph since it coincides with the maximum flow through the network. The graph first has to be modified in order to include neutral (1-weighted per edge) source and sink nodes
After this modification the same algorithm can be applied to obtain a maximum flow of 2 (i.e. two nodes of the left set are suitable to be associated with nodes of the right set).