Lecture 15 Maximum Matching The Hungarian tree method

  • Slides: 32
Download presentation
Lecture 15 Maximum Matching The Hungarian tree method for bipartite graphs 2002 SDU 1

Lecture 15 Maximum Matching The Hungarian tree method for bipartite graphs 2002 SDU 1

Definitions An edge is matched if it is in a matching M, and free

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 otherwi A vertex in X is called an x-vertex; a vertex in Y is called a y-vertex. Matching matched Vertex Free Vertex

Alternating Path Alternating path (with respect to M): A simple path that consists of

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 g h d-h-e: alternating path a-f-b-h-d-i: augmenting path f-b-h-e: not alternating path e-j: augmenting path i j

Algorithm’s idea “Flip” augmenting path to get better matching Note: After flipping, the number

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 iff there is

Idea Theorem (Berge 1975): A matching M in G is maximum iff there 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’

Proof for the other direction ( ) Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|. Consider H = M M’ = (M M’)-(M M’) 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.

a c b d e Edge in M’ Edge in M x y a

a c b d e Edge in M’ Edge in M x y a c y 2002 SDU z h i b e h i Edges in M M’ x 7

Idea of Algorithm Start with an arbitrary matching While we still can find an

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 Start with arbitrary matching

Labelling Algorithm Pick a free x-vertex, say r, and label it outer. r(o)

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 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

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. a(i) b(i) r(o)

Labelling Algorithm b(i) a(i) c(o) d(o) r(o)

Labelling Algorithm b(i) a(i) c(o) d(o) r(o)

Labelling Algorithm e(i) c(o) b(i) a(i) d(o) r(o)

Labelling Algorithm e(i) c(o) b(i) a(i) d(o) r(o)

Alternating Tree Until find an augmenting path (i. e. , by encountering an inner

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) c(o) b(i) a(i) d(o) r(o) b(i) a(i) d(o) c(o) e(i)

Flip!

Flip!

Alternating Tree or the tree is blocked, i. e. , cannot be extended any

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) c(o) d(o) r(o) b(i) a(i) d(o) c(o)

Hungarian Tree Notice: if there is an edge (x, y), x T, and y

Hungarian Tree Notice: if there is an edge (x, y), x T, and y T, 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 contra x(i) a(i) c(o) y d(o) r(o)

Hungarian Tree Observation:If, in the process of searching for an augmenting path, a Hungarian

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) 2002 SDU y d(o) r(o) 19

Hungarian Tree Observation:If, in the process of searching for an augmenting path, a Hungarian

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 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.

Hungarian Tree In the remaining graph, no x-vertex is free, stop.

Overall algorithm Input: Abipartite graph (X Y, E) Output: A maximum matching M in

Overall algorithm Input: Abipartite graph (X Y, E) Output: A maximum matching M in G. § 1. Initialize M to 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 G G-T § 5. else find an augmenting path p in T and let M=M P. § 6. end while. O(V 2+VE)—running BFS once costs O(V+E) time, and there at most O(V) free xvertices.

An O(n 2. 5)-algorithm -Hopcroft and Karp 2002 SDU 24

An O(n 2. 5)-algorithm -Hopcroft and Karp 2002 SDU 24

An 2. 5 O(n )-algorithm Lemma 17. 2 Let M be a matching, p

An 2. 5 O(n )-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 p p’, 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 25 p’.

An 2. 5 O(n )-algorithm Lemma 17. 3 Let M be a matching, p

An 2. 5 O(n )-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|p p’|. Proof. Let M’= M p p’. By lemma 17. 1, M’ is a matching and |M’|=|M|+2. By Theorem 17. 1, M M’ contains two vertex-disjoint augmenting paths p 1 and p 2 with respect to M. Since, by Lemma 17. 2, M M’=p p’, we have |p p’| |p 1|+|p 2|. Since p is of shortest length, |p 1| |p| and |p 2| |p|. Therefore, |p p’| 2|p|. From the identity |p p’|=|p|+|p’|-|p p’|, we obtain |p’| |p|+2|p p’|.

An O(n 2. 5)-algorithm Let M be matching, k the length of a shortest

An O(n 2. 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. 2002 SDU 27

Theorem 17. 5 The number of phases required to find a maximum matching in

Theorem 17. 5 The number of phases required to find a maximum matching in a bipartite graph is at most. proof. Let M be 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 BIMATCH 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

Algorithm BIMATCH 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Start with the empty matching M={}. Maximum false While not maximum (construct a dag D) L 0 Set of free vertices in X L 1 {y Y |(x, y) E for some x L 0} E 0 = {(x, y) E | x L 0, y L 1} Mark all vertices in L 0 and L 1 i 0 while Li+1 contains no free vertices and is not empty i i+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 {y Y | 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 L 0. Remove all vertices on p and incident edges from the dag D. Set M= M p. 20. end for. 21. end if 2002 SDU 29 22. end while.

 2002 SDU 30

2002 SDU 30

 2002 SDU 31

2002 SDU 31

 2002 SDU 32

2002 SDU 32