Ejemplo n.º 1
0
  private void relax(EdgeWeightDigraph G, int v) throws Exception {
    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 (pq.contains(w)) {
          pq.changeKey(w, distTo[w]);
        } else {
          pq.increaseKey(w, distTo[w]);
        }
      }
    }
  }
Ejemplo n.º 2
0
  public DijkstraSP(EdgeWeightDigraph G, int s) throws Exception {
    edgeTo = new DirectedEdge[G.V()];
    distTo = new double[G.V()];
    pq = new IndexMinPQ<Double>(G.V());

    for (int v = 0; v < G.V(); v++) {
      distTo[v] = Double.POSITIVE_INFINITY;
    }

    distTo[s] = 0.0;

    pq.insert(s, 0.0);

    while (!pq.isEmpty()) {
      relax(G, pq.delMin());
    }
  }