List of usage examples for com.google.common.graph MutableValueGraph removeEdge
@CanIgnoreReturnValue V removeEdge(@CompatibleWith("N") Object nodeU, @CompatibleWith("N") Object nodeV);
From source file:no.ssb.vtl.script.operations.hierarchy.HierarchyOperation.java
@VisibleForTesting static <T> LinkedList<T> sortTopologically(ValueGraph<T, Composition> graph) { // Kahn's algorithm MutableValueGraph<T, Composition> g = Graphs.copyOf(graph); LinkedList<T> sorted = Lists.newLinkedList(); Deque<T> leaves = Lists.newLinkedList(g.nodes().stream().filter(n -> g.inDegree(n) == 0).collect(toList())); while (!leaves.isEmpty()) { T node = leaves.pop();/* ww w .ja va2 s. c o m*/ sorted.push(node); Set<T> successors = ImmutableSet.copyOf(g.successors(node)); for (T successor : successors) { g.removeEdge(node, successor); if (g.inDegree(successor) == 0) { leaves.addLast(successor); } } } checkArgument(g.edges().isEmpty(), "the graph contains a circular dependency %s", g); Collections.reverse(sorted); return sorted; }