Example usage for com.google.common.graph Network outEdges

List of usage examples for com.google.common.graph Network outEdges

Introduction

In this page you can find the example usage for com.google.common.graph Network outEdges.

Prototype

Set<E> outEdges(Object node);

Source Link

Document

Returns all edges in this graph which can be traversed in the direction (if any) of the edge starting from node .

Usage

From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java

/**
 * Returns a list of all distinct paths from roots of the network to leaves. The list can be in
 * arbitrary orders and can contain duplicate paths if there are multiple edges from two nodes.
 *//*from  w ww .  j  a v  a  2s .c  om*/
public static <NodeT, EdgeT> List<List<NodeT>> allPathsFromRootsToLeaves(Network<NodeT, EdgeT> network) {
    ArrayDeque<List<NodeT>> paths = new ArrayDeque<>();
    // Populate the list with all roots
    for (NodeT node : network.nodes()) {
        if (network.inDegree(node) == 0) {
            paths.add(ImmutableList.of(node));
        }
    }

    List<List<NodeT>> distinctPathsFromRootsToLeaves = new ArrayList<>();
    while (!paths.isEmpty()) {
        List<NodeT> path = paths.removeFirst();
        NodeT lastNode = path.get(path.size() - 1);
        if (network.outDegree(lastNode) == 0) {
            distinctPathsFromRootsToLeaves.add(new ArrayList<>(path));
        } else {
            for (EdgeT edge : network.outEdges(lastNode)) {
                paths.addFirst(ImmutableList.<NodeT>builder().addAll(path)
                        .add(network.incidentNodes(edge).target()).build());
            }
        }
    }
    return distinctPathsFromRootsToLeaves;
}