Content deleted Content added
Andryandrew (talk | contribs) m Formatting |
m →Explanation: Formatting. |
||
Line 106:
=== Explanation ===
Let the vertices of our graph be partitioned in <code>U</code> and <code>V</code>, and consider a partial matching, as indicated by the <code>Pair_U</code> and <code>Pair_V</code> tables that contain the one vertex to which each vertex of <code>U</code> and of <code>V</code> is matched, or <code>NIL</code> for unmatched vertices. The key idea is to add two dummy vertices on each side of the graph: uDummy connected to all unmatched vertices in <code>U</code> and vDummy connected to all unmatched vertices in <code>V</code>. Now, if we run a [[breadth-first search]] (BFS) from uDummy to vDummy then we can get the paths of minimal length that connect currently unmatched vertices in <code>U</code> to currently unmatched vertices in <code>V</code>. Note that, as the graph is bipartite, these paths always alternate between vertices in <code>U</code> and vertices in <code>V</code>, and we require in our BFS that when going from <code>V</code> to <code>U</code>, we always select a matched edge. If we reach an unmatched vertex of <code>V</code>, then we end at vDummy and the search for paths in the BFS terminate. To summarize, the BFS starts at unmatched vertices in <code>U</code>, goes to all their neighbors in <code>V</code>, if all are matched then it goes back to the vertices in <code>U</code> to which all these vertices are matched (and which were not visited before), then it goes to all the neighbors of these vertices, etc., until one of the vertices reached in <code>V</code> is unmatched.
Observe in particular that BFS marks the unmatched nodes of <code>U</code> with distance 0, then increments the distance every time it comes back to <code>U</code>. This guarantees that the paths considered in the BFS are of minimal length to connect unmatched vertices of <code>U</code> to unmatched vertices of <code>V</code> while always going back from <code>V</code> to <code>U</code> on edges that are currently part of the matching. In particular, the special <code>NIL</code> vertex, which corresponds to vDummy, then gets assigned a finite distance, so the BFS function returns true iff some path has been found. If no path has been found, then there are no augmenting paths left and the matching is maximal.
If BFS returns true, then we can go ahead and update the pairing for vertices on the minimal-length paths found from <code>U</code> to <code>V</code>: we do so using a [[depth-first search]] (DFS). Note that each vertex in <code>V</code> on such a path, except for the last one, is currently matched. So we can explore with the DFS, making sure that the paths that we follow correspond to the distances computed in the BFS. We update along every such path by removing from the matching all edges of the path that are currently in the matching, and adding to the matching all edges of the path that are currently not in the matching: as this is an augmenting path (the first and last edges of the path were not part of the matching, and the path alternated between matched and unmatched edges), then this increases the number of edges in the matching. This is same as replacing the current matching by the symmetric difference between the current matching and the entire path..
Note that the code ensures that all augmenting paths that we consider are vertex disjoint. Indeed, after doing the symmetric difference for a path, none of its vertices could be considered again in the DFS, just because the <code>Dist[Pair_V[v]]</code> will not be equal to <code>Dist[u] + 1</code> (it would be exactly <code>Dist[u]</code>).
Also observe that the DFS does not visit the same vertex multiple times. This is thanks to the following lines:
Dist[u] = ∞
return false
When we were not able to find any shortest augmenting path from a vertex <code>u</code>, then the DFS marks vertex <code>u</code> by setting <code>Dist[u]</code> to infinity, so that these vertices are not visited again.
One last observation is that we actually don't need uDummy: its role is simply to put all unmatched vertices of <code>U</code> in the queue when we start the BFS. As for vDummy, it is denoted as <code>NIL</code> in the pseudocode above.
== See also ==
|