320 likes | 492 Views
Lecture 15 Maximum Matching. The Hungarian tree method for bipartite graphs. Definitions. An edge is matched if it is in a matching M , and free otherwise. A vertex is matched if it is incident to a matched edge, and free otherwise.
E N D
Lecture 15 Maximum Matching • The Hungarian tree method for bipartite graphs
Definitions An edge is matched if it is in a matching M, and free otherwise. A vertex is matched if it is incident to a matched edge, and free otherwise. A vertex in X is called an x-vertex; a vertex in Y is called a y-vertex. Matching Free Vertex matched Vertex
Alternating Path • Alternating path (with respect to M): A simple path that consists of alternating matched and unmatched edges. • Augmenting path (with respect to M): An alternating path with both endpoints free. a c d e b f h i j g d-h-e: alternating path a-f-b-h-d-i: augmenting path f-b-h-e: not alternating path e-j: augmenting path
Algorithm’s idea • “Flip” augmenting path to get better matching • Note: After flipping, the number of matched edges will increase by 1!
Idea • Theorem (Berge 1975): A matching M in G is maximum iffthere is no augmenting path Proof: • () If there is an augmenting path, clearly not maximum. (Flip matching and non-matching edges in that path to get a “better” matching!)
Proof for the other direction • () Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|. • Consider H = MM’ = (MM’)-(MM’)i.e. a set of edges in M or M’ but not both • H has two properties: • Within H, number of edges belong to M’ > number of edges belong to M. • H can be decomposed into a set of paths. All paths should be alternating between edges in M and M’. • There should exist a path with more edges from M’. Also, it is alternating with respect to M.
c a d e b Edge in M’ Edge in M z y h i x b c a e Edges in MM’ x h y i
Idea of Algorithm • Start with an arbitrary matching • While we still can find an augmenting path • Find the augmenting path P • Flip the edges in P
Labelling Algorithm • Start with arbitrary matching
Labelling Algorithm • Pick a free x-vertex, say r, and label it outer . r(o)
Labelling Algorithm • Run BFS along alternating unmatched and matched edges r(o)
Labelling Algorithm • Label a y-vertex inner if it is adjacent to r through a free edge, and grow this breadth-first tree along alternating unmatched and matched edges. Label the vertices encountered outer and inner alternatively. b(i) a(i) r(o)
Labelling Algorithm b(i) a(i) c(o) d(o) r(o)
Labelling Algorithm e(i) b(i) a(i) c(o) d(o) r(o)
Alternating Tree Until find an augmenting path (i.e., by encountering an inner vertex which is free ), e.g., r-a-c-e. r(o) e(i) b(i) a(i) b(i) a(i) d(o) c(o) c(o) d(o) r(o) e(i)
Alternating Tree or the tree is blocked, i.e., cannot be extended any more. In this case, the alternating tree is called a Hungarian tree. r(o) b(i) a(i) b(i) a(i) d(o) c(o) c(o) d(o) r(o)
Hungarian Tree • Notice: if there is an edge (x, y), x T, and yT, then x must • be an inner vertex. Otherwise, the tree should be extended through • (x,y) to y. It follows that • no vertex in a Hungarian tree can occur in an augmenting path P. • Both ends of P are free vertices, so, they cannot be in T • (the root of T cannot be the end of P). Then P • “enters” T through an inner vertex and an unmatched edge. Then • P must follow some alternating path in T until it “leaves” T • also through an inner vertex but through a matched edge. A contradiction. x(i) a(i) c(o) d(o) r(o) y
Hungarian Tree • Observation:If, in the process of searching for an augmenting path, a Hungarian tree is found, then it can be removed permanently without affecting the search. x(i) a(i) c(o) d(o) r(o) y
Hungarian Tree • Observation:If, in the process of searching for an augmenting path, a Hungarian tree is found, then it can be removed permanently without affecting the search. e(i) f(i) y(o)
Hungarian Tree Another augmenting path y-f is found. Flip. e(i) f(i) y(o)
Hungarian Tree In the remaining graph, no x-vertex is free, stop.
Overall algorithm • Input:Abipartite graph (XY, E) • Output: A maximum matching M in G. • 1. Initialize Mto any arbitrary (possibly empty) matching • 2.while there exists a free x-vertex and a free y-vertex • 3. let r be a free x-vertex. Using breadth-first search, grow an alternating tree T rooted at r • 4. if T is a Hungarian tree then let GG-T • 5. else find an augmenting path p in T and let M=MP. • 6. end while. • O(V2+VE)—running BFS once costs O(V+E) time, and there are at most O(V) free x-vertices.
An O(n2.5)-algorithm • Lemma 17.2 Let M be a matching, p an augmenting path with respect to M, and p’ an augmenting path with respect to M p. Let M’=M p p’. Then, M M’=p p’. • Proof. We only need to consider edges in p p’. Let e be an edge in p p’. If e is in p p’, then its status (matched or unmatched ) in M is different from its status in M’ since its status will change only once: either by p or p’. Consequently, e is in M M’. On the other hand, if e is in pp’, then its status is the same in both M and M’ since its status will change twice:first by p and then by p’, that is , e is not in M M’. Consequently, M M’=p p’.
An O(n2.5)-algorithm • Lemma 17.3 Let M be a matching, p a shortest augmenting path with respect to M, and p’ an augmenting path with respect to M p. Then, |p’| |p|+2|pp’|. Proof. Let M’= M p p’. By lemma 17.1, M’ is a matching and |M’|=|M|+2. By Theorem 17.1, MM’ contains two vertex-disjoint augmenting paths p1 and p2 with respect to M. Since, by Lemma 17.2, M M’=p p’, we have |p p’| |p1|+|p2|. Since p is of shortest length, |p1||p| and |p2||p|. Therefore, |p p’|2|p|. From the identity |p p’|=|p|+|p’|-|pp’|, we obtain |p’||p|+2|pp’|.
An O(n2.5)-algorithm • Let M be matching, k the length of a shortest augmenting path with respect to M and S a maximal set of vertex-disjoint augmenting paths with respect to M of length k. Let M’ be obtained from M by augmenting M by all the augmenting paths in S. Let p be an augmenting path in M’. We have the following corollary of Lemma 17.3. • Corollary 17.2. |p| k + 2. • Theorem 17.5 The number of phases required to find a maximum matching in a bipartite graph is at most .
Theorem 17.5 The number of phases required to find a maximum matching in a bipartite graph is at most . • proof. Let Mbe the matching obtained after at least phases and M* a maximum matching. Since the length of augmenting paths increases from one phase to the next by at least 2, the length of any augmenting path in M is at least +1. BY theorem 17.1, there are exactly |M*|-|M| vertex-disjoint augmenting paths with respect to M. Since the length of each path is at least +1, and hence each path consists of at least +2 vertices, we must have . Since each phase contributes at least one augmenting path, the remaining number of phases is at most . It follows that the total number of phases required by the algorithm is at most
Algorithm BIMATCH3 • Start with the empty matching M={}. • Maximumfalse • While not maximum (construct a dag D) • L0Set of free vertices in X • L1{yY |(x,y) E for some x L0} • E0 = {(x,y) E | x L0, y L1} • Mark all vertices in L0 and L1 • i0 • while Li+1 contains no free vertices and is not empty • ii+2 • Li{x X | x is unmarked and is joined by a matched edge to a vertex y Li-1} • Ei-1 = {(x,y) E | y Li-1, x Li} • Li+1{yY | y is unmarked and is joined by an unmatched edge to a vertex x Li } • Ei = {(x,y) E | x Li, y Li+1} • mark all vertices in Li and Li+1 • end while • if Li+1 is empty then maximum true • else • for each free vertex y Li+1 {augment} Starting at y, use depth-first search to find an augmenting path p that ends at a free vertex x L0. Remove all vertices on p and incident edges from the dag D. Set M= Mp. • end for. • end if • end while.