Bipartite Matchings and Stable Marriage Meghana Nasre Department of Computer Science and Engineering Indian Institute of Technology, Madras Faculty Development Program SSN College of Engineering, Chennai December 12, 2014
Why matchings? Several real world applications involve assigning: jobs to machines. students to projects. jobs to applicants. roommate to another.
Outline of Talk Quick introduction to graphs. Matchings in graphs. Bipartite matching algorithm. Stable matchings.
Introduction to graphs
G = (V, E). Representation and search techniques v1 v2 v6 v3 v5 How large can m be? v4 n = V = 6, m = E = 8.
G = (V, E). Representation and search techniques v1 v2 v6 v3 v5 How large can m be? Representation: v4 n = V = 6, m = E = 8. Adjacency Lists O(m + n) space. Adjacency Matrix O(n 2 ) space.
G = (V, E). Representation and search techniques v1 v2 v6 v3 v5 How large can m be? Representation: v4 n = V = 6, m = E = 8. Adjacency Lists O(m + n) space. Adjacency Matrix O(n 2 ) space. Search methods: Breadth First Search. Depth First Search.
Matching in a graph
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v6 v3 v5 v4
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v6 v3 v5 v4
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v6 v3 v5 v4 Goal: compute a maximum sized matching. Question: is the above matching maximum sized?
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v1 v2 v6 v6 v3 v5 v3 v5 v4 v4
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v1 v2 v6 v6 v3 v5 v3 v5 v4 Maximal: No more edges can be added to the matching. v4
Matching in a graph A matching M is a set of vertex disjoint edges. v1 v2 v1 v2 v6 v6 v3 v5 v3 v5 v4 Maximal: No more edges can be added to the matching. Size may not be largest possible. Maximum: Has size as large as possible. Note that every maximum matching is maximal. v4
Alternating paths A path having alternate matched and unmatched edges. v1 v2 v6 v3 v5 v4
Alternating paths A path having alternate matched and unmatched edges. v1 v2 v6 v3 v5 v4 Is there any other alternating path?
Alternating paths A path having alternate matched and unmatched edges. v1 v2 v6 v3 v5 v4 Is there any other alternating path? Which paths are not alternating?
Augmenting paths An alternating path starting and ending in free edges. v1 v2 v6 v3 v5 v4
Augmenting paths An alternating path starting and ending in free edges. v1 v2 v6 v3 v5 v4 How are augmenting paths useful? Properties of augmenting paths.
Using augmenting paths Berge s Theorem If aug. path P is present size of matching can be increased.
Using augmenting paths Berge s Theorem If aug. path P is present size of matching can be increased. M = M P. v1 v2 v1 v2 v6 augment M with P v6 v3 P v5 v3 P v5 v4 M M v4
Using augmenting paths Berge s Theorem If no aug. path w.r.t. M M is maximum. Proof (by contradiction) Suppose M does not admit any aug. path and still it is not maximum. Some other matching M is maximum.
Using augmenting paths Berge s Theorem If no aug. path w.r.t. M M is maximum. Proof (by contradiction) Suppose M does not admit any aug. path and still it is not maximum. Some other matching M is maximum. Consider M M. Construct an aug. path w.r.t. M.
Maximum matching algorithm Given G = (V, E), compute a maximum sized matching.
Maximum matching algorithm Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M.
Maximum matching algorithm Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Two questions that need to be always asked: 1. Correctness? 2. Complexity/ Running time?
Maximum matching algorithm Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Two questions that need to be always asked: 1. Correctness? 2. Complexity/ Running time? How to efficiently compute an augmenting path?
Bipartite matching algorithm
Bipartite graphs G = (A B, E). A B Equivalent statements Vertices can be partitioned into 2 disjoint sets. G is bipartite iff G does not have any odd cycle. Graph is 2 colorable iff it is bipartite.
Finding aug. path in bipartite graphs a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 A B
Finding aug. path in bipartite graphs a1 a2 a3 b1 b2 b3 Aug. paths w.r.t. M: a 1, b 2. a 5, b 5. a4 b4 a5 b5 A B
Finding aug. path in bipartite graphs a1 a2 a3 a4 b1 b2 b3 b4 Aug. paths w.r.t. M: a 1, b 2. a 5, b 5. a 4, b 3, a 2, b 4. a5 b5 A B
Finding aug. path in bipartite graphs a1 a2 a3 a4 a5 A B b1 b2 b3 b4 b5 Aug. paths w.r.t. M: a 1, b 2. a 5, b 5. a 4, b 3, a 2, b 4. Finding aug. paths reachability in a modified graph.
Finding aug. path in bipartite graphs a1 b1 a1 b1 a2 b2 a2 b2 a3 b3 s a3 b3 t a4 b4 a4 b4 a5 b5 a5 b5 A B A B
Finding aug. path in bipartite graphs a1 b1 a1 b1 a2 b2 a2 b2 a3 b3 s a3 b3 t a4 b4 a4 b4 a5 b5 a5 b5 A B A B Add dummy nodes s and t. Add edges from s to unmatched vertices in A. Add edges from unmatched vertices in B to t. Matched edges: B A. Unmatched edges: A B.
Finding aug. path in bipartite graphs a1 b1 a1 b1 a2 b2 a2 b2 a3 b3 s a3 b3 t a4 b4 a4 b4 a5 b5 a5 b5 A B A B Aug. path in G w.r.t. M is a simple directed s to t path in G.
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time?
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time? 1. How many iterations?
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time? 1. How many iterations? at most n/2.
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time? 1. How many iterations? at most n/2. 2. How long does each iteration take?
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time? 1. How many iterations? at most n/2. 2. How long does each iteration take? at most O(m + n).
Maximum matching in bipartite graph Given G = (V, E), compute a maximum sized matching. Iterative Improvement Initialize M to be empty. while there exists an aug. path P w.r.t. M M = M P. return M. Running time? 1. How many iterations? at most n/2. 2. How long does each iteration take? at most O(m + n). 3. Total running time: O(mn).
Efficient algorithms The best known for both bipartite and general graphs is O( nm) time algorithms. Algorithms on general graphs are significantly involved.
Efficient algorithms The best known for both bipartite and general graphs is O( nm) time algorithms. Algorithms on general graphs are significantly involved. Edmond s Blossom Shrinking algorithm. (Also the paper where the notion of polynomial time being efficient was formalized). Weighted matchings can also be done in polynomial time. Beautiful theory of Linear Programming Duality involved.
Stable matching problem
A bit of history.. Introduced by Gale and Shapley in 1962 while studying college admissions. Bipartite matching problem with preferences. Arises in several places like TA-allocation, project allocation. Classical setting: marriage between n men and n women.
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is optimal.
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is optimal. m1 w1 m2 w2 m3 w3 Question: Is this a good matching?
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is optimal. m1 w1 m2 w2 m3 w3 Question: Is this a good matching? pair (m 2, w 2 ) is bad!
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable.
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. m1 w1 m1 w1 m2 w2 m2 w2 m3 w3 m3 w3 unstable. stable.
Setting Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. m1 w1 m1 w1 m2 w2 m2 w2 m3 w3 m3 w3 unstable. stable. unstable pair: A pair (m, w) not matched to each other, both of which prefer each other to their current partners in M.
Stable marriage problem Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance?
Stable marriage problem Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance? Is stable marriage unique?
Stable marriage problem Input: m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance? Is stable marriage unique? Can it be computed efficiently?
Stable marriage problem Remarkable result of Gale and Shapley m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance?
Stable marriage problem Remarkable result of Gale and Shapley m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance? Yes, always! Is stable marriage unique?
Stable marriage problem Remarkable result of Gale and Shapley m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance? Yes, always! Is stable marriage unique? No, there is a range of stable matchings; can be unique in some cases. Can it be computed efficiently?
Stable marriage problem Remarkable result of Gale and Shapley m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 Goal: To compute a matching that is stable. Does a stable marriage exist in any instance? Yes, always! Is stable marriage unique? No, there is a range of stable matchings; can be unique in some cases. Can it be computed efficiently? Yes, in O(n 2 ) time.
Stable marriage algorithm m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 set all men and women as unengaged. while there exists an unengaged man m 1. m proposes to the most preferred woman w to whom he has not yet proposed. 2. w accepts if either she is unengaged or she is engaged to m and w prefers m to m.
Stable marriage algorithm m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 set all men and women as unengaged. while there exists an unengaged man m 1. m proposes to the most preferred woman w to whom he has not yet proposed. 2. w accepts if either she is unengaged or she is engaged to m and w prefers m to m. Questions: Does the algorithm even terminate?
Stable marriage algorithm m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 set all men and women as unengaged. while there exists an unengaged man m 1. m proposes to the most preferred woman w to whom he has not yet proposed. 2. w accepts if either she is unengaged or she is engaged to m and w prefers m to m. Questions: Does the algorithm even terminate? Why does it output a stable marriage?
Stable marriage algorithm m 1 : w 1, w 2, w 3 m 2 : w 2, w 3, w 1 m 3 : w 3, w 1, w 2 w 1 : m 3, m 2, m 1 w 2 : m 2, m 1, m 3 w 3 : m 1, m 3, m 2 set all men and women as unengaged. while there exists an unengaged man m 1. m proposes to the most preferred woman w to whom he has not yet proposed. 2. w accepts if either she is unengaged or she is engaged to m and w prefers m to m. Questions: Does the algorithm even terminate? Why does it output a stable marriage? How does the ordering of men in the while loop matter?
Stable marriage algorithm A surprisingly simple algorithm that is guaranteed to produce a stable matching. A rich structure underlying the problem.
Stable marriage algorithm A surprisingly simple algorithm that is guaranteed to produce a stable matching. A rich structure underlying the problem. Has been dealt in two books one by Irving and Gusfield and a recent one by Manlove. National Residency Matching program one of the most important applications amongst several others. Pioneering work by Roth and Shapley won the 2012 Nobel prize for Economics.
Summary Matchings, definitions, augmenting/alternating paths. A template for finding matchings by iterative improvement. An efficient algorithm in the bipartite case. Stable marriage algorithm and properties.
Thank You!