/** Calculates the matrix of all shortest paths, but does not populate the paths map. */
  private void lazyCalculateMatrix() {
    if (d != null) {
      // already done
      return;
    }

    int n = vertices.size();

    // init the backtrace matrix
    backtrace = new int[n][n];
    for (int i = 0; i < n; i++) {
      Arrays.fill(backtrace[i], -1);
    }

    // initialize matrix, 0
    d = new double[n][n];
    for (int i = 0; i < n; i++) {
      Arrays.fill(d[i], Double.POSITIVE_INFINITY);
    }

    // initialize matrix, 1
    for (int i = 0; i < n; i++) {
      d[i][i] = 0.0;
    }

    // initialize matrix, 2
    Set<E> edges = graph.edgeSet();
    for (E edge : edges) {
      V v1 = graph.getEdgeSource(edge);
      V v2 = graph.getEdgeTarget(edge);

      int v_1 = vertices.indexOf(v1);
      int v_2 = vertices.indexOf(v2);

      d[v_1][v_2] = graph.getEdgeWeight(edge);
      if (!(graph instanceof DirectedGraph<?, ?>)) {
        d[v_2][v_1] = graph.getEdgeWeight(edge);
      }
    }

    // run fw alg
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          double ik_kj = d[i][k] + d[k][j];
          if (ik_kj < d[i][j]) {
            d[i][j] = ik_kj;
            backtrace[i][j] = k;
          }
        }
      }
    }
  }
示例#2
0
  /**
   * Constructs the shortest path array for the given graph.
   *
   * @param g input graph
   * @param <V> a V object.
   * @param <E> a E object.
   */
  public FloydWarshall(Graph<V, E> g) {
    int sz = g.vertexSet().size();
    d = new double[sz][sz];
    indices = new HashMap<V, Integer>();

    // Initialise distance to infinity, or the neighbours weight, or 0 if
    // same
    for (V v1 : g.vertexSet()) {
      for (V v2 : g.vertexSet()) {
        if (v1 == v2) {
          d[index(v1)][index(v2)] = 0;
        } else {
          E e = g.getEdge(v1, v2);

          if (e == null) {
            d[index(v1)][index(v2)] = Double.POSITIVE_INFINITY;
          } else {
            d[index(v1)][index(v2)] = g.getEdgeWeight(e);
          }
        }
      }
    }

    // now iterate k times
    for (int k = 0; k < sz; k++) {
      for (V v1 : g.vertexSet()) {
        for (V v2 : g.vertexSet()) {
          d[index(v1)][index(v2)] =
              Math.min(d[index(v1)][index(v2)], d[index(v1)][k] + d[k][index(v2)]);
          if (Double.POSITIVE_INFINITY != d[index(v1)][index(v2)])
            diameter = Math.max(diameter, d[index(v1)][index(v2)]);
        }
      }
    }
  }
  private GraphPath<V, E> getShortestPathImpl(V a, V b) {
    int v_a = vertices.indexOf(a);
    int v_b = vertices.indexOf(b);

    List<E> edges = new ArrayList<E>();
    shortestPathRecur(edges, v_a, v_b);

    // no path, return null
    if (edges.size() < 1) {
      return null;
    }

    double weight = 0.;
    for (E e : edges) {
      weight += graph.getEdgeWeight(e);
    }

    GraphPathImpl<V, E> path = new GraphPathImpl<V, E>(graph, a, b, edges, weight);

    return path;
  }