



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Merlin's algorithm for finding a perfect matching in a bipartite graph, as presented in the arthur-merlin story. The text also covers the concept of stable marriage and hall's theorem, which guarantees the existence of a matching that saturates all vertices in one set if certain conditions are met. The document also includes the gale-shapley proposal algorithm for finding a stable matching.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




In the land ruled by the legendary King Arthur, there existed one hundred knights and one hundred ladies. King Arthur proclaimed that all knights and ladies should get married. He soon asked each of the ladies to write down a list of which of the knights they would be willing to wed, and then hand him their preferences.
King Arthur took all of the ladies’ preferences and went back to his castle. He called for his magician, the legendary Merlin, who had access to all the elves, faries, and other mystical creatures. Arthur ordered Merlin to find an marriage so that there would be a perfect matching of the one hundred knights to the one hundred ladies so that each lady is matched to a knight she finds acceptable.
Definition 1.0.1. A bipartite graph is a graph consisting of two disjoint sets of nodes X and Y , and a set of edges connecting pairs of nodes (a, b), with a ∈ X and b ∈ Y.
Definition 1.0.2. A perfect matching (or perfect marriage) on a bipartite graph G = (X, Y, E) is a subset S ⊂ E of the edges such that if (a, b) ∈ S then (a, c) 6 ∈ S for any b 6 = c and (c, b) 6 ∈ S for any c 6 = a.
Soon enough, by way of magic, Merlin obtained a solution and handed the marriage list to Arthur. Arthur, suspicious of Merlin’s abilities, wanted to check that the claimed solution from Merlin was valid. To do so, he used the following algorithm:
ArthursAlgorithm(): For each lady: Check that she is only matched to one knight. Check that the knight she is matched to is on her list.
Such an algorithm takes time polynomial in the number of knights and ladies.
Definition 1.0.3. An algorithm is polynomial-time if its running time for an input of length n is O(nc) for some constant c.
Now suppose that Merlin was not so lucky. When handed the ladies’ preference lists, he was not able to find a perfect matching despite utilizing the powers of fairies, elves, creatures, and the clouds. Returning to Arthur, Merlin explained his situation, but Arthur was unbelieving: “Merlin, you must find a way to match these knights and ladies. If you cannot find a matching, then you shall lose your head.”
The decision question solved by Merlin in the story is: “Given a bipartite graph with n knights and n ladies does there exist a perfect marriage?” We can use ArthursAlgorithm() to verify a proposed solution.
Definition 1.1.1. NP is the set of problems for which Merlin can present a proof to Arthur that the solution is “yes”, such that Arthur can verify the proof in polynomial-time.
For some problems, Arthur does not need Merlin: he can solve the prob- lem himself in polynomial-time.
Definition 1.1.2. P is the set of problems Arthur can solve in polynomial- time (i.e. can do without Merlin).
Arthur, who is hesitant to pay a wizard’s salary and subsidize the fairies, elves, creatures, and other expenses, asks what sort of problems he can pass off to Merlin to solve and which he can still verify the solutions to in polynomial-time. In particular, what are the hardest problems he can give to Merlin to solve and still verify the solutions to.
Definition 1.1.3. A problem X is NP-Hard if X ∈ P ⇒ P = N P.
Suppose G is a bipartite graph such that |NG(S)| ≥ |S| for all S, but G contains no matching saturating all the vertices in X. Let M ∗^ be a maximum- sized matching in G, and let u ∈ X be unmatched in M ∗.
Definition 1.2.5. An alternating path is a path of edges such that exactly every other edge is contained in M ∗.
Definition 1.2.6. An augmenting path is an alternating path in which the first and last edges are unmatched.
The matching size can be increased by one by swapping all the edges of an augmenting path from matched to unmatched an vice versa. Let S ∈ X be the set of all vertices reached by alternating paths from u. Let T = NG(S).
Claim 1.2.7. M ∗^ matches T perfectly with S \ {u}.
The alternating paths reach Y along edges not in M ∗^ and reach u along the edges in M ∗. In other words, every vertex of S \ {u} is reached along an edge in M ∗^ from a vertex in T , so S \ {u} is all matched. But T is matched too, because if there was an unsaturated vertex in T , we would have an alternating path from u to it, contradicting the maximality.
Now we use Hall’s Theorem to construct an algorithm for find a perfect matching.
Theorem 1.2.8. There exists a polynomial time algorithm to find a perfect matching.
Proof. By construction. We present an algorithm in this section which is based on the proof of Hall’s Theorem.
The Augmenting Path Algorithm
Input: A bipartite graph G = (X, Y, E) with a partition of X (knights) and Y (ladies), and a matching M with k edges in G. Output: A matching M ∗^ with at least one more edge than M or a proof that G violates Hall’s condition [2].
The Algorithm: Let u ∈ U be any unmatched vertex in X. Grow a BFS tree from u ∈ U.
Consider only unmatched edges from X to Y and only matched edges from Y to X. If an augmenting path is found, create a new matching M ∗^ by swapping the matched/unmatched edges along this path and output M ∗. If no augmenting path is found, then output S, NG(S). Since no augmenting path is found, |T | = |S \ {u}| and thus |NG(S)| < |S|.
Time complexity: In each step we add one more edge. We need n edges for the final matching and therefore there are n steps. In each step a BFS tree is created. Each vertex and edge is visited once. Therefore the total time complexity is O(n(n + n^2 )) = O(n^3 ).
Now we consider the problem of stable marriage. Like perfect marriage, stable marriage is concerned with pairing nodes of two disjoint sets. Unlike perfect marriage, stable marriage does not have lists of acceptable pairs, but instead an lists of pairs ordered by preference. Thus stable marriage is concerned with the relative preferences of pairings, rather than whether they are valid or not.
Definition 2.0.9. A marriage (matching) is stable if and only if there is no man x and woman a such that x prefers a to his current partner and a prefers x to her current partner.
This problem occurs in assigning medical school residents to hospitals. Each resident ranks the hospitals and each hospital ranks the residents. The goal is to match residents to hospitals without creating a situation where a resident and a hospital both prefer each other to their current matches.
Example: A set of 4 men {x, y, z, w} and 4 women {a, b, c, d}. Each ranked the other group according to “liking”.
Men: Women: x : a > b > c > d a : z > x > y > w y : a > c > b > d b : y > w > x > z z : c > d > a > b c : w > x > y > z w : c > b > a > d d : x > y > z > w
Claim 2.0.12. It cannot be that x is matched to b and y is matched to a such that x prefers a and a prefers x.
Proof. x proposed to b and not a (by previous claim), so x prefers b to a.