001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
package algs44;
import  stdlib.*;

/* ***********************************************************************
 *  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.
 *
 *  Remark: could use dense version of Dijsktra's algorithm for
 *  improved theoretical efficiency of N^3, but it doesn't seem to
 *  help in practice.
 *
 *  Assumes N-by-N cost matrix is nonnegative.
 *
 *
 *********************************************************************/

public class AssignmentProblem {
  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[] 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


  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++)
        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 check();
  }

  // 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, reduced(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
  private double reduced(int i, int j) {
    return weight[i][j] + px[i] - py[j];
  }

  // dual variable for row i
  public double dualRow(int i) {
    return px[i];
  }

  // dual variable for column j
  public double dualCol(int j) {
    return py[j];
  }

  // total weight of min weight perfect matching
  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;
  }

  public int sol(int i) {
    return xy[i];
  }

  // 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 (reduced(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) && (reduced(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 check() {
    return isPerfectMatching() && isDualFeasible() && isComplementarySlack();
  }

  public static void main(String[] args) {
    In in = new In(args[0]);
    int N = in.readInt();
    double[][] weight = new double[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        weight[i][j] = in.readDouble();
      }
    }

    AssignmentProblem assignment = new AssignmentProblem(weight);
    StdOut.println("weight = " + assignment.weight());
    for (int i = 0; i < N; i++)
      StdOut.println(i + "-" + assignment.sol(i) + "' " + weight[i][assignment.sol(i)]);

    for (int i = 0; i < N; i++)
      StdOut.println("px[" + i + "] = " + assignment.dualRow(i));
    for (int j = 0; j < N; j++)
      StdOut.println("py[" + j + "] = " + assignment.dualCol(j));
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        StdOut.println("reduced[" + i + "-" + j + "] = " + assignment.reduced(i, j));

  }

}