• Aucun résultat trouvé

The Matching Problem

Dans le document ALGORITHMS and THEORY of COMPUTATION HANDBOOK (Page 161-169)

Advanced Combinatorial Algorithms

7.2 The Matching Problem

An entire book [23] has been devoted to the study of various aspects of the matching problem, ranging from necessary and sufficient conditions for the existence of perfect matchings to algorithms for solving the matching problem. Many of the basic algorithms studied in Chapter 6 play an important role in developing various implementations for network flow and matching algorithms.

First thematchingproblem, which is a special case of the max-flow problem is introduced. Then the assignment problem,a generalization of the matching problem, is studied.

The maximum matching problem is discussed in detail only for bipartite graphs. The same principles are used to design efficient algorithms to solve the matching problem in arbitrary graphs. The algorithms for general graphs are complex due to the presence of odd-length cycles calledblossoms,and the reader is referred to [26, Chapter 10], or [29, Chapter 9] for a detailed treatment of how blossoms are handled.

Matching Problem Definitions

Given a graphG= (V, E), amatchingMis a subset of the edges such that no two edges inMshare a common vertex. In other words, the problem is that of finding a set of independent edges, that have no incident vertices in common. The cardinality ofMis usually referred to as itssize.

The following terms are defined with respect to a matchingM. The edges inMare calledmatched edges and edges not inMare calledfree edges.Likewise, a vertex is amatched vertexif it is incident to a matched edge. Afree vertexis one that is not matched. Themateof a matched vertexvis its neighborwthat is at the other end of the matched edge incident tov. A matching is calledperfectif all vertices of the graph are matched in it. (When the number of vertices is odd, we permit one vertex to remain unmatched.) The objective of the maximum matching problem is to maximize|M|, the size of the matching. If the edges of the graph have weights, then theweightof a matching is defined to be the sum of the weights of the

edges in the matching. A pathp=[v1, v2, . . . , vk] is called analternating pathif the edges(v2j−1, v2j), j = 1,2, . . .are free, and the edges(v2j, v2j+1), j = 1,2, . . .are matched. Anaugmenting path p =[v1, v2, . . . , vk] is an alternating path in which bothv1 andvk are free vertices. Observe that an augmenting path is defined with respect to a specific matching. The symmetric difference of a matching Mand an augmenting pathP,MP, is defined to be(MP )(PM). It can be shown thatMP is also a matching. Figure 7.1shows an augmenting pathp=[a, b, c, d, g, h] with respect to the given matching. The symmetric difference operation can be also be used as above between two matchings. In this case, the resulting graph consists of a collection of paths and cycles with alternate edges from each matching.

FIGURE 7.1 An augmenting pathpwith respect to a matching.

Applications of Matching

Matchings lie at the heart of many optimization problems and the problem has many applications: assigning workers to jobs, assigning a collection of jobs with precedence constraints to two processors such that the total execution time is minimized, determining the structure of chemical bonds in Chemistry, matching moving objects based on a sequence of snapshots, and localization of objects in space after obtaining information from multiple sensors (see [1]).

Matchings and Augmenting Paths

The following theorem gives necessary and sufficient conditions for the existence of a perfect matching in a bipartite graph.

THEOREM 7.1 (Hall’s Theorem)A bipartite graphG=(X, Y, E)with|X| = |Y|has a perfect matching if and only if ∀S⊆X,|N(S)| ≥ |S|, whereN(S)Yis the set of vertices that are neighbors of some vertex inS.

Although the above theorem captures exactly the conditions under which a given bipartite graph has a perfect matching, it does not lead to an algorithm for finding perfect matchings directly. The following lemma shows how an augmenting path with respect to a given matching can be used to increase the size of a matching. An efficient algorithm will be described later that uses augmenting paths to construct a maximum matching incrementally.

LEMMA 7.1 LetPbe the edges on an augmenting pathp=[v1, . . . , vk] with respect to a matching M. ThenM=MPis a matching of cardinality|M| +1.

PROOF SincePis an augmenting path, bothv1andvkare free vertices inM. The number of free edges inPis one more than the number of matched edges in it. The symmetric difference operator replaces the matched edges ofMinP by the free edges inP. Hence, the size of the resulting matching,|M|, is one more than|M|.

The following theorem provides a necessary and sufficient condition for a given matchingMto be a maximum matching.

THEOREM 7.2 A matchingMin a graphGis a maximum matching if and only if there is no augmenting path inGwith respect toM.

PROOF If there is an augmenting path with respect toM, thenMcannot be a maximum matching, since by Lemma 7.1 there is a matching whose size is larger than that ofM. To prove the converse we show that if there is no augmenting path with respect toMthenMis a maximum matching. Suppose that there is a matchingMsuch that|M|>|M|. Consider the subgraph ofGinduced by the edgesMM. Each vertex in this subgraph has degree at most two, since each node has at most one edge from each matching incident to it. Hence, each connected component of this subgraph is either a path or a simple cycle. For each cycle, the number of edges ofMis the same as the number of edges ofM. Since|M|>|M|, one of the paths must have more edges fromMthan fromM. This path is an augmenting path inGwith respect to the matchingM, contradicting the assumption that there were no augmenting paths with respect toM. Bipartite Matching Algorithm

High-Level Description: The algorithm starts with the empty matchingM = ∅, and augments the matching in phases. In each phase, an augmenting path with respect to the current matchingMis found, and it is used to increase the size of the matching. An augmenting path, if one exists, can be found in O(|E|)time, using a procedure similar to breadth-first search.

The search for an augmenting path proceeds from the free vertices. At each step when a vertex inXis processed, all its unvisited neighbors are also searched. When a matched vertex inYis considered, only its matched neighbor is searched. This search proceeds along a subgraph referred to as theHungarian tree.

The algorithm uses a queueQto hold vertices that are yet to be processed. Initially, all free vertices in Xare placed in the queue. The vertices are removed one by one from the queue and processed as follows.

In turn, when vertexvis removed from the queue, the edges incident to it are scanned. If it has a neighbor in the vertex setYthat is free, then the search for an augmenting path is successful; procedureAugment is called to update the matching, and the algorithm proceeds to its next phase. Otherwise, add the mates of all the matched neighbors ofvto the queue if they have never been added to the queue, and continue the search for an augmenting path. If the algorithm empties the queue without finding an augmenting path, its current matching is a maximum matching and it terminates.

The main data structure that the algorithm uses are the arraysmateandfree.The arraymateis used to represent the current matching. For a matched vertexvG,mate[v] denotes the matched neighbor of vertexv. ForvX,free[v] is a vertex inYthat is adjacent tovand is free. If no such vertex exists then free[v]= 0. The setAstores a set of directed edges(v, v)such that there is an alternating path of two edges fromvtov. This will be used in the search for augmenting paths from free vertices, while extending the alternating paths. When we add a vertexvto the queue, we setlabel[v] tovif we came tovfromv, since we need this information to augment on the alternating path we eventually find.

Bipartite Matching(G=(X, Y, E)) 1 forall verticesvinGdo

2 mate[v]←0.

3 end-for 4 done←false.

5 while notdonedo 6 Initialize.

7 MakeEmptyQueue(Q).

8 for all verticesxXdo (* add unmatched vertices toQ*) 9 if mate[x]=0then

10 Push(Q, x). 11 label[x]←0.

12 end-if 13 end-for 14 found←false.

15 while not foundand not Empty(Q)do

16 x←Pop(Q).

17 if f ree[x]=0then (* found augmenting path *) 18 Augment(x).

19 found←true.

20 else (* extend alternating paths fromx*) 21 for all edges(x, x)Ado

22 if label[x]=0then (*xnot already inQ*) 23 label[x]←x.

24 Push(Q, x).

25 end-if

26 end-for

27 end-if

28 if Empty(Q)then 29 done←true.

30 end-if 31 end-while 32 end-while end-proc

Initialize

1 for all verticesxXdo 2 f ree[x]←0.

3 end-for

4 for all edges(x, y)Edo

5 if mate[y]=0then f ree[x]←y

6 else if mate[y]=xthen AA(x, mate[y]). 7 end-if

8 end-for end-proc

Augment(x)

1 if label[x]=0then 2 mate[x]←f ree[x].

3 mate[f ree[x]]x 4 else

5 f ree[label[x]]mate[x]

6 mate[x]f ree[x]

7 mate[f ree[x]]←x 8 Augment(label[x]) 9 end-if

end-proc

Sample Execution

Figure 7.2shows a sample execution of the matching algorithm. We start with a partial matching and show the structure of the resulting Hungarian tree. In this example, the search starts from the free vertex b. We addcandetoQ. After we explorec, we adddtoQ, and thenf anda. Sincef ree[a]=u, we stop since an augmenting path from vertexbto vertexuis found by the algorithm.

FIGURE 7.2 Sample execution of matching algorithm.

Analysis

If there are augmenting paths with respect to the current matching, the algorithm will find at least one of them. Hence, when the algorithm terminates, the graph has no augmenting paths with respect to the current matching and the current matching is optimal. Each iteration of the main while loop of the algorithm runs inO(|E|)time. The construction of the auxiliary graphAand computation of the array f reealso takeO(|E|)time. In each iteration, the size of the matching increases by one and thus, there are at most min(|X|,|Y|)iterations of the while loop. Therefore the algorithm solves the matching problem for bipartite graphs in timeO(min(|X|,|Y|)|E|). Hopcroft and Karp (see [26]) showed how to improve the running time by finding a maximal set of disjoint augmenting paths in a single phase inO(|E|)time.

They also proved that the algorithm runs in onlyO(

|V|)phases, yielding a worst-case running time of O(

|V||E|).

The Matching Problem in General Graphs

The techniques used to solve the matching problem in bipartite graphs do not extend directly to non-bipartite graphs. The notion of augmenting paths and their relation to maximum matchings (Theorem 7.2) remain the same. Therefore the natural algorithm of starting with an empty matching and increasing its size repeatedly with an augmenting path until no augmenting paths exist in the graph still works. But the problem of finding augmenting paths in non-bipartite graphs is harder. The main trouble is due to odd length cycles known asblossomsthat appear along alternating paths explored by the algorithm as it is looking for augmenting paths. We illustrate this difficulty with an example inFig. 7.3. The search for an augmenting path from an unmatched vertex such ase, could go through the following sequence of vertices [e, b, g, d, h, g, b, a]. Even though the augmenting path satisfies the “local” conditions for being an augmenting path, it is not a valid augmenting path since it is not simple. The reason for this is that the odd length cycle(g, d, h, g)causes the path to “fold” on itself—a problem that does not arise in the bipartite case. In fact, the matching does contain a valid augmenting path [e, f, c, d, h, g, b, a]. In fact, not all odd cycles cause this problem, but odd cycles that are as dense in matched edges as possible, i.e., it depends on the current matching. By “shrinking” blossoms to single nodes, we can get rid of them [26].

Subsequent work focused on efficient implementation of this method.

FIGURE 7.3 Difficulty in dealing with blossoms.

Edmonds (see [26]) gave the first polynomial-time algorithm for solving the maximum matching problem in general graphs. The current fastest algorithm for this problem is due to Micali and Vazirani [24]

and their algorithm runs inO(|E|

|V|)steps, which is the same bound obtained by the Hopcroft–Karp algorithm for finding a maximum matching in bipartite graphs.

Assignment Problem

We now introduce theassignment problem— that of finding a maximum weight matching in a given bipartite graph in which edges are given nonnegative weights. There is no loss of generality in assuming that the graph is a complete bipartite graph, since zero-weight edges may be added between pairs of vertices that are nonadjacent in the original graph without affecting the weight of a maximum-weight matching. The minimization version of the weighted version is the problem of finding aminimum-weight perfect matchingin a complete bipartite graph. Both versions of the weighted matching problem are equivalent and we sketch below how to reduce the minimum-weight perfect matching to maximum-weight matching. Choose a constantW that is larger than the weight of any edge, and assign each edge a new weight ofw(e) = Ww(e). Observe that maximum-weight matchings with the new weight function are minimum-weight perfect matchings with the original weights.

In this section, we restrict our attention to the study of the maximum-weight matching problem for bipartite graphs. Similar techniques have been used to solve the maximum-weight matching problem in arbitrary graphs (see [22, 26]).

The input is a complete bipartite graphG=(X, Y, X×Y )and each edgeehas a nonnegative weight ofw(e). The following algorithm is known as theHungarian method(see [1, 23, 26]). The method can be viewed as a primal-dual algorithm in the framework of linear programming [26]. No knowledge of linear programming is assumed here.

Afeasible vertex-labeling(is defined to be a mapping from the set of vertices inGto the real numbers such that for each edge(xi, yj)the following condition holds:

( (xi)+(

Theequality subgraph,G(, is defined to be the spanning subgraph ofGwhich includes all vertices ofG but only those edges(xi, yj)which have weights such that

( (xi)+( yj

=w xi, yj

.

The connection between equality subgraphs and maximum-weighted matchings is established by the following theorem.

THEOREM 7.3 If the equality subgraph,G(, has a perfect matching,M, thenMis a maximum weight matching inG.

LetMbe any perfect matching inG. Then

w(M)=

Hence,Mis a maximum weight perfect matching.

High-Level Description:The above theorem is the basis of the following algorithm for finding a maximum weight matching in a complete bipartite graph. The algorithm starts with a feasible labeling, then computes the equality subgraph and a maximum cardinality matching in this subgraph. If the matching found is perfect, by Theorem 7.3, the matching must be a maximum weight matching and the algorithm returns it as its output. Otherwise the matching is not perfect, and more edges need to be added to the equality subgraph byrevisingthe vertex labels. The revision should ensure that edges from the current matching do not leave the equality subgraph. After more edges are added to the equality subgraph, the algorithm grows the Hungarian trees further. Either the size of the matching increases because an augmenting path is found, or a new vertex is added to the Hungarian tree. In the former case, the current phase terminates and the algorithm starts a new phase since the matching size has increased. In the latter case, new nodes are added to the Hungarian tree. In|X|phases, the tree includes all the nodes, and therefore there are at most|X|phases before the size of the matching increases.

We now describe in more detail how the labels are updated and which edges are added to the equality subgraph. SupposeMis a maximum matching found by the algorithm. Hungarian trees are grown from all the free vertices inX. Vertices ofX(including the free vertices) that are encountered in the search are added to a setSand vertices ofYthat are encountered in the search are added to a setT. LetS=XS andT =YT.Figure 7.4illustrates the structure of the setsSandT. Matched edges are shown in bold;

the other edges are the edges inG(. Observe that there are no edges in the equality subgraph fromStoT, even though there may be edges fromTtoS. The algorithm now revises the labels as follows. Decrease all the labels of vertices inSby a quantityδ(to be determined later) and increase the labels of the vertices in Tbyδ. This ensures that edges in the matching continue to stay in the equality subgraph. Edges inG(not inG() that go from vertices inSto vertices inT are candidate edges to enter the equality subgraph, since one label is decreasing and the other is unchanged. The algorithm choosesδto be the smallest value such that some edge ofGG(enters the equality subgraph. Suppose this edge goes fromxStoyT. If yis free then an augmenting path has been found. On the other hand ifyis matched, the Hungarian tree is grown by movingytoT and its matched neighbor toSand the process of revising labels is continued.

FIGURE 7.4 SetsSandTas maintained by the algorithm.

Weighted Edge-Cover Problem

There are many applications of weighted matchings. One useful application is the problem of finding a minimum-weight edge coverof a graph. Given a graphG =(V, E)with weights on the edges, a set of edgesCEforms anedge coverofGif every vertex inV is incident to at least one edge inC. The weight of the coverCis the sum of the weights of its edges. The objective is to find an edge coverCof minimum weight. The problem can be reduced to the minimum-weight perfect matching problem as follows: create an identical copyG =(V, E)of graphG, except the weights of edges inEare all 0.

Add an edge from eachvV tovVwith weightwmin(v)=minx∈N(v)w(v, x). The final graphH is the union ofGandG, together with the edges connecting the two copies of each vertex;H contains 2|V|vertices and 2|E| + |V|edges. There exist other reductions from minimum-weight edge-cover to minimum-weight perfect matching; the reduction outlined above has the advantage that it creates a graph withO(|V|)vertices andO(|E|)edges. The minimum weight perfect matching problem may be solved using the techniques described earlier for the bipartite case, but the algorithm is more complex [22, 26].

THEOREM 7.4 The weight of a minimum-weight perfect matching inH is equal to the weight of a minimum-weight edge cover inG.

PROOF Consider a minimum-weight edge coverCinG. There is no loss of generality in assuming that a minimum-weight edge cover has no path of three edges, since the middle edge can be removed from the cover, thus, reducing its weight further. Hence, the edge coverCis a union of “stars” (trees of height 1).

For each star that has a vertex of degree more than one, we can match one of the leaf nodes to its copy in G, with weight at most the weight of the edge incident on the leaf vertex, thus, reducing the degree of the star. We repeat this until each vertex has degree at most one in the edge cover, i.e., it is a matching. In H, select this matching, once in each copy ofG. Observe that the cost of the matching within the second copyGis 0. Thus, givenC, we can find a perfect matching inH whose weight is no larger.

To argue the converse, we now show how to construct a coverCfrom a given perfect matchingMinH. For each edge(v, v)M, add toCa least weight edge incident tovinG. Also include inCany edge of

To argue the converse, we now show how to construct a coverCfrom a given perfect matchingMinH. For each edge(v, v)M, add toCa least weight edge incident tovinG. Also include inCany edge of

Dans le document ALGORITHMS and THEORY of COMPUTATION HANDBOOK (Page 161-169)