Networks { public static <NodeT, EdgeT> List<List<NodeT>> allPathsFromRootsToLeaves( Network<NodeT, EdgeT> network) { ArrayDeque<List<NodeT>> paths = new ArrayDeque<>(); 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; } private  Networks(); static void replaceDirectedNetworkNodes(
      MutableNetwork<N, E> network, Function<N, N> function); static Set<N> reachableNodes(
      Network<N, E> network, Set<N> startNodes, Set<N> endNodes); static Set<N> topologicalOrder(Network<N, E> network); static String toDot(Network<N, E> network); static List<List<NodeT>> allPathsFromRootsToLeaves(
      Network<NodeT, EdgeT> network);  }
@Test public void testAllPathsFromRootsToLeaves() { List<List<String>> expectedPaths = ImmutableList.of( ImmutableList.of("D"), ImmutableList.of("A", "B", "C", "F"), ImmutableList.of("A", "B", "E", "G"), ImmutableList.of("A", "B", "E", "G"), ImmutableList.of("A", "B", "E", "H"), ImmutableList.of("I", "J", "E", "G"), ImmutableList.of("I", "J", "E", "G"), ImmutableList.of("I", "J", "E", "H"), ImmutableList.of("I", "E", "G"), ImmutableList.of("I", "E", "G"), ImmutableList.of("I", "E", "H"), ImmutableList.of("I", "K", "L"), ImmutableList.of("M", "N", "L"), ImmutableList.of("M", "N", "L"), ImmutableList.of("O")); MutableNetwork<String, String> network = createNetwork(); List<List<String>> actualPaths = Networks.allPathsFromRootsToLeaves(network); assertThat(actualPaths, containsInAnyOrder(expectedPaths.toArray())); assertEquals(actualPaths.size(), expectedPaths.size()); }