0% found this document useful (0 votes)
61 views

Lecture Notes On Bipartite Matching

The document summarizes key concepts related to bipartite matching problems, including: 1) It introduces bipartite graphs and defines matchings, perfect matchings, and exposes vertices. It describes the maximum cardinality matching and minimum weight perfect matching problems. 2) It discusses using alternating paths and augmenting paths to find a maximum matching. An algorithm is described that repeatedly finds and augments along augmenting paths until none remain. 3) König's theorem is presented, stating the maximum matching size equals the minimum vertex cover size. The algorithm simultaneously finds both by labeling vertices based on reachability from exposed vertices.

Uploaded by

vietexob
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Lecture Notes On Bipartite Matching

The document summarizes key concepts related to bipartite matching problems, including: 1) It introduces bipartite graphs and defines matchings, perfect matchings, and exposes vertices. It describes the maximum cardinality matching and minimum weight perfect matching problems. 2) It discusses using alternating paths and augmenting paths to find a maximum matching. An algorithm is described that repeatedly finds and augments along augmenting paths until none remain. 3) König's theorem is presented, stating the maximum matching size equals the minimum vertex cover size. The algorithm simultaneously finds both by labeling vertices based on reachability from exposed vertices.

Uploaded by

vietexob
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Massachusetts Institute of Technology 18.433: Combinatorial Optimization Michel X.

Goemans

Handout 3 February 8th, 2007

Lecture notes on bipartite matching


We start by introducing some basic graph terminology. A graph G = (V, E) consists of a set V of vertices and a set E of pairs of vertices called edges. For an edge e = (u, v), we say that the endpoints of e are u and v; we also say that e is incident to u and v. A graph G = (V, E) is bipartite if the vertex set V can be partitioned into two sets A and B (the bipartition) such that no edge in E has both endpoints in the same set of the bipartition. A matching M E is a collection of edges such that every vertex of V is incident to at most one edge of M . If a vertex v has no edge of M incident to it then v is said to be exposed (or unmatched). A matching is perfect if no vertex is exposed; in other words, a matching is perfect if its cardinality is equal to |A| = |B|.
1 2 3 4 6 exposed matching

7 8

9 10

Figure 1: Example. The edges (1, 6), (2, 7) and (3, 8) form a matching. Vertices 4, 5, 9 and 10 are exposed. We are interested in the following two problems: Maximum cardinality matching problem: Find a matching M of maximum size. Minimum weight perfect matching problem: Given a cost cij for all (i, j) E, nd a perfect matching of minimum cost where the cost of a matching M is given by c(M ) = (i,j)M cij . This problem is also called the assignment problem. Similar problems (but more complicated) can be dened on non-bipartite graphs.

Maximum cardinality matching problem

Before describing an algorithm for solving the maximum cardinality matching problem, one would like to be able to prove optimality of a matching (without reference to any algorithm).

Lecture notes on bipartite matching

For this purpose, one would like to nd upper bounds on the size of any matching and hope that the smallest of these upper bounds be equal to the size of the largest matching. This approach of the problem is identical to the approach we used to derive duality for linear programming. As well see, in this case, the dual problem has a strong combinatorial avor. A vertex cover is a set C of vertices such that all edges e of E are incident to at least one vertex of C. In other words, there is no edge completely contained in V C (we use to denote the dierence of two sets). Clearly, the size of any matching is at most the size of any vertex cover. This follows from the fact that, given any matching M , a vertex cover C must contain at least one of the endpoints of each edge in M . We have just proved weak duality: The maximum size of a matching is at most the minimum size of a vertex cover. As well prove later in these notes, equality in fact holds: Theorem 1 (Knig) For any bipartite graph, the maximum size of a matching is equal to o the minimum size of a vertex cover. We shall prove this theorem algorithmically, by describing an ecient algorithm which simultaneously gives a maximum matching and a minimum vertex cover. Knigs theorem o gives a good characterization of the problem, namely a simple proof of optimality. In the example above, one can prove that the matching (1, 9), (2, 6), (3, 8) and (5, 7) is of maximum size since there exists a vertex cover of size 4. Just take the set {1, 2, 5, 8}. The natural approach to solving this cardinality matching problem is to try a greedy algorithm: Start with any matching (e.g. an empty matching) and repeatedly add disjoint edges until no more edges can be added. This approach, however, is not guaranteed to give a maximum matching. We will now present an algorithm that does work, and is based on the concepts of alternating paths and augmenting paths. A path is simply a collection of edges (v0 , v1 ), (v1 , v2 ), . . . , (vk1 , vk ) where the vi s are distinct vertices. A path can simply be represented as v0 -v1 -. . .-vk . Denition 1 An alternating path with respect to M is a path that alternates between edges in M and edges in E M . Denition 2 An augmenting path with respect to M is an alternating path in which the rst and last vertices are exposed. In the above example, the paths 4-8-3, 6-1-7-2 or 5-7-2-6-1-9 are alternating, but only the last one is augmenting. Notice that an augmenting path with respect to M which contains k edges of M must also contain exactly k + 1 edges not in M . Also, the two endpoints of an augmenting path must be on dierent sides of the bipartition. The most interesting property of an augmenting path P with respect to a matching M is that if we set M = M P (M P ) (P M ), then we get a matching M and, moreover, the size of M is one unit larger than the size of M . That is, we can form a larger matching M from M by taking the edges of P not in M and adding them to M while removing from M the edges in M that are also in the path P . We say that we have augmented M along P . The usefulness of augmenting paths is given in the following theorem.

Lecture notes on bipartite matching

Theorem 2 A matching M is maximum if and only if there are no augmenting paths with respect to M . Proof: (By contradiction) () Let P be some augmenting path with respect to M . Set M = M P . Then M is a matching with cardinality greater than M . This contradicts the maximality of M . () If M is not maximum, let M be a maximum matching (so that |M | > |M |). Let Q = M M . Then: Q has more edges from M than from M (since |M | > |M | implies that |M M | > |M M |). Each vertex is incident to at most one edge in M Q and one edge M Q. Thus Q is composed of cycles and paths that alternate between edges from M and M . Therefore there must be some path with more edges from M in it than from M (all cycles will be of even length and have the same number of edges from M and M ). This path is an augmenting path with respect to M . Hence there must exist an augmenting path P with respect to M , which is a contradiction. This theorem motivates the following algorithm. Start with any matching M , say the empty matching. Repeatedly locate an augmenting path P with respect to M , augment M along P and replace M by the resulting matching. Stop when no more augmenting path exists. By the above theorem, we are guaranteed to have found an optimum matching. The algorithm terminates in augmentations, where is the size of the maximum matching. Clearly, n where n = |V |. 2 In the example, one would thus augment M along an augmenting path, say 5-7-2-6-1-9, obtain the matching (1, 9), (2, 6), (3, 8) and (5, 7), and then realize that no more augmenting paths can be found. The question now is how to decide the existence of an augmenting path and how to nd one, if one exists. These tasks can be done as follows. Direct edges in G according to M as follows : An edge goes from A to B if it does not belong to the matching M and from B to A if it does. Call this directed graph D. Claim 3 There exists an augmenting path in G with respect to M i there exists a directed path in D between an exposed vertex in A and an exposed vertex in B. Exercise 1. Prove claim 3.

This gives an O(m) algorithm (where m = |E|) for nding an augmenting path in G. Let A and B be the set of exposed vertices w.r.t. M in A and B respectively. We can simply attach a vertex s to all the vertices in A and do a depth-rst-search from s till we hit a vertex in B and then trace back our path.

Lecture notes on bipartite matching

Thus the overall complexity of nding a maximum cardinality matching is O(nm). This can be improved to O( nm) by augmenting along several augmenting paths simultaneously. If there is no augmenting path with respect to M , then we can also use our search procedure for an augmenting path in order to construct an optimum vertex cover. Consider the set L (for Labelling) of vertices which can be reached by a directed path from an exposed vertex in A. Claim 4 When the algorithm terminates, C = (AL)(BL) is a vertex cover. Moreover, |C | = |M | where M is the matching returned by the algorithm. This claim immediately proves Knigs theorem. o Proof: We rst show that C is a vertex cover. Assume not. Then there must exist an edge e = (a, b) E with a A L and b (B L). The edge e cannot belong to the matching. If it did, then b should be in L for otherwise a would not be in L. Hence, e must be in E M and is therefore directed from A to B. This therefore implies that b can be reached from an exposed vertex in A (namely go to a and then take the edge (a, b)), contradicting the fact that b L. / To show the second part of the proof, we show that |C | |M |, since the reverse inequality is true for any matching and any vertex cover. The proof follows from the following observations. 1. No vertex in A L is exposed by denition of L, 2. No vertex in B L is exposed since this would imply the existence of an augmenting path and, thus, the algorithm would not have terminated, 3. There is no edge of the matching between a vertex a (AL) and a vertex b (B L). Otherwise, a would be in L. These remarks imply that every vertex in C is matched and moreover the corresponding edges of the matching are distinct. Hence, |C | |M |. Exercise 2. An edge cover of a graph G = (V, E) is a subset of R of E such that every vertex of V is incident to at least one edge in R. Let G be a bipartite graph with no isolated vertex. Show that the cardinality of the minimum edge cover R of G is equal to |V | minus the cardinality of the maximum matching M of G. Give an ecient algorithm for nding the minimum edge cover of G.

Minimum weight perfect matching

By assigning innite costs to the edges not present, one can assume that the bipartite graph is complete. The minimum cost (weight) perfect matching problem is often described by the following story: There are n jobs to be processed on n machines or computers and one would

Lecture notes on bipartite matching

like to process exactly one job per machine such that the total cost of processing the jobs is minimized. Formally, we are given costs cij for every i A, j B and the goal is to nd a perfect matching M minimizing (i,j)M cij . In these notes, we present an algorithm for this problem which is based upon linear programming, and we will take this opportunity to illustrate several important concepts in linear programming. The rst algorithm given for the assignment problem was given by Kuhn [1955], but he showed only niteness of the algorithm. A rened version was given by Jim Munkres [1957], and showed a polynomial running time. An algorithm is polynomialtime if its running time (the number of basic operations to run it) is upper bounded by a polynomial in the size of the input (i.e. the number of bits needed to represent the input). Munkres analysis even shows that the algorithm is strongly polynomial, and this means that the running time is polynomial in the number of numbers involved (i.e. does not depend on the size of the costs cij ). In this algorithm, the number of operations is upper bounded by O(n3 ) where n = |V |. The algorithm is often called the Hungarian method, as it relies on ideas developed by Hungarians including Knig. o We start by giving a formulation of the problem as an integer program. We rst need to associate a point to every matching. For this purpose, given a matching M , let its incidence vector be x where xij = 1 if (i, j) M and 0 otherwise. One can formulate the minimum weight perfect matching problem as follows: Min
i,j

cij xij

subject to: xij = 1


j

iA jB i A, j B i A, j B.

xij = 1
i

xij 0 xij integer

This is not a linear program, but a so-called integer program. Notice that any solution to this integer program corresponds to a matching and therefore this is a valid formulation of the minimum weight perfect matching problem in bipartite graphs. Consider now the linear program (P ) obtained by dropping the integrality constraints: Min
i,j

cij xij

subject to: (P )
j

xij = 1 xij = 1
i

iA jB

Lecture notes on bipartite matching xij 0 i A, j B.

This is the linear programming relaxation of the above integer program. In a linear program, the variables can take fractional values and therefore there are many feasible solutions to the set of constraints above which do not correspond to matchings. But we only care about the optimum solutions. The set of feasible solutions to the constraints in (P ) forms a polytope, and when we optimize a linear constraint over a polytope, the optimum will be attained at one of the corners or extreme points of the polytope. In general, even if all the coecients of the constraint matrix in a linear program are either 0 or 1, the extreme points of a linear program are not guaranteed to have all coordinates integral (this is of no surprise since the general integer programming problem is NP-hard, while linear programming is polynomially solvable). As a result, in general, there is no guarantee that the value ZIP of an integer program is equal to the value ZLP of its LP relaxation. However, since the integer program is more constrained than the relaxation, we always have that ZIP ZLP , implying that ZLP is a lower bound on ZIP for a minimization problem. Moreover, if an optimum solution to a linear programming relaxation is integral then it must also be an optimum solution to the integer program. Exercise 3. Exercise 4. Prove this last claim. Give an example of an integer program where ZIP = ZLP .

However, in the case of the perfect matching problem, the constraint matrix has a very special form and one can show the following very important result. Theorem 5 Any extreme point of (P ) is a 0-1 vector and, hence, is the incidence vector of a perfect matching. Because of the above theorem, the polytope P = {x : xij = 1
j

iA jB i A, j B}

xij = 1
i

xij 0

is called the bipartite perfect matching polytope. To demonstrate the beauty of matchings, we shall give two completely dierent proofs of this result, one purely algorithmic and one purely algebraic. The algebraic proof is related to the notion of totally unimodularity and is presented in Section 3. To prove it algorithmically, we describe an algorithm for solving the minimum weight perfect matching problem. The algorithm is primal-dual. To explain what this means, we need to introduce the notion of duality of linear programs, and lets do it in the specic case

Lecture notes on bipartite matching

of our bipartite matching problem. Suppose we have values ui for i A and vj for j B such that ui + vj cij for all i A and j B. Then for any perfect matching M , we have that cij ui + vj . (1)
(i,j)M iA jB

Thus, iA ui + jB vj is a lower bound on the cost of the minimum cost perfect matching (for bipartite graphs). To get the best lower bound, we would like to maximize this quantity, and therefore we obtain another linear program Max
iA

ui +
jB

vj

subject to: (D) ui + vj cij i A, j B.

This is called the dual linear program (D), and it can be shown to be dual to (P ) in the sense of linear programming duality. The dual constraints can be interpreted as wij 0 where wij = cij ui vj . If, for any instance, we could always nd a feasible solution u, v to (D) and a perfect matching M such that we have equality in (1) (i.e. the cost of the perfect matching is equal to the value of the dual solution) then we would know that the matching found is optimum. Given a solution u, v to the dual, a perfect matching M would satisfy equality if it contains only edges (i, j) such that wij = cij ui vj = 0. This is what is referred to as complementary slackness. However, for a given u, v, we may not be able to nd a perfect matching among the edges with wij = 0. The algorithm performs a series of iterations. It always maintains a dual feasible solution and tries to nd an almost primal feasible solution x satisfying complementary slackness. The fact that complementary slackness is imposed is crucial in any primal-dual algorithm. In fact, the most important (and elegant) algorithms in combinatorial optimization are primal-dual. This is one of the most important tool for designing ecient algorithms for combinatorial optimization problems (for problems which, of course, admit such ecient solutions). More precisely, the algorithm works as follows. It rst starts with any dual feasible solution, say ui = 0 for all i and vj = mini cij for all j. In a given iteration, the algorithm has a dual feasible solution (u, v) or say (u, v, w). Imposing complementary slackness means that we are interested in matchings which are subgraphs of E = {(i, j) : wij = 0}. If E has a perfect matching then the incidence vector of that matching is a feasible solution in (P ) and satises complementary slackness with the current dual solution and, hence, must be optimal. To check whether E has a perfect matching, one can use the cardinality matching algorithm developed earlier in these notes. If the maximum matching output is not perfect then the algorithm will use information from the optimum vertex cover C to update the dual solution in such a way that the value of the dual solution increases (we are maximizing in the dual).

Lecture notes on bipartite matching

In particular, if L is as in the previous section then there is no edge of E between A L and B L. In other words, for every i (A L) and every j (B L), we have wij > 0. Let = min wij .
i(AL),j(BL)

By the above argument, > 0. The dual solution is updated as follows: ui = and vj = ui iAL ui + i A L vj iBL vj j B L

One easily check that this dual solution is feasible, in the sense that the corresponding vector w satises wij 0 for all i and j. What is the value of the new dual solution? The dierence between the values of the new dual solution and the old dual solution is equal to: (|A L| |B L|) = (|A L| + |A L| |A L| |B L|) = ( n |C |), 2

where A has size n/2 and C is the optimum vertex cover for the bipartite graph with edge set E. But by assumption |C | < n , implying that the value of the dual solution strictly 2 increases. One repeats this procedure until the algorithm terminates. At that point, we have an incidence vector of a perfect matching and also a dual feasible solution which satisfy complmentary slackness. They must therefore be optimal and this proves the existence of an integral optimum solution to (P ). Since, by carefully choosing the cost function, one can make any extreme point be the unique optimum solution to the linear program, this proves Theorem 5. Of course, as some of the readers might have noticed, the proof is not complete yet since one needs to prove that the algorithm indeed terminates. This can be proved by noticing that at least one more vertex of B must be reachable from an exposed vertex of A (and no vertex of B becomes unreachable), since an edge e = (i, j) with i (A L) and j B L now has wij = 0 by our choice of . This also gives an estimate of the number of iterations. In at most n/2 iterations, all vertices of B are reachable or the matching found has increased by at least one unit. Therefore, after O(n2 ) iterations, the matching found is perfect. The overall running time of the algorithm is thus O(n4 ) since it takes O(n2 ) to compute the set L in each iteration. By looking more closely at how vertices get labelled between two increases of the size of the matching, one can reduce the running time analysis to O(n3 ). Exercise 5. Check that the running time of the algorithm is indeed O(n3 ).

Example: Consider the instance given by the following cost matrix dened on a bipartite graph with 5 vertices on each side of the bipartition:

Lecture notes on bipartite matching 0 1 1 4 0 2 3 3 0 0 7 9 3 1 3 2 3 1 0 0 3 3 2 2 0

Assume that uT = (2, 3, 0, 2, 0) and v T = (2, 0, 3, 0, 0). The set E of edges with wij = 0 corresponds exactly to the set of edges in Figure 1. The maximum cardinality matching algorithm nds the matching (1, 9), (2, 6), (3, 8) and (5, 7), and the set of labelled vertices is {3, 4, 8}. We compute as =
i{3,4},j{6,7,9,10}

min

wij = 1

corresponding to the edge (3, 9). The new vectors u and v are uT = (2, 3, 1, 1, 0) and v T = (2, 0, 2, 0, 0). The value of the dual solution has increased from 4 units to 5. The corresponding set E now has a perfect matching, namely (1, 6), (2, 7), (3, 9), (4, 8) and (5, 10) of cost 5. Both the matching and the dual solution are optimal.

Total unimodularity

The algebraic proof of Theorem 5 relies on the concept of totally unimodularity. Denition 3 A matrix A is totally unimodular if every square submatrix of A has determinant 1, 0 or +1. The importance of total unimodularity stems from the following theorem. This theorem gives a subclass of integer programs which are easily solved. A polyhedron P is said to be integral if all its vertices or extreme points are integral. Theorem 6 Let A be a totally unimodular matrix. Then, for any integral right-hand-side b, the polyhedron P = {x : Ax b, x 0} is integral. Before we prove this result, two remarks can be made. First, the proof below will in fact show that the same result holds for the polyhedrons {x : Ax b, x 0} or {x : Ax = b, x 0}. In the latter case, though, a slightly weaker condition than totally unimodularity is sucient to prove the result. Secondly, in the above theorem, one can prove the converse as well: If P = {x : Ax b, x 0} is integral for all integral b then A must be totally unimodular. Proof: Adding slacks, we get the polyhedron Q = {(x, s) : Ax + Is = b, x 0, s 0}. One can easily show (see exercise below) that P is integral i Q is integral.

Lecture notes on bipartite matching

10

Consider now any bfs of Q. The basis B consists of some columns of A as well as some columns of the identity matrix I. Since the columns of I have only one nonzero entry per column, namely a one, we can expand the determinant of B along these entries and derive that, in absolute values, the determinant of B is equal to the determinant of some square submatrix of A. By denition of totally unimodularity, this implies that the determinant of B must belong to {1, 0, 1}. By denition of a basis, it cannot be equal to 0. Hence, it must be equal to 1. We now prove that the bfs must be integral. The non-basic variables, by denition, must have value zero. The vector of basic variables, on the other hand, is equal to B 1 b. From linear algebra, B 1 can be expressed as 1 B adj det B where B adj is the adjunct matrix of B and consists of subdeterminants of B. Hence, both b and B adj are integral which implies that B 1 b is integral since | det B| = 1. This proves the integrality of the bfs. Exercise 6. Let P = {x : Ax b, x 0} and let Q = {(x, s) : Ax + Is = b, x 0, s 0}. Show that x is an extreme point of P i (x, b Ax) is an extreme point of Q. Conclude that whenever A and b have only integral entries, P is integral i Q is integral. In the case of the bipartite matching problem, the constraint matrix A has a very special structure and we show below that it is totally unimodular. This alongs with Theorem 6 proves Theorem 5. Remember that A corresponds to the system xij = 1
j

for all i A, for all j B.

xij = 1
i

Theorem 7 The matrix A is totally unimodular. Proof: Consider any square submatrix T of A. We consider three cases. First, if T has a column or a row with all entries equal to zero then the determinant is zero. Secondly, if there exists a column or a row of T with only one +1 then by expanding the determinant along that +1, we can consider a smaller sized matrix T . The last case is when T has at least two nonzero entries per column (and per row). Given the special structure of A, there must in fact be exactly 2 nonzero entries per column. By adding up the rows of T corresponding to the vertices of A and adding up the rows of T corresponding to the vertices of B, one therefore obtains the same vector which proves that the rows of T are linearly dependent, implying that its determinant is zero. This proves the totally unimodularity of A. We conclude with a technical remark. One should rst remove one of the rows of A before applying Theorem 6 since, as such, it does not have full row rank and this fact was implicitly used in the denition of a bfs. However, deleting a row of A still preserves its totally unimodularity.

Lecture notes on bipartite matching Exercise 7. Exercise 8. If A is totally unimodular then AT is totally unimodular. Use total unimodularity to prove Knigs theorem. o

11

The following theorem gives a necessary and sucient condition for a matrix to be totally unimodular. Theorem 8 Let A be a m n matrix with entries in {1, 0, 1}. Then A is TUM if and only if for all subsets R {1, 2, , n} of rows, there exists a partition of R into R 1 and R2 such that for all j {1, 2, , m}:
iR1

aij

iR2

aij {0, 1, 1}.

We will prove only the if direction. Proof: Assume that, for every R, the desired partition exists. We need to prove that the determinant of any k k submatrix of A is in {1, 0, 1}, and this must be true for any k. Let us prove it by induction on k. It is trivially true for k = 1. Assume it is true for k 1 and we will prove it for k. Let B be a k k submatrix of A, and we can assume that B is invertible (otherwise the 1 determinant is 0 and there is nothing to prove). The inverse B 1 can be written as det(B) B , where all entries of B correspond to (k 1) (k 1) submatrices of A. By our inductive hypothesis, all entries of B are in {1, 0, 1}. Let b be the rst row of B and e1 be the 1 k-dimensional row vector [1 0 0 0], thus b = e1 B . By the relationship between B and 1 B , we have that b B = e1 B B = det(B)e1 B 1 B = det(B)e1 . (2) 1 Let R = {i : b {1, 1}}. By assumption, we know that there exists a partition of R 1i into R1 and R2 such that for all j: bij bij {1, 0, 1}. (3)

iR1

iR2

From (2), we have that b bij = 1i


iR

det(B) j = 1 0 j=1

(4)

Since the left-hand-sides of equations (3) and (4) dier by a multiple of 2 for each j (since b {1, 1}), this implies that 1i bij bij
iR2

iR1

=0 j=1 {1, 1} j = 1

(5)

Lecture notes on bipartite matching

12

The fact that we could not get 0 for j = 1 follows from the fact that otherwise B would be singular (we would get exactly the 0 vector by adding and subtracting rows of B). If we dene y Rk by i R1 1 1 i R2 yi = 0 otherwise we get that yB = e1 . Thus y = e1 B 1 = 1 1 e1 B = b , det B det B 1

which implies that det B must be either 1 or -1.

You might also like