// 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) { if (!isValid(v)) { throw new java.lang.NullPointerException("Invalid v parameter"); } if (!isValid(w)) { throw new java.lang.NullPointerException("Ivalid w parameter"); } int shortestLen = G.V() + 1; int len = -1; for (int iv : v) { for (int iw : w) { len = length(iv, iw); if ((len > 0) && (len < shortestLen)) { shortestLen = len; } } } if (shortestLen < G.V() + 1) { return shortestLen; } return -1; }
// constructor takes a digraph (not necessarily a DAG) public SAP(Digraph G) { digraph = new Digraph(G.V()); for (int v = 0; v < G.V(); v++) { for (int w : G.adj(v)) { digraph.addEdge(v, w); } } }
private boolean isOnlyOneRoot(Digraph g) { int total = 0; for (int i = 0; i < g.V(); i++) { if (g.outdegree(i) == 0) total++; } return total == 1; }
/* do unit testing of this class */ public static void main(String[] args) { System.out.println("Test started file name " + args[0]); In in = new In(args[0]); Digraph G = new Digraph(in); System.out.println("Graph vertices " + G.V() + " graph edges " + G.E()); System.out.println(G.toString()); SAP sap = new SAP(G); while (!StdIn.isEmpty()) { int v = StdIn.readInt(); int w = StdIn.readInt(); int length = sap.length(v, w); int ancestor = sap.ancestor(v, w); StdOut.printf("length = %d, ancestor = %d\n", length, ancestor); } }
// do unit testing of this class public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); StdOut.println(G.toString()); SAP sap = new SAP(G); while (!StdIn.isEmpty()) { int v = StdIn.readInt(); int w = StdIn.readInt(); int length = sap.length(v, w); int ancestor = sap.ancestor(v, w); StdOut.printf("length = %d, ancestor = %d\n", length, ancestor); } }
public WordNet(String synsetFileName, String hyponymFileName) { In synsetFile = new In(synsetFileName); In hyponymFile = new In(hyponymFileName); String[] currentLine; String[] splitNouns; while (!synsetFile.isEmpty()) { currentLine = synsetFile.readLine().split(","); map.put(Integer.parseInt(currentLine[0]), currentLine[1]); splitNouns = currentLine[1].split(" "); for (int i = 0; i < splitNouns.length; i += 1) { if (!nouns.contains(splitNouns[i])) { nouns.add(splitNouns[i]); } } } synsets = new Digraph(map.size()); while (!hyponymFile.isEmpty()) { currentLine = hyponymFile.readLine().split(","); for (int i = 1; i < currentLine.length; i++) { synsets.addEdge(Integer.parseInt(currentLine[0]), Integer.parseInt(currentLine[i])); } } }
/* * 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; }
/* * 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; }
// a common ancestor that participates in shortest ancestral path; -1 if no such path public int ancestor(Iterable<Integer> v, Iterable<Integer> w) { if (!isValid(v)) { throw new java.lang.NullPointerException("Invalid v parameter"); } if (!isValid(w)) { throw new java.lang.NullPointerException("Ivalid w parameter"); } int ancestor; int len; ShortestPath sp; ShortestPath maxsp = new ShortestPath(); maxsp.length = G.V() + 1; maxsp.ancestor = -1; for (int iv : v) { for (int iw : w) { sp = findShortestPath(iv, iw); if ((sp.length > 0) && (sp.length < maxsp.length)) { maxsp.length = sp.length; maxsp.length = sp.ancestor; } } } return maxsp.ancestor; }
private ShortestPath findShortestPath(int v, int w) { if (!isValid(v)) { throw new java.lang.NullPointerException("Invalid v parameter"); } if (!isValid(w)) { throw new java.lang.NullPointerException("Ivalid w parameter"); } Queue<Integer> q = new Queue<Integer>(); boolean[] marked = new boolean[G.V()]; int[] distTo = new int[G.V()]; q.enqueue(v); q.enqueue(w); marked[v] = true; marked[w] = true; distTo[v] = 0; distTo[w] = 0; while (!q.isEmpty()) { int a = q.dequeue(); for (int b : G.adj(a)) { if (marked[b]) { int length = distTo[b] + distTo[a] + 1; ShortestPath sp = new ShortestPath(); sp.ancestor = b; sp.length = length; return sp; } distTo[b] = distTo[a] + 1; marked[b] = true; q.enqueue(b); } } return null; }
// constructor takes the name of the two input files public WordNet(String synsets, String hypernyms) { ifNullThrowException(synsets); ifNullThrowException(hypernyms); st = new LinearProbingHashST<String, ArrayList<Integer>>(); In in = new In(synsets); int len = 0; int No; String[] words; ArrayList<String> tempKeys = new ArrayList<String>(); while (in.hasNextLine()) { String[] a = in.readLine().split(","); tempKeys.add(a[1]); No = Integer.parseInt(a[0]); words = a[1].split(" "); for (String word : words) { if (st.contains(word)) { st.get(word).add(No); } else { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(No); st.put(word, list); } } len++; } keys = new String[len]; tempKeys.toArray(keys); G = new Digraph(len); in = new In(hypernyms); while (in.hasNextLine()) { String temp = in.readLine(); String[] a = temp.split(","); int v = Integer.parseInt(a[0]); for (int i = 1; i < a.length; i++) { int w = Integer.parseInt(a[i]); G.addEdge(v, w); } } ifNotDagThrowException(G); sap = new SAP(G); }
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; }
public static void main(String[] args) { In in1 = new In(Inputs.DIGRAPH_SMALL); In in2 = new In(Inputs.DIGRAPH_SMALL); edu.princeton.cs.algs4.Digraph sedgewicks = new edu.princeton.cs.algs4.Digraph(in1); Digraph digraph = new Digraph(in2); in1.close(); in2.close(); StdOut.printf("Sedgewicks - v: %d, e: %d\n", sedgewicks.V(), sedgewicks.E()); for (int i = 0; i < sedgewicks.V(); i++) { for (int w : sedgewicks.adj(i)) { StdOut.printf("%d -> %d\n", i, w); } } StdOut.println(); StdOut.printf("My - v: %d, e: %d\n", digraph.V(), digraph.E()); for (int i = 0; i < digraph.V(); i++) { for (int w : digraph.adj(i)) { StdOut.printf("%d -> %d\n", i, w); } } StdOut.println(); edu.princeton.cs.algs4.Digraph sedwickDi = sedgewicks.reverse(); StdOut.printf("Sedgewicks reverse - v: %d, e: %d\n", sedwickDi.V(), sedwickDi.E()); for (int i = 0; i < sedwickDi.V(); i++) { for (int w : sedwickDi.adj(i)) { StdOut.printf("%d -> %d\n", i, w); } } StdOut.println(); Digraph myReverse = digraph.reverse(); StdOut.printf("My reverse - v: %d, e: %d\n", myReverse.V(), myReverse.E()); for (int i = 0; i < myReverse.V(); i++) { for (int w : myReverse.adj(i)) { StdOut.printf("%d -> %d\n", i, w); } } }
private boolean checkVertexInBound(int vertex) { return ((0 <= vertex) && (vertex < copyG.V())); }
public UncoverBipartite(Digraph G) { this.G = G; this.bipartite = new Digraph(G.V() * 2); // for both side of the bigraph }