Bipartite Matching Matching 3330: Algorithms A graph is bipartite if its vertex set can be partitioned into two subsets A and B so that each edge has one endpoint in A and the other endpoint in B. A B A matching M is a subset of edges so that every vertex has degree at most one in M. 2 Maximum Matching First Try The bipartite matching problem: Find a matching with the maximum number of edges. Greedy method? (add an edge with both endpoints unmatched) u3 u4 A perfect matching is a matching in which every vertex is matched. v3 The perfect matching problem: Is there a perfect matching? 3 4 Key Questions Existence of Perfect Matching How to tell if a graph does not have a (perfect) matching? How to determine the size of a maximum matching? How to find a maximum matching efficiently? Hall s Theorem [1935]: A bipartite graph G=(A,B;E) has a matching that saturates A if and only if N(S) >= S for every subset S of A, where N(S) is the neighbors of S. S N(S) 5 6 1
Bound for Maximum Matching Algorithmic Idea? What is a good upper bound on the size of a maximum matching? Hall s Theorem [1935]: u3 u4 A bipartite graph G=(A,B;E) has a matching that saturates A if and only if N(S) >= S for every subset S of A. König [1931]: In a bipartite graph, the size of a maximum matching v3 is equal to the size of a minimum vertex cover. Any idea to find a larger matching? 7 8 Augmenting Path Optimality Condition What if there is no more M-augmenting path? If there is no M-augmenting path, then M is maximum! Prove the contrapositive: v3 v3 A bigger matching an M-augmenting path Given a matching M, an M-alternating path is a path that alternates between edges in M and edges not in M. An M-alternating path whose endpoints are unmatched by M is an M-augmenting path. 1. Let M* be a maximal matching and M* > M. Consider 2. Every vertex in has degree at most 2 3. A component in is an even cycle or a path 9 4. Since, an M-augmenting path! 10 Algorithm Finding M-augmenting paths Key: M is maximum no M-augmenting path Orient the edges (edges in M go up, others go down) An M-augmenting path a directed path between two unmatched vertices How to find efficiently? v3 11 12 2
Faster Algorithms Complexity At most n/2 iterations An augmenting path in time by a DFS or a BFS Total running time Observation: Many short and disjoint augmenting paths. Idea: Find augmenting paths simultaneously in one search. 13 14 Application of Bipartite Matching General Matching Isaac Jerry Darek Tom Given a graph (not necessarily bipartite), find a matching with maximum total weight. Marking Tutorials Solutions Newsgroup Job Assignment Problem: Each person is willing to do a subset of jobs. Can you find an assignment so that all jobs are taken care of? unweighted (cardinality) version: a matching with maximum number of edges 15 16 Characterization of Perfect Matching Min-Max Theorem Hall s Theorem [1935]: A bipartite graph G=(A,B;E) has a matching that saturates A if and only if N(S) >= S for every subset S of A. König [1931]: In a bipartite graph, the size of a maximum matching is equal to the size of a minimum vertex cover. Tutte s Theorem [1947]: A graph has a perfect matching if and only if o(g-s) <= S for every subset S of V, where o(g-s) is the number of connected components with an odd number of vertices in the subgraph G-S. Tutte-Berge formula [1958]: The size of a maximum matching = 17 18 3
Augmenting Path? Optimality Condition? What if there is no more M-augmenting path? If there is no M-augmenting path, then M is maximum? Prove the contrapositive: v3 v3 A bigger matching an M-augmenting path Given a matching M, an M-alternating path is a path that alternates between edges in M and edges not in M. An M-alternating path whose endpoints are unmatched by M is an M-augmenting path. 1. Consider 2. Every vertex in has degree at most 2 3. A component in is an even cycle or a path 4. Since, an M-augmenting path! Works for general graphs. 19 20 Algorithm? Finding Augmenting Path in Bipartite Graphs Key: M is maximum no M-augmenting path Orient the edges (edges in M go up, others go down) edges in M having positive weights, otherwise negative weights How to find efficiently? v3 In a bipartite graph, an M-augmenting path corresponds to a directed path. 21 22 Finding M-augmenting paths in General Graphs Don t know how to orient the edges so that: an M-augmenting path an alternating path between two free vertices v 0 v 7 v3 Complexity At most n/2 augmentations. Each augmentation does at most n contractions. An alternating walk can be found in O(m) time. Total running time is O(mn 2 ). v7 v6 v5 An operation called Contraction is needed. 23 24 4
Weighted Bipartite Matching Weighted Bipartite Matching Given a weighted bipartite graph, find a matching with maximum total weight. A B Not necessarily a maximum size matching. 25 Wlog, assume perfect matching on complete bipartite graph; (add fictitious edges with weights 0) 26 First Algorithm First Algorithm Find maximum weight perfect matching Orient the edges (edges in M go up, others go down) edges in M having positive weights, otherwise negative weights How to know if a given matching M is optimal? Idea: Imagine there is a larger matching M* and consider the union of M and M* v3 Then M is maximum if and only if there is no negative cycles 27 28 First Algorithm Key: M is maximum no negative cycle Complexity At most nw iterations A negative cycle in O(n 3 ) time by Floyd Warshall Total running time O(n 4 W) Can choose minimum mean cycle to avoid W How to find efficiently? 29 30 5
Traveling salesman problem Near Optimal solution Faster Easier to implement. EULER CYCLE Given a graph, G, find a cycle that visits every edge exactly once Necessary & Sufficient Condition: G is connected and every vertex is even degree. Algorithm (O(n 2 )): 1. Repeatedly use DFS to find and remove a cycle from G 2. Merge all the cycles into one cycle. 31 Euclidian Traveling Salesman Problem Euclidian Traveling Salesman Problem MST APPROX-TSP-TOUR2(G, c) 1 Select a vertex r Є V[G] to be root. 2 Compute a MST T for G from root r using Prim Alg. 3 Find a minimal-weight matching M for vertices of odd degree in T. 4 Find an Euler cycle C in G = (V, T U M). 5 L=list of vertices in preorder walk of C. 6 return the Hamiltonian cycle H in the order L. Euler Cycle Add Min Matching HAM-Cycle 33 34 Time Complexity APPROX-TSP-TOUR2(G, c) 1 Select a vertex r Є V[G] to be root. 2 Compute a MST T for G from root r using Prim Alg. 3 Find a minimal-weight matching M for vertices of odd degree in T. 4 Find an Euler cycle A in G = (V, T U M). 5 L=list of vertices in preorder walk of A. 6 return the Hamiltonian cycle H in order L. O(1) O(n lg n) O(n 4 ) O(n 2 ) O(n) O(n) Traveling salesman problem This is polynomial-time 3/2-approximation algorithm. (Why?) Because: APPROX-TSP-TOUR2 is O(n 4 ) C(MST) C(H*) Optimal H*: optimal soln C(A) = C(MST)+C(M) A: Euler cycle C(M) 0.5C(H*) M: min matching C(H) C(M) H: approx soln & C(H) 1.5C(H*) triangle inequality Euler cycle Solution 35 36 6
Proof of C(M) 0.5C(H*) Let optimal tour be H*: j 1 i 1 j i i 2 j k i 2m {i 1,i 2,,i 2m }: the set of odd degree vertices in T. Define 2 matchings: M 1 ={[i 1,i 2 ],[i 3,i 4 ],,[i 2m-1,i 2m ]} M 2 ={[i 2,i 3 ],[i 4,i 5 ],,[i 2m,i 1 ]} M is min matching: C(M) C(M1) and C(M) C(M2) By triangle inequality: C(H*) length(m 1 ) + length(m 2 ) 2 length(m) C(M) 1/2 C(H*) 7