List of usage examples for com.google.common.graph Network outEdges
Set<E> outEdges(Object node);
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; }