gt:             for (int w = 0; w < G.V(); w++) {
context: package com.ojcoleman.ahni.util.bellmanfordsp;
  
  import java.util.Stack;
  
  /**
   *  The <tt>BellmanFordSP</tt> class represents a data type for solving the
   *  single-source shortest paths problem in edge-weighted digraphs with
   *  no negative cycles. 
   *  The edge weights can be positive, negative, or zero.
   *  This class finds either a shortest path from the source vertex <em>s</em>
   *  to every other vertex or a negative cycle reachable from the source vertex.
   *  <p>
   *  This implementation uses the Bellman-Ford-Moore algorithm.
   *  The constructor takes time proportional to <em>V</em> (<em>V</em> + <em>E</em>)
   *  in the worst case, where <em>V</em> is the number of vertices and <em>E</em>
   *  is the number of edges.
   *  Afterwards, the <tt>distTo()</tt>, <tt>hasPathTo()</tt>, and <tt>hasNegativeCycle()</tt>
   *  methods take constant time; the <tt>pathTo()</tt> and <tt>negativeCycle()</tt>
   *  method takes time proportional to the number of edges returned.
   *  <p>
   *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/algs4/44sp">Section 4.4</a> of
   *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
   *
   *  @author Robert Sedgewick
   *  @author Kevin Wayne
   */
  public class BellmanFordSP {
      private double[] distTo;               // distTo[v] = distance  of shortest s->v path
      private DirectedEdge[] edgeTo;         // edgeTo[v] = last edge on shortest s->v path
      private boolean[] onQueue;             // onQueue[v] = is v currently on the queue?
      private Queue<Integer> queue;          // queue of vertices to relax
      private int cost;                      // number of calls to relax()
      private Iterable<DirectedEdge> cycle;  // negative cycle (or null if no such cycle)
  
      /**
       * Computes a shortest paths tree from <tt>s</tt> to every other vertex in
       * the edge-weighted digraph <tt>G</tt>.
       * @param G the acyclic digraph
       * @param s the source vertex
       * @throws IllegalArgumentException unless 0 &le; <tt>s</tt> &le; <tt>V</tt> - 1
       */
      public BellmanFordSP(EdgeWeightedDigraph G, int s) {
          distTo  = new double[G.V()];
          edgeTo  = new DirectedEdge[G.V()];
          onQueue = new boolean[G.V()];
          for (int v = 0; v < G.V(); v++)
              distTo[v] = Double.POSITIVE_INFINITY;
          distTo[s] = 0.0;
  
          // Bellman-Ford algorithm
          queue = new Queue<Integer>();
          queue.enqueue(s);
          onQueue[s] = true;
          while (!queue.isEmpty() && !hasNegativeCycle()) {
              int v = queue.dequeue();
              onQueue[v] = false;
              relax(G, v);
          }
  
          assert check(G, s);
      }
  
      // relax vertex v and put other endpoints on queue if changed
      private void relax(EdgeWeightedDigraph G, int v) {
          for (DirectedEdge e : G.adj(v)) {
              int w = e.to();
              if (distTo[w] > distTo[v] + e.weight()) {
                  distTo[w] = distTo[v] + e.weight();
                  edgeTo[w] = e;
                  if (!onQueue[w]) {
                      queue.enqueue(w);
                      onQueue[w] = true;
                  }
              }
              if (cost++ % G.V() == 0)
                  findNegativeCycle();
          }
      }
  
      /**
       * Is there a negative cycle reachable from the source vertex <tt>s</tt>?
       * @return <tt>true</tt> if there is a negative cycle reachable from the
       *    source vertex <tt>s</tt>, and <tt>false</tt> otherwise
       */
      public boolean hasNegativeCycle() {
          return cycle != null;
      }
  
      /**
       * Returns a negative cycle reachable from the source vertex <tt>s</tt>, or <tt>null</tt>
       * if there is no such cycle.
       * @return a negative cycle reachable from the soruce vertex <tt>s</tt> 
       *    as an iterable of edges, and <tt>null</tt> if there is no such cycle
       */
      public Iterable<DirectedEdge> negativeCycle() {
          return cycle;
      }
  
      // by finding a cycle in predecessor graph
      private void findNegativeCycle() {
          int V = edgeTo.length;
          EdgeWeightedDigraph spt = new EdgeWeightedDigraph(V);
          for (int v = 0; v < V; v++)
              if (edgeTo[v] != null)
                  spt.addEdge(edgeTo[v]);
  
          EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(spt);
          cycle = finder.cycle();
      }
  
      /**
       * Returns the length of a shortest path from the source vertex <tt>s</tt> to vertex <tt>v</tt>.
       * @param v the destination vertex
       * @return the length of a shortest path from the source vertex <tt>s</tt> to vertex <tt>v</tt>;
       *    <tt>Double.POSITIVE_INFINITY</tt> if no such path
       * @throws UnsupportedOperationException if there is a negative cost cycle reachable
       *    from the source vertex <tt>s</tt>
       */
      public double distTo(int v) {
          if (hasNegativeCycle())
              throw new UnsupportedOperationException("Negative cost cycle exists");
          return distTo[v];
      }
  
      /**
       * Is there a path from the source <tt>s</tt> to vertex <tt>v</tt>?
       * @param v the destination vertex
       * @return <tt>true</tt> if there is a path from the source vertex
       *    <tt>s</tt> to vertex <tt>v</tt>, and <tt>false</tt> otherwise
       */
      public boolean hasPathTo(int v) {
          return distTo[v] < Double.POSITIVE_INFINITY;
      }
  
      /**
       * Returns a shortest path from the source <tt>s</tt> to vertex <tt>v</tt>.
       * @param v the destination vertex
       * @return a shortest path from the source <tt>s</tt> to vertex <tt>v</tt>
       *    as an iterable of edges, and <tt>null</tt> if no such path
       * @throws UnsupportedOperationException if there is a negative cost cycle reachable
       *    from the source vertex <tt>s</tt>
       */
      public Iterable<DirectedEdge> pathTo(int v) {
          if (hasNegativeCycle())
              throw new UnsupportedOperationException("Negative cost cycle exists");
          if (!hasPathTo(v)) return null;
          Stack<DirectedEdge> path = new Stack<DirectedEdge>();
          for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) {
              path.push(e);
          }
          return path;
      }
  
      // check optimality conditions: either 
      // (i) there exists a negative cycle reacheable from s
      //     or 
      // (ii)  for all edges e = v->w:            distTo[w] <= distTo[v] + e.weight()
      // (ii') for all edges e = v->w on the SPT: distTo[w] == distTo[v] + e.weight()
      private boolean check(EdgeWeightedDigraph G, int s) {
  
          // has a negative cycle
          if (hasNegativeCycle()) {
              double weight = 0.0;
              for (DirectedEdge e : negativeCycle()) {
                  weight += e.weight();
              }
              if (weight >= 0.0) {
                  System.err.println("error: weight of negative cycle = " + weight);
                  return false;
              }
          }
  
          // no negative cycle reachable from source
          else {
  
              // check that distTo[v] and edgeTo[v] are consistent
              if (distTo[s] != 0.0 || edgeTo[s] != null) {
                  System.err.println("distanceTo[s] and edgeTo[s] inconsistent");
                  return false;
              }
              for (int v = 0; v < G.V(); v++) {
                  if (v == s) continue;
                  if (edgeTo[v] == null && distTo[v] != Double.POSITIVE_INFINITY) {
                      System.err.println("distTo[] and edgeTo[] inconsistent");
                      return false;
                  }
              }
  
              // check that all edges e = v->w satisfy distTo[w] <= distTo[v] + e.weight()
              for (int v = 0; v < G.V(); v++) {
                  for (DirectedEdge e : G.adj(v)) {
                      int w = e.to();
                      if (distTo[v] + e.weight() < distTo[w]) {
                          System.err.println("edge " + e + " not relaxed");
                          return false;
                      }
                  }
              }
  
              // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + e.weight()
  