コード例 #1
0
ファイル: SAP.java プロジェクト: borysg1/algoTraining
  /*
   * a common ancestor of v and w that participates in a shortest ancestral
   * path; -1 if no such path
   */
  public int ancestor(int v, int w) {
    if ((!checkVertexInBound(v)) || (!checkVertexInBound(w)))
      throw new java.lang.IndexOutOfBoundsException();

    BreadthFirstDirectedPaths bfdDirPaths1 = new BreadthFirstDirectedPaths(copyG, v);
    BreadthFirstDirectedPaths bfdDirPaths2 = new BreadthFirstDirectedPaths(copyG, w);

    int minLength = Integer.MAX_VALUE;
    int commAncestor = Integer.MAX_VALUE;

    for (int vrtx = 0; vrtx < copyG.V(); vrtx++) {
      int dist1 = bfdDirPaths1.distTo(vrtx);
      int dist2 = bfdDirPaths2.distTo(vrtx);
      int dist = dist1 + dist2;
      if (Integer.MAX_VALUE != dist1 && Integer.MAX_VALUE != dist2 && dist < minLength) {
        minLength = dist;
        commAncestor = vrtx;
      }
    }

    if (Integer.MAX_VALUE == minLength) {
      commAncestor = -1;
    }

    return commAncestor;
  }
コード例 #2
0
ファイル: SAP.java プロジェクト: borysg1/algoTraining
  /*
   * length of shortest ancestral path between any vertex in v and any vertex
   * in w; -1 if no such path
   */
  public int length(Iterable<Integer> v, Iterable<Integer> w) {
    for (int vrtx : v) {
      if (!checkVertexInBound(vrtx)) throw new java.lang.IndexOutOfBoundsException();
    }

    for (int vrtx : w) {
      if (!checkVertexInBound(vrtx)) throw new java.lang.IndexOutOfBoundsException();
    }

    BreadthFirstDirectedPaths bfdDirPaths1 = new BreadthFirstDirectedPaths(copyG, v);
    BreadthFirstDirectedPaths bfdDirPaths2 = new BreadthFirstDirectedPaths(copyG, w);

    int minPath = Integer.MAX_VALUE;

    for (int vrtx = 0; vrtx < copyG.V(); vrtx++) {
      int dist1 = bfdDirPaths1.distTo(vrtx);
      int dist2 = bfdDirPaths2.distTo(vrtx);
      int dist = dist1 + dist2;
      if (Integer.MAX_VALUE != dist1 && Integer.MAX_VALUE != dist2 && dist < minPath) {
        minPath = dist;
      }
    }

    if (Integer.MAX_VALUE == minPath) {
      minPath = -1;
    }

    return minPath;
  }
コード例 #3
0
ファイル: SAP.java プロジェクト: nicusorb/coursera
  private Ancestor computeAncestor(Iterable<Integer> v, Iterable<Integer> w) {
    BreadthFirstDirectedPaths bfsFromV = new BreadthFirstDirectedPaths(digraph, v);
    BreadthFirstDirectedPaths bfsFromW = new BreadthFirstDirectedPaths(digraph, w);

    Ancestor ancestor = new Ancestor();
    ancestor.vertex = -1;
    ancestor.dist = Integer.MAX_VALUE;

    for (int vertex = 0; vertex < digraph.V(); vertex++) {
      if (isAnAncestor(vertex, bfsFromV, bfsFromW)) {
        int distToVertex = bfsFromV.distTo(vertex) + bfsFromW.distTo(vertex);
        if (distToVertex < ancestor.dist) {
          ancestor.vertex = vertex;
          ancestor.dist = distToVertex;
        }
      }
    }
    if (ancestor.dist == Integer.MAX_VALUE) ancestor.dist = -1;
    return ancestor;
  }
コード例 #4
0
ファイル: SAP.java プロジェクト: ryf123/algorithm
 // if retDist is set, then return shortest dist
 // else return shortest ancestor
 private int get_dist_ancestor(Iterable<Integer> v, Iterable<Integer> w, boolean retDist) {
   int dist = INFINITY;
   int synset = -1;
   BreadthFirstDirectedPaths bfsA = new BreadthFirstDirectedPaths(this.graph, v);
   BreadthFirstDirectedPaths bfsB = new BreadthFirstDirectedPaths(this.graph, w);
   for (int i = 0; i < this.graph.V(); i++) {
     if (bfsA.distTo(i) != INFINITY && bfsB.distTo(i) != INFINITY) {
       int currentDist = bfsA.distTo(i) + bfsB.distTo(i);
       if (currentDist < dist) {
         synset = i;
         dist = currentDist;
       }
     }
   }
   if (dist == INFINITY) {
     return -1;
   }
   if (retDist) {
     return dist;
   } else {
     return synset;
   }
 }
コード例 #5
0
ファイル: SAP.java プロジェクト: nicusorb/coursera
 private boolean isAnAncestor(
     int vertex, BreadthFirstDirectedPaths bfsFromV, BreadthFirstDirectedPaths bfsFromW) {
   return bfsFromV.hasPathTo(vertex) && bfsFromW.hasPathTo(vertex);
 }