AssignmentProblem.java
10.9 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/******************************************************************************
* Compilation: javac AssignmentProblem.java
* Execution: java AssignmentProblem n
* Dependencies: DijkstraSP.java DirectedEdge.java
*
* Solve an n-by-n assignment problem in n^3 log n time using the
* successive shortest path algorithm.
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code AssignmentProblem} class represents a data type for computing
* an optimal solution to an <em>n</em>-by-<em>n</em> <em>assignment problem</em>.
* The assignment problem is to find a minimum weight matching in an
* edge-weighted complete bipartite graph.
* <p>
* The data type supplies methods for determining the optimal solution
* and the corresponding dual solution.
* <p>
* This implementation uses the <em>successive shortest paths algorithm</em>.
* The order of growth of the running time in the worst case is
* O(<em>n</em>^3 log <em>n</em>) to solve an <em>n</em>-by-<em>n</em>
* instance.
* <p>
* For additional documentation, see
* <a href="http://algs4.cs.princeton.edu/65reductions">Section 6.5</a>
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class AssignmentProblem {
private static final double FLOATING_POINT_EPSILON = 1E-14;
private static final int UNMATCHED = -1;
private int n; // number of rows and columns
private double[][] weight; // the n-by-n cost matrix
private double minWeight; // minimum value of any weight
private double[] px; // px[i] = dual variable for row i
private double[] py; // py[j] = dual variable for col j
private int[] xy; // xy[i] = j means i-j is a match
private int[] yx; // yx[j] = i means i-j is a match
/**
* Determines an optimal solution to the assignment problem.
*
* @param weight the <em>n</em>-by-<em>n</em> matrix of weights
* @throws IllegalArgumentException unless all weights are nonnegative
* @throws NullPointerException if {@code weight} is {@code null}
*/
public AssignmentProblem(double[][] weight) {
n = weight.length;
this.weight = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Double.isNaN(weight[i][j]))
throw new IllegalArgumentException("weight " + i + "-" + j + " is NaN");
if (weight[i][j] < minWeight) minWeight = weight[i][j];
this.weight[i][j] = weight[i][j];
}
}
// dual variables
px = new double[n];
py = new double[n];
// initial matching is empty
xy = new int[n];
yx = new int[n];
for (int i = 0; i < n; i++)
xy[i] = UNMATCHED;
for (int j = 0; j < n; j++)
yx[j] = UNMATCHED;
// add n edges to matching
for (int k = 0; k < n; k++) {
assert isDualFeasible();
assert isComplementarySlack();
augment();
}
assert certifySolution();
}
// find shortest augmenting path and upate
private void augment() {
// build residual graph
EdgeWeightedDigraph G = new EdgeWeightedDigraph(2*n+2);
int s = 2*n, t = 2*n+1;
for (int i = 0; i < n; i++) {
if (xy[i] == UNMATCHED)
G.addEdge(new DirectedEdge(s, i, 0.0));
}
for (int j = 0; j < n; j++) {
if (yx[j] == UNMATCHED)
G.addEdge(new DirectedEdge(n+j, t, py[j]));
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (xy[i] == j) G.addEdge(new DirectedEdge(n+j, i, 0.0));
else G.addEdge(new DirectedEdge(i, n+j, reducedCost(i, j)));
}
}
// compute shortest path from s to every other vertex
DijkstraSP spt = new DijkstraSP(G, s);
// augment along alternating path
for (DirectedEdge e : spt.pathTo(t)) {
int i = e.from(), j = e.to() - n;
if (i < n) {
xy[i] = j;
yx[j] = i;
}
}
// update dual variables
for (int i = 0; i < n; i++)
px[i] += spt.distTo(i);
for (int j = 0; j < n; j++)
py[j] += spt.distTo(n+j);
}
// reduced cost of i-j
// (subtracting off minWeight reweights all weights to be non-negative)
private double reducedCost(int i, int j) {
double reducedCost = (weight[i][j] - minWeight) + px[i] - py[j];
// to avoid issues with floating-point precision
double magnitude = Math.abs(weight[i][j]) + Math.abs(px[i]) + Math.abs(py[j]);
if (Math.abs(reducedCost) <= FLOATING_POINT_EPSILON * magnitude) return 0.0;
assert reducedCost >= 0.0;
return reducedCost;
}
/**
* Returns the dual optimal value for the specified row.
*
* @param i the row index
* @return the dual optimal value for row {@code i}
* @throws IndexOutOfBoundsException unless {@code 0 <= i < n}
*
*/
// dual variable for row i
public double dualRow(int i) {
validate(i);
return px[i];
}
/**
* Returns the dual optimal value for the specified column.
*
* @param j the column index
* @return the dual optimal value for column {@code j}
* @throws IndexOutOfBoundsException unless {@code 0 <= j < n}
*
*/
public double dualCol(int j) {
validate(j);
return py[j];
}
/**
* Returns the column associated with the specified row in the optimal solution.
*
* @param i the row index
* @return the column matched to row {@code i} in the optimal solution
* @throws IndexOutOfBoundsException unless {@code 0 <= i < n}
*
*/
public int sol(int i) {
validate(i);
return xy[i];
}
/**
* Returns the total weight of the optimal solution
*
* @return the total weight of the optimal solution
*
*/
public double weight() {
double total = 0.0;
for (int i = 0; i < n; i++) {
if (xy[i] != UNMATCHED)
total += weight[i][xy[i]];
}
return total;
}
private void validate(int i) {
if (i < 0 || i >= n) throw new IndexOutOfBoundsException();
}
/**************************************************************************
*
* The code below is solely for testing correctness of the data type.
*
**************************************************************************/
// check that dual variables are feasible
private boolean isDualFeasible() {
// check that all edges have >= 0 reduced cost
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (reducedCost(i, j) < 0) {
StdOut.println("Dual variables are not feasible");
return false;
}
}
}
return true;
}
// check that primal and dual variables are complementary slack
private boolean isComplementarySlack() {
// check that all matched edges have 0-reduced cost
for (int i = 0; i < n; i++) {
if ((xy[i] != UNMATCHED) && (reducedCost(i, xy[i]) != 0)) {
StdOut.println("Primal and dual variables are not complementary slack");
return false;
}
}
return true;
}
// check that primal variables are a perfect matching
private boolean isPerfectMatching() {
// check that xy[] is a perfect matching
boolean[] perm = new boolean[n];
for (int i = 0; i < n; i++) {
if (perm[xy[i]]) {
StdOut.println("Not a perfect matching");
return false;
}
perm[xy[i]] = true;
}
// check that xy[] and yx[] are inverses
for (int j = 0; j < n; j++) {
if (xy[yx[j]] != j) {
StdOut.println("xy[] and yx[] are not inverses");
return false;
}
}
for (int i = 0; i < n; i++) {
if (yx[xy[i]] != i) {
StdOut.println("xy[] and yx[] are not inverses");
return false;
}
}
return true;
}
// check optimality conditions
private boolean certifySolution() {
return isPerfectMatching() && isDualFeasible() && isComplementarySlack();
}
/**
* Unit tests the {@code AssignmentProblem} data type.
* Takes a command-line argument n; creates a random n-by-n matrix;
* solves the n-by-n assignment problem; and prints the optimal
* solution.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// create random n-by-n matrix
int n = Integer.parseInt(args[0]);
double[][] weight = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
weight[i][j] = StdRandom.uniform(900) + 100; // 3 digits
}
}
// solve assignment problem
AssignmentProblem assignment = new AssignmentProblem(weight);
StdOut.printf("weight = %.0f\n", assignment.weight());
StdOut.println();
// print n-by-n matrix and optimal solution
if (n >= 20) return;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == assignment.sol(i))
StdOut.printf("*%.0f ", weight[i][j]);
else
StdOut.printf(" %.0f ", weight[i][j]);
}
StdOut.println();
}
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/