List of usage examples for com.google.common.graph Network inDegree
@Override
int inDegree(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. */// w w w .j a v a2s.c o m 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; }
From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java
/** Returns a set of nodes sorted in topological order. */ public static <N, E> Set<N> topologicalOrder(Network<N, E> network) { // TODO: Upgrade Guava and remove this method if topological sorting becomes // supported externally or remove this comment if its not going to be supported externally. checkArgument(network.isDirected(), "Only directed networks are supported, given %s", network); checkArgument(!network.allowsSelfLoops(), "Only networks without self loops are supported, given %s", network);//from w w w . j a v a 2 s . co m // Linked hashset will prevent duplicates from appearing and will maintain insertion order. LinkedHashSet<N> nodes = new LinkedHashSet<>(network.nodes().size()); Queue<N> processingOrder = new ArrayDeque<>(); // Add all the roots for (N node : network.nodes()) { if (network.inDegree(node) == 0) { processingOrder.add(node); } } while (!processingOrder.isEmpty()) { N current = processingOrder.remove(); // If all predecessors have already been added, then we can add this node, otherwise // we need to add the node to the back of the processing queue. if (nodes.containsAll(network.predecessors(current))) { nodes.add(current); processingOrder.addAll(network.successors(current)); } else { processingOrder.add(current); } } return nodes; }