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

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

Introduction

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

Prototype

@Override
int outDegree(Object node);

Source Link

Document

Returns the number of #outEdges(Object) outgoing edges in this graph of node .

Usage

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

static OutputReceiver[] getOutputReceivers(Network<Node, Edge> network, Node node) {
    int outDegree = network.outDegree(node);
    if (outDegree == 0) {
        return EMPTY_OUTPUT_RECEIVER_ARRAY;
    }/* w ww  .j  av  a2s  .c  o m*/

    OutputReceiver[] receivers = new OutputReceiver[outDegree];
    Iterator<Node> receiverNodes = network.successors(node).iterator();
    int i = 0;
    do {
        receivers[i] = ((OutputReceiverNode) receiverNodes.next()).getOutputReceiver();
        i += 1;
    } while (receiverNodes.hasNext());

    return receivers;
}

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.
 *//*www . ja  va 2  s .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;
}