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
package algs44;
import algs13.Stack;
import  stdlib.*;

/* ***********************************************************************
 *  Compilation:  javac FloydWarshall.java
 *  Execution:    java FloydWarshall V E
 *  Dependencies: AdjMatrixEdgeWeightedDigraph.java
 *
 *  Floyd-Warshall all-pairs shortest path algorithm.
 *
 *  % java FloydWarshall 100 500
 *
 *  Should check for negative cycles during triple loop; otherwise
 *  intermediate numbers can get exponentially large.
 *  Reference: "The Floyd-Warshall algorithm on graphs with negative cycles"
 *  by Stefan Hougardy
 *
 *************************************************************************/

public class FloydWarshall {
  private double[][] distTo;        // distTo[v][w] = length of    shortest v->w path
  private DirectedEdge[][] edgeTo;  // edgeTo[v][w] = last edge on shortest v->w path

  public FloydWarshall(AdjMatrixEdgeWeightedDigraph G) {
    int V = G.V();
    distTo = new double[V][V];
    edgeTo = new DirectedEdge[V][V];

    // initialize distances to infinity
    for (int v = 0; v < V; v++) {
      for (int w = 0; w < V; w++) {
        distTo[v][w] = Double.POSITIVE_INFINITY;
      }
    }

    // initialize distances using edge-weighted digraph's
    for (int v = 0; v < G.V(); v++) {
      for (DirectedEdge e : G.adj(v)) {
        distTo[e.from()][e.to()] = e.weight();
        edgeTo[e.from()][e.to()] = e;
      }
      // in case of self-loops
      if (distTo[v][v] >= 0.0) {
        distTo[v][v] = 0.0;
        edgeTo[v][v] = null;
      }
    }

    // Floyd-Warshall updates
    for (int i = 0; i < V; i++) {
      // compute shortest paths using only 0, 1, ..., i as intermediate vertices
      for (int v = 0; v < V; v++) {
        if (edgeTo[v][i] == null) continue;    // optimization
        for (int w = 0; w < V; w++) {
          if (distTo[v][w] > distTo[v][i] + distTo[i][w]) {
            distTo[v][w] = distTo[v][i] + distTo[i][w];
            edgeTo[v][w] = edgeTo[i][w];
          }
        }
        if (distTo[v][v] < 0.0) return;  // negative cycle
      }
    }
  }

  // is there a negative cycle?
  public boolean hasNegativeCycle() {
    for (int v = 0; v < distTo.length; v++)
      if (distTo[v][v] < 0.0) return true;
    return false;
  }

  // negative cycle
  public Iterable<DirectedEdge> negativeCycle() {
    for (int v = 0; v < distTo.length; v++) {
      // negative cycle in v's predecessor graph
      if (distTo[v][v] < 0.0) {
        int V = edgeTo.length;
        EdgeWeightedDigraph spt = new EdgeWeightedDigraph(V);
        for (int w = 0; w < V; w++)
          if (edgeTo[v][w] != null)
            spt.addEdge(edgeTo[v][w]);
        EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(spt);
        assert finder.hasCycle();
        return finder.cycle();
      }
    }
    return null;
  }

  // is there a path from v to w?
  public boolean hasPath(int v, int w) {
    return distTo[v][w] < Double.POSITIVE_INFINITY;
  }


  // return length of shortest path from v to w
  public double dist(int v, int w) {
    return distTo[v][w];
  }

  // return view of shortest path from v to w, null if no such path
  public Iterable<DirectedEdge> path(int v, int w) {
    if (!hasPath(v, w) || hasNegativeCycle()) return null;
    Stack<DirectedEdge> path = new Stack<>();
    for (DirectedEdge e = edgeTo[v][w]; e != null; e = edgeTo[v][e.from()]) {
      path.push(e);
    }
    return path;
  }

  // check optimality conditions
  private boolean check(EdgeWeightedDigraph G, int s) {

    // no negative cycle
    if (!hasNegativeCycle()) {
      for (int v = 0; v < G.V(); v++) {
        for (DirectedEdge e : G.adj(v)) {
          int w = e.to();
          for (int i = 0; i < G.V(); i++) {
            if (distTo[i][w] > distTo[i][v] + e.weight()) {
              System.err.println("edge " + e + " is eligible");
              return false;
            }
          }
        }
      }
    }
    return true;
  }



  public static void main(String[] args) {

    // random graph with V vertices and E edges, parallel edges allowed
    int V = Integer.parseInt(args[0]);
    int E = Integer.parseInt(args[1]);
    AdjMatrixEdgeWeightedDigraph G = new AdjMatrixEdgeWeightedDigraph(V);
    for (int i = 0; i < E; i++) {
      int v = (int) (V * Math.random());
      int w = (int) (V * Math.random());
      double weight = Math.round(100 * (Math.random() - 0.15)) / 100.0;
      if (v == w) G.addEdge(new DirectedEdge(v, w, Math.abs(weight)));
      else        G.addEdge(new DirectedEdge(v, w, weight));
    }

    StdOut.println(G);

    // run Floyd-Warshall algorithm
    FloydWarshall spt = new FloydWarshall(G);

    // print all-pairs shortest path distances
    StdOut.format("     ");
    for (int v = 0; v < G.V(); v++) {
      StdOut.format("%6d ", v);
    }
    StdOut.println();
    for (int v = 0; v < G.V(); v++) {
      StdOut.format("%3d: ", v);
      for (int w = 0; w < G.V(); w++) {
        if (spt.hasPath(v, w)) StdOut.format("%6.2f ", spt.dist(v, w));
        else                   StdOut.format("   Inf ");
      }
      StdOut.println();
    }

    // print negative cycle
    if (spt.hasNegativeCycle()) {
      StdOut.println("Negative cost cycle:");
      for (DirectedEdge e : spt.negativeCycle())
        StdOut.println(e);
      StdOut.println();
    }

    // print all-pairs shortest paths
    else {
      for (int v = 0; v < G.V(); v++) {
        for (int w = 0; w < G.V(); w++) {
          if (spt.hasPath(v, w)) {
            StdOut.format("%d to %d (%5.2f)  ", v, w, spt.dist(v, w));
            for (DirectedEdge e : spt.path(v, w))
              StdOut.print(e + "  ");
            StdOut.println();
          }
          else {
            StdOut.format("%d to %d          no path\n", v, w);
          }
        }
      }
    }

  }

}