Example usage for com.google.common.graph ElementOrder sorted

List of usage examples for com.google.common.graph ElementOrder sorted

Introduction

In this page you can find the example usage for com.google.common.graph ElementOrder sorted.

Prototype

public static <S> ElementOrder<S> sorted(Comparator<S> comparator) 

Source Link

Document

Returns an instance which specifies that the ordering of the elements is guaranteed to be determined by comparator .

Usage

From source file:org.apache.beam.runners.core.construction.graph.Networks.java

/**
 * Return a set of nodes in sorted topological order.
 *
 * <p>Note that back edges within directed graphs are "broken" returning a topological order for a
 * directed acyclic network which approximates the original network.
 *
 * <p>Nodes will be considered in the order specified by the {@link
 * ElementOrder#sorted(Comparator) sorted ElementOrder} created with the provided comparator.
 *//*from   ww w  .j a  v a2 s .  co  m*/
public static <NodeT, EdgeT> Iterable<NodeT> topologicalOrder(Network<NodeT, EdgeT> network,
        Comparator<NodeT> nodeOrder) {
    // Copy the characteristics of the network to ensure that the result network can represent the
    // original network, just with the provided suborder
    MutableNetwork<NodeT, EdgeT> orderedNetwork = NetworkBuilder.from(network)
            .nodeOrder(ElementOrder.sorted(nodeOrder)).build();
    for (NodeT node : network.nodes()) {
        orderedNetwork.addNode(node);
    }
    for (EdgeT edge : network.edges()) {
        EndpointPair<NodeT> incident = network.incidentNodes(edge);
        orderedNetwork.addEdge(incident.source(), incident.target(), edge);
    }
    return computeTopologicalOrder(orderedNetwork);
}