PC^2 sucks.
[andmenj-acm.git] / Documentation / docs-sonyckson / Hungarian.cpp
blobc4919887be490b9a92f4ab2ed1658212550f4c96
1 Introduction
3 Are you familiar with the following situation? You open the Div I Medium and don't know how to approach it, while a lot of people in your room submitted it in less than 10 minutes. Then, after the contest, you find out in the editorial that this problem can be simply reduced to a classical one. If yes, then this tutorial will surely be useful for you.
4 Problem statement
6 In this article we'll deal with one optimization problem, which can be informally defined as:
8 Assume that we have N workers and N jobs that should be done. For each pair (worker, job) we know salary that should be paid to worker for him to perform the job. Our goal is to complete all jobs minimizing total inputs, while assigning each worker to exactly one job and vice versa.
10 Converting this problem to a formal mathematical definition we can form the following equations:
11 - cost matrix, where cij - cost of worker i to perform job j.
12 - resulting binary matrix, where xij = 1 if and only if ith worker is assigned to jth job.
13 - one worker to one job assignment.
14 - one job to one worker assignment.
15 - total cost function.
17 We can also rephrase this problem in terms of graph theory. Let's look at the job and workers as if they were a bipartite graph, where each edge between the ith worker and jth job has weight of cij. Then our task is to find minimum-weight matching in the graph (the matching will consists of N edges, because our bipartite graph is complete).
19 Small example just to make things clearer:
21 General description of the algorithm
23 This problem is known as the assignment problem. The assignment problem is a special case of the transportation problem, which in turn is a special case of the min-cost flow problem, so it can be solved using algorithms that solve the more general cases. Also, our problem is a special case of binary integer linear programming problem (which is NP-hard). But, due to the specifics of the problem, there are more efficient algorithms to solve it. We'll handle the assignment problem with the Hungarian algorithm (or Kuhn-Munkres algorithm). I'll illustrate two different implementations of this algorithm, both graph theoretic, one easy and fast to implement with O(n4) complexity, and the other one with O(n3) complexity, but harder to implement.
25 There are also implementations of Hungarian algorithm that do not use graph theory. Rather, they just operate with cost matrix, making different transformation of it (see [1] for clear explanation). We'll not touch these approaches, because it's less practical for TopCoder needs.
26 O(n4) algorithm explanation
28 As mentioned above, we are dealing with a bipartite graph. The main idea of the method is the following: consider we've found the perfect matching using only edges of weight 0 (hereinafter called "0-weight edges"). Obviously, these edges will be the solution of the assignment problem. If we can't find perfect matching on the current step, then the Hungarian algorithm changes weights of the available edges in such a way that the new 0-weight edges appear and these changes do not influence the optimal solution.
30 To clarify, let's look at the step-by-step overview:
32 Step 0)
34 A. For each vertex from left part (workers) find the minimal outgoing edge and subtract its weight from all weights connected with this vertex. This will introduce 0-weight edges (at least one).
36 B. Apply the same procedure for the vertices in the right part (jobs).
38 Actually, this step is not necessary, but it decreases the number of main cycle iterations.
40 Step 1)
42 A. Find the maximum matching using only 0-weight edges (for this purpose you can use max-flow algorithm, augmenting path algorithm, etc.).
44 B. If it is perfect, then the problem is solved. Otherwise find the minimum vertex cover V (for the subgraph with 0-weight edges only), the best way to do this is to use Köning's graph theorem.
46 Step 2) Let and adjust the weights using the following rule:
50 Step 3) Repeat Step 1 until solved.
52 But there is a nuance here; finding the maximum matching in step 1 on each iteration will cause the algorithm to become O(n5). In order to avoid this, on each step we can just modify the matching from the previous step, which only takes O(n2) operations.
54 It's easy to see that no more than n2 iterations will occur, because every time at least one edge becomes 0-weight. Therefore, the overall complexity is O(n4).
55 O(n3) algorithm explanation
57 Warning! In this section we will deal with the maximum-weighted matching problem. It's obviously easy to transform minimum problem to the maximum one, just by setting:
62 Before discussing the algorithm, let's take a look at some of the theoretical ideas. Let's start off by considering we have a complete bipartite graph G=(V,E) where and , w(x,y) - weight of edge (x,y).
64 Vertex and set neighborhood
66 Let . Then is v's neighborhood, or all vertices that share an edge with v.
68 Let . Then is S's neighborhood, or all vertices that share an edge with a vertex in S.
70 Vertex labeling
72 This is simply a function (for each vertex we assign some number called a label). Let's call this labeling feasible if it satisfies the following condition: . In other words, the sum of the labels of the vertices on both sides of a given edge are greater than or equal to the weight of that edge.
74 Equality subgraph
76 Let Gl=(V,El) be a spanning subgraph of G (in other words, it includes all vertices from G). If G only those edges (x,y) which satisfy the following condition: , then it is an equality subgraph. In other words, it only includes those edges from the bipartite matching which allow the vertices to be perfectly feasible.
78 Now we're ready for the theorem which provides the connection between equality subgraphs and maximum-weighted matching:
80 If M* is a perfect matching in the equality subgraph Gl, then M* is a maximum-weighted matching in G.
82 The proof is rather straightforward, but if you want you can do it for practice. Let's continue with a few final definitions:
84 Alternating path and alternating tree
86 Consider we have a matching M ().
88 Vertex is called matched if , otherwise it is called exposed (free, unmatched).
90 (In the diagram below, W1, W2, W3, J1, J3, J4 are matched, W4, J2 are exposed)
92 Path P is called alternating if its edges alternate between M and E\M. (For example, (W4, J4, W3, J3, W2, J2) and (W4, J1, W1) are alternating paths)
94 If the first and last vertices in alternating path are exposed, it is called augmenting (because we can increment the size of the matching by inverting edges along this path, therefore matching unmatched edges and vice versa). ((W4, J4, W3, J3, W2, J2) - augmenting alternating path)
96 A tree which has a root in some exposed vertex, and a property that every path starting in the root is alternating, is called an alternating tree. (Example on the picture above, with root in W4)
98 That's all for the theory, now let's look at the algorithm:
100 First let's have a look on the scheme of the Hungarian algorithm:
102 Step 0. Find some initial feasible vertex labeling and some initial matching.
104 Step 1. If M is perfect, then it's optimal, so problem is solved. Otherwise, some exposed exists; set . (x - is a root of the alternating tree we're going to build). Go to step 2.
106 Step 2. If go to step 3, else . Find
109 and replace existing labeling with the next one:
112 Now replace with
114 Step 3. Find some vertex . If y is exposed then an alternating path from x (root of the tree) to y exists, augment matching along this path and go to step 1. If y is matched in M with some vertex z add (z,y) to the alternating tree and set , go to step 2.
116 And now let's illustrate these steps by considering an example and writing some code.
118 As an example we'll use the previous one, but first let's transform it to the maximum-weighted matching problem, using the second method from the two described above. (See Picture 1)
121 Picture 1
123 Here are the global variables that will be used in the code:
125 #define N 55 //max number of vertices in one part
126 #define INF 100000000 //just infinity
128 int cost[N][N]; //cost matrix
129 int n, max_match; //n workers and n jobs
130 int lx[N], ly[N]; //labels of X and Y parts
131 int xy[N]; //xy[x] - vertex that is matched with x,
132 int yx[N]; //yx[y] - vertex that is matched with y
133 bool S[N], T[N]; //sets S and T in algorithm
134 int slack[N]; //as in the algorithm description
135 int slackx[N]; //slackx[y] such a vertex, that
136 // l(slackx[y]) + l(y) - w(slackx[y],y) = slack[y]
137 int prev[N]; //array for memorizing alternating paths
139 Step 0:
141 It's easy to see that next initial labeling will be feasible:
143 And as an initial matching we'll use an empty one. So we'll get equality subgraph as on Picture 2. The code for initializing is quite easy, but I'll paste it for completeness:
145 void init_labels()
147 memset(lx, 0, sizeof(lx));
148 memset(ly, 0, sizeof(ly));
149 for (int x = 0; x < n; x++)
150 for (int y = 0; y < n; y++)
151 lx[x] = max(lx[x], cost[x][y]);
154 The next three steps will be implemented in one function, which will correspond to a single iteration of the algorithm. When the algorithm halts, we will have a perfect matching, that's why we'll have n iterations of the algorithm and therefore (n+1) calls of the function.
156 Step 1
158 According to this step we need to check whether the matching is already perfect, if the answer is positive we just stop algorithm, otherwise we need to clear S, T and alternating tree and then find some exposed vertex from the X part. Also, in this step we are initializing a slack array, I'll describe it on the next step.
160 void augment() //main function of the algorithm
162 if (max_match == n) return; //check wether matching is already perfect
163 int x, y, root; //just counters and root vertex
164 int q[N], wr = 0, rd = 0; //q - queue for bfs, wr,rd - write and read
165 //pos in queue
166 memset(S, false, sizeof(S)); //init set S
167 memset(T, false, sizeof(T)); //init set T
168 memset(prev, -1, sizeof(prev)); //init set prev - for the alternating tree
169 for (x = 0; x < n; x++) //finding root of the tree
170 if (xy[x] == -1)
172 q[wr++] = root = x;
173 prev[x] = -2;
174 S[x] = true;
175 break;
178 for (y = 0; y < n; y++) //initializing slack array
180 slack[y] = lx[root] + ly[y] - cost[root][y];
181 slackx[y] = root;
184 Step 2
186 On this step, the alternating tree is completely built for the current labeling, but the augmenting path hasn't been found yet, so we need to improve the labeling. It will add new edges to the equality subgraph, giving an opportunity to expand the alternating tree. This is the main idea of the method; we are improving the labeling until we find an augmenting path in the equality graph corresponding to the current labeling. Let's turn back to step 2. There we just change labels using formulas (1) and (2), but using them in an obvious manner will cause the algorithm to have O(n4) time. So, in order to avoid this we use a slack array initialized in O(n) time because we only augment the array created in step 1:
188 Then we just need O(n) to calculate a delta Δ (see (1)):
190 Updating slack:
191 1) On step 3, when vertex x moves from X\S to S, this takes O(n).
192 2) On step 2, when updating labeling, it's also takes O(n), because:
194 So we get O(n) instead of O(n2) as in the straightforward approach.
195 Here's code for the label updating function:
197 void update_labels()
199 int x, y, delta = INF; //init delta as infinity
200 for (y = 0; y < n; y++) //calculate delta using slack
201 if (!T[y])
202 delta = min(delta, slack[y]);
203 for (x = 0; x < n; x++) //update X labels
204 if (S[x]) lx[x] -= delta;
205 for (y = 0; y < n; y++) //update Y labels
206 if (T[y]) ly[y] += delta;
207 for (y = 0; y < n; y++) //update slack array
208 if (!T[y])
209 slack[y] -= delta;
212 Step 3
214 In step 3, first we build an alternating tree starting from some exposed vertex, chosen at the beginning of each iteration. We will do this using breadth-first search algorithm. If on some step we meet an exposed vertex from the Y part, then finally we can augment our path, finishing up with a call to the main function of the algorithm. So the code will be the following:
216 1) Here's the function that adds new edges to the alternating tree:
218 void add_to_tree(int x, int prevx)
219 //x - current vertex,prevx - vertex from X before x in the alternating path,
220 //so we add edges (prevx, xy[x]), (xy[x], x)
222 S[x] = true; //add x to S
223 prev[x] = prevx; //we need this when augmenting
224 for (int y = 0; y < n; y++) //update slacks, because we add new vertex to S
225 if (lx[x] + ly[y] - cost[x][y] < slack[y])
227 slack[y] = lx[x] + ly[y] - cost[x][y];
228 slackx[y] = x;
232 3) And now, the end of the augment() function:
234 //second part of augment() function
235 while (true) //main cycle
237 while (rd < wr) //building tree with bfs cycle
239 x = q[rd++]; //current vertex from X part
240 for (y = 0; y < n; y++) //iterate through all edges in equality graph
241 if (cost[x][y] == lx[x] + ly[y] && !T[y])
243 if (yx[y] == -1) break; //an exposed vertex in Y found, so
244 //augmenting path exists!
245 T[y] = true; //else just add y to T,
246 q[wr++] = yx[y]; //add vertex yx[y], which is matched
247 //with y, to the queue
248 add_to_tree(yx[y], x); //add edges (x,y) and (y,yx[y]) to the tree
250 if (y < n) break; //augmenting path found!
252 if (y < n) break; //augmenting path found!
254 update_labels(); //augmenting path not found, so improve labeling
255 wr = rd = 0;
256 for (y = 0; y < n; y++)
257 //in this cycle we add edges that were added to the equality graph as a
258 //result of improving the labeling, we add edge (slackx[y], y) to the tree if
259 //and only if !T[y] && slack[y] == 0, also with this edge we add another one
260 //(y, yx[y]) or augment the matching, if y was exposed
261 if (!T[y] && slack[y] == 0)
263 if (yx[y] == -1) //exposed vertex in Y found - augmenting path exists!
265 x = slackx[y];
266 break;
268 else
270 T[y] = true; //else just add y to T,
271 if (!S[yx[y]])
273 q[wr++] = yx[y]; //add vertex yx[y], which is matched with
274 //y, to the queue
275 add_to_tree(yx[y], slackx[y]); //and add edges (x,y) and (y,
276 //yx[y]) to the tree
280 if (y < n) break; //augmenting path found!
283 if (y < n) //we found augmenting path!
285 max_match++; //increment matching
286 //in this cycle we inverse edges along augmenting path
287 for (int cx = x, cy = y, ty; cx != -2; cx = prev[cx], cy = ty)
289 ty = xy[cx];
290 yx[cy] = cx;
291 xy[cx] = cy;
293 augment(); //recall function, go to step 1 of the algorithm
295 }//end of augment() function
297 The only thing in code that hasn't been explained yet is the procedure that goes after labels are updated. Say we've updated labels and now we need to complete our alternating tree; to do this and to keep algorithm in O(n3) time (it's only possible if we use each edge no more than one time per iteration) we need to know what edges should be added without iterating through all of them, and the answer for this question is to use BFS to add edges only from those vertices in Y, that are not in T and for which slack[y] = 0 (it's easy to prove that in such way we'll add all edges and keep algorithm to be O(n3)). See picture below for explanation:
299 At last, here's the function that implements Hungarian algorithm:
301 int hungarian()
303 int ret = 0; //weight of the optimal matching
304 max_match = 0; //number of vertices in current matching
305 memset(xy, -1, sizeof(xy));
306 memset(yx, -1, sizeof(yx));
307 init_labels(); //step 0
308 augment(); //steps 1-3
309 for (int x = 0; x < n; x++) //forming answer there
310 ret += cost[x][xy[x]];
311 return ret;
314 To see all this in practice let's complete the example started on step 0.
315 Build
316 alternating tree Augmenting
317 path found Build
318 alternating tree
319 → → →
320 Update labels
321 (Δ=1) Build
322 alternating tree Update labels
323 (Δ=1)
324 → → →
325 Build
326 alternating tree Augmenting
327 path found Build
328 alternating tree
329 → → →
330 Update labels
331 (Δ=2) Build
332 alternating tree Update labels
333 (Δ=1)
334 → → →
335 Build
336 alternating tree Augmenting
337 path found Optimal matching found
338 → →
340 Finally, let's talk about the complexity of this algorithm. On each iteration we increment matching so we have n iterations. On each iterations each edge of the graph is used no more than one time when finding augmenting path, so we've got O(n2) operations. Concerning labeling we update slack array each time when we insert vertex from X into S, so this happens no more than n times per iteration, updating slack takes O(n) operations, so again we've got O(n2). Updating labels happens no more than n time per iterations (because we add at least one vertex from Y to T per iteration), it takes O(n) operations - again O(n2). So total complexity of this implementation is O(n3).
341 Some practice
343 For practice let's consider the medium problem from SRM 371 (div. 1). It's obvious we need to find the maximum-weighted matching in graph, where the X part is our players, the Y part is the opposing club players, and the weight of each edge is:
345 Though this problem has a much simpler solution, this one is obvious and fast coding can bring more points.
347 Try this one for more practice. I hope this article has increased the wealth of your knowledge in classical algorithms… Good luck and have fun!