-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhungarian.h
308 lines (283 loc) · 9.46 KB
/
hungarian.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Written in 2015 by Shaunak Kishore ([email protected]).
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// Implementation of the Hungarian algorithm for finding minimum-weight
// matchings. Usage:
// - Compute Cost** cost_matrix, an nxn array where cost_matrix[x][y]
// is the cost of matching x with y.
// - Construct Hungarian(n, cost_matrix).
// - Call Solve() on the Hungarian instance and verify that its status is OK.
// - Use GetTotalCost, GetXMatch, and GetYMatch to read the output.
//
// The algorithm will minimize the total cost of the matching by default.
// If you pass maximize=true in the constructor, it will maximize the total
// cost instead.
//
// Costs in the cost matrix may be arbitrary integers. The first step of the
// algorithm is to reduce the matrix so that all costs are non-negative, but
// but this is handled entirely within the solver.
//
// We restrict to integer costs because the algorithm is not numerically stable.
// When all inputs are integers, the intermediate edge weights we compute are
// also guaranteed to be integers.
#ifndef __HUNGARIAN__
#define __HUNGARIAN__
typedef int Cost;
namespace {
inline Cost min(Cost a, Cost b) {
return (a < b ? a : b);
}
} // namespace
class Hungarian {
public:
// cost_matrix must be an nxn matrix of Cost values. However, templating this
// contstructor allows it to take both Cost** and vector<vector<Cost>>,
// without actually including the std::vector library.
template <typename T>
Hungarian(int n_, const T& cost_matrix_, bool maximize=false)
: n(n_), cost_matrix(new Cost[n*n]), x_match(new int[n]),
y_match(new int[n]), x_label(new Cost[n]), y_label(new Cost[n]) {
const int sign = (maximize ? -1 : 1);
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
cost_matrix[n*x+y] = sign*cost_matrix_[x][y];
}
}
}
enum Status {
OK = 0,
ERROR_INTEGER_OVERFLOW = 1,
ERROR_AUGMENTATION_STEP_FAILED = 2,
ERROR_NON_TIGHT_EDGE_MATCHED = 3
};
Status Solve() {
for (int i = 0; i < n; i++) {
// Initially, all vertices are unmatched.
x_match[i] = -1;
y_match[i] = -1;
}
matched = 0;
// We first reduce the matrix so that all entries are non-negative.
// This step may fail. In particular, it will fail if there are two entries
// in the cost matrix that are more than INT_MAX apart.
if (!ReduceCostMatrix()) {
return ERROR_INTEGER_OVERFLOW;
}
// Greedily match pairs vertices that are connected by a tight edge.
// This step will help the algorithm terminate in fewer than n augmentation
// steps on easier inputs.
FindGreedySolution();
// Run augmentation steps to finish matching the vertices.
while (matched < n) {
const int last_matched = matched;
RunAugmentationStep();
if (matched <= last_matched) {
return ERROR_AUGMENTATION_STEP_FAILED;
}
for (int x = 0; x < n; x++) {
if (x_match[x] != -1 && GetSlack(x, x_match[x]) != 0) {
return ERROR_NON_TIGHT_EDGE_MATCHED;
}
}
}
return OK;
}
~Hungarian() {
delete[] cost_matrix;
delete[] x_match;
delete[] y_match;
delete[] x_label;
delete[] y_label;
}
// Given the original matrix again, return the total cost of the matching.
Cost GetTotalCost(const Cost** cost_matrix) const {
Cost score = 0;
for (int x = 0; x < n; x++) {
score += cost_matrix[x][x_match[x]];
}
return score;
}
// Return the y-coordinate that was matched with the given x-coordinate.
// Note that x-coordinates correspond to the first coordinate of the matrix,
// so the total cost is given by
// sum(x = 0,1,...n) cost_matrix[x][GetXMatch(x)]
int GetXMatch(int x) {
return x_match[x];
}
// Return the x-coordinate that was matched with the given y-coordinate.
int GetYMatch(int y) {
return y_match[y];
}
private:
// The input matrix and its size. We store the matrix in a 1D array and use
// cost_matrix[n*x+y] to access the (x, y) entry of the matrix.
int n;
Cost* cost_matrix;
// x_match[x] is the y that is currently matched with the given x.
// If x is unmatched, then x_match[x] will equal -1. y_match is similar.
int* x_match;
int* y_match;
// x_label[x] and y_label[y] are dual variables that satisfy the condition:
// cost_matrix[n*x+y] >= x_label[x] + y_label[y]
// We define the slack of the edge (x, y) to be:
// slack(x, y) = cost_matrix[n*x+y] - x_label[x] - y_label[y]
// An edge is tight if its slack is zero. Throughout this algorithm, we will
// maintain the invariant that all matched edges are tight.
Cost* x_label;
Cost* y_label;
// The number of edges currently matched.
int matched;
Cost GetSlack(int x, int y) const {
return cost_matrix[n*x+y] - x_label[x] - y_label[y];
}
void Match(int x, int y) {
x_match[x] = y;
y_match[y] = x;
}
bool ReduceCostMatrix() {
// Subtract the minimum value in each column from all entries in that column.
// After this operation, all entries in the matrix will be non-negative, so
// x-labels of 0 will satisfy the slack inequality.
//
// Returns false if a value in the matrix is still negative after reduction,
// which can only occur due to integer underflow.
for (int x = 0; x < n; x++) {
Cost min_cost = 0;
for (int y = 0; y < n; y++) {
min_cost = min(min_cost, cost_matrix[n*x+y]);
}
for (int y = 0; y < n; y++) {
cost_matrix[n*x+y] -= min_cost;
}
x_label[x] = 0;
}
// Do the same for y.
for (int y = 0; y < n; y++) {
Cost min_cost = 0;
for (int x = 0; x < n; x++) {
min_cost = min(min_cost, cost_matrix[n*x+y]);
}
for (int x = 0; x < n; x++) {
cost_matrix[n*x+y] -= min_cost;
if (cost_matrix[n*x+y] < 0) {
return false;
}
}
y_label[y] = 0;
}
return true;
}
void FindGreedySolution() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (x_match[x] == -1 && y_match[y] == -1 && GetSlack(x, y) == 0) {
Match(x, y);
matched += 1;
}
}
}
}
void RunAugmentationStep() {
// An augmentation step consists of searching for a path in the graph of
// tight edges on which every other edge is included in the matching, but
// in which the first and last edges are free. Once we find such a path,
// we can increase the size of our matching by 1 by flipping the matches
// along it.
//
// If we cannot find an augmenting path, this function will change the
// labels for vertices on slack edges along the path and try again.
bool* x_in_tree = new bool[n];
int* y_parent = new int[n];
for (int x = 0; x < n; x++) {
x_in_tree[x] = false;
y_parent[x] = -1;
}
int root = FindUnmatchedXValue();
if (root == -1) {
// All x-values are matched. Return early. This should not occur normally;
// RunAugmentationStep should only be called if there are unmatched nodes.
return;
}
// slack[y] will be the minimum currently-known slack between y and any
// node on the left, and slack_x[y] will be the node that minimizes it.
Cost* slack = new Cost[n];
int* slack_x = new int[n];
for (int y = 0; y < n; y++) {
slack[y] = GetSlack(root, y);
slack_x[y] = root;
}
x_in_tree[root] = true;
while (true) {
Cost delta = -1;
int delta_x, delta_y;
for (int y = 0; y < n; y++) {
if (y_parent[y] < 0 && (delta == -1 || slack[y] < delta)) {
delta = slack[y];
delta_x = slack_x[y];
delta_y = y;
}
}
UpdateLabels(delta, x_in_tree, y_parent, slack);
y_parent[delta_y] = delta_x;
if (y_match[delta_y] == -1) {
// Augmenting path found, ending with the edge (delta_x, delta_y).
// We flip all edges along the path.
int cur_y = delta_y;
while (cur_y != -1) {
int cur_x = y_parent[cur_y];
int next_y = x_match[cur_x];
Match(cur_x, cur_y);
cur_y = next_y;
}
matched += 1;
break;
}
// We've added a new node to the BFS tree through only tight edges.
// We need to adjust slack values as a result.
int x = y_match[delta_y];
x_in_tree[x] = true;
for (int y = 0; y < n; y++) {
if (y_parent[y] == -1) {
Cost new_slack = GetSlack(x, y);
if (slack[y] > new_slack) {
slack[y] = new_slack;
slack_x[y] = x;
}
}
}
}
delete[] x_in_tree;
delete[] y_parent;
delete[] slack;
delete[] slack_x;
}
int FindUnmatchedXValue() const {
for (int x = 0; x < n; x++) {
if (x_match[x] == -1) {
return x;
}
}
// We should NEVER reach this point while running the algorithm.
// If all x-values are matched, we should not be in an augmentation step.
//assert(false);
return -1;
}
void UpdateLabels(Cost delta, bool* x_in_tree, int* y_parent, Cost* slack) {
for (int x = 0; x < n; x++) {
if (x_in_tree[x]) {
x_label[x] += delta;
}
}
for (int y = 0; y < n; y++) {
if (y_parent[y] == -1) {
slack[y] -= delta;
} else {
y_label[y] -= delta;
}
}
}
};
#endif // __HUNGARIAN__