public static void main(String[] args) { Path path = Paths.get("/Users/apple/depot/scala-playground/src/main/java/dsAlgo/graph/directedGraphWithCycle.txt"); DirectedGraph graph = DirectedGraph.loadFromFile(path); DirectedCycle cycle = new DirectedCycle(graph); if (cycle.hasCycle()) { StringBuilder sb = new StringBuilder(); IntStack stack = cycle.cycle(); while (!stack.isEmpty()) { sb.append(stack.popInt()); if (!stack.isEmpty()) sb.append(" -> "); } System.out.println(sb.toString()); } }
public IntStack pathTo(int w) { if (isConnectedTo(w)) { IntStack result = new IntArrayList(); for (int r = w; r != sourceVertex; r = edgeTo[r]) result.push(r); result.push(sourceVertex); return result; } else { return new IntArrayList(0); } }
public IntStack cycle() { return cycle; }