Example usage for com.google.common.graph MutableValueGraph removeEdge

List of usage examples for com.google.common.graph MutableValueGraph removeEdge

Introduction

In this page you can find the example usage for com.google.common.graph MutableValueGraph removeEdge.

Prototype

@CanIgnoreReturnValue
V removeEdge(@CompatibleWith("N") Object nodeU, @CompatibleWith("N") Object nodeV);

Source Link

Document

Removes the edge connecting nodeU to nodeV , if it is present.

Usage

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;
}