Networks { public static <NodeT> Iterable<NodeT> topologicalOrder(Network<NodeT, ?> network) { return computeTopologicalOrder(Graphs.copyOf(network)); } private  Networks(); static void replaceDirectedNetworkNodes(
      MutableNetwork<NodeT, EdgeT> network, Function<NodeT, NodeT> function); static Set<NodeT> reachableNodes(
      Network<NodeT, EdgeT> network, Set<NodeT> startNodes, Set<NodeT> endNodes); static Iterable<NodeT> topologicalOrder(Network<NodeT, ?> network); static Iterable<NodeT> topologicalOrder(
      Network<NodeT, EdgeT> network, Comparator<NodeT> nodeOrder); static String toDot(Network<NodeT, EdgeT> network); static List<List<NodeT>> allPathsFromRootsToLeaves(
      Network<NodeT, EdgeT> network);  }
@Test public void testTopologicalSortWithEmptyNetwork() { assertThat(Networks.topologicalOrder(createEmptyNetwork()), emptyIterable()); }
@Test public void testTopologicalSortWithSuborder() { Comparator<String> subOrder = (Comparator<String>) (Comparator) Ordering.arbitrary(); MutableNetwork<String, String> network = createNetwork(); Iterable<String> sortedNodes = Networks.topologicalOrder(network, subOrder); MutableNetwork<String, String> naturalOrderedNetwork = NetworkBuilder.from(network) .nodeOrder(ElementOrder.<String>natural()) .edgeOrder(ElementOrder.<String>natural()) .build(); MutableNetwork<String, String> arbitraryOrderNetwork = NetworkBuilder.from(network) .nodeOrder(ElementOrder.unordered()) .edgeOrder(ElementOrder.unordered()) .build(); MutableNetwork<String, String> reverseNaturalOrderNetwork = NetworkBuilder.from(network) .nodeOrder(ElementOrder.sorted(Ordering.natural().reverse())) .edgeOrder(ElementOrder.sorted(Ordering.natural().reverse())) .build(); for (String node : network.nodes()) { naturalOrderedNetwork.addNode(node); arbitraryOrderNetwork.addNode(node); reverseNaturalOrderNetwork.addNode(node); } for (String edge : network.edges()) { EndpointPair<String> incident = network.incidentNodes(edge); naturalOrderedNetwork.addEdge(incident.source(), incident.target(), edge); arbitraryOrderNetwork.addEdge(incident.source(), incident.target(), edge); reverseNaturalOrderNetwork.addEdge(incident.source(), incident.target(), edge); } Iterable<String> naturalSortedNodes = Networks.topologicalOrder(naturalOrderedNetwork, subOrder); Iterable<String> arbitrarySortedNodes = Networks.topologicalOrder(arbitraryOrderNetwork, subOrder); Iterable<String> reverseNaturalSortedNodes = Networks.topologicalOrder(reverseNaturalOrderNetwork, subOrder); assertThat(sortedNodes, equalTo(naturalSortedNodes)); assertThat(sortedNodes, equalTo(arbitrarySortedNodes)); assertThat(sortedNodes, equalTo(reverseNaturalSortedNodes)); }