Returns the set of all nodes in graph that have no predecessors. Note that in an undirected graph, this is equivalent to all isolated nodes.
(Graph<N> graph)
| 100 | |
| 101 | |
| 102 | public static <N> ImmutableSet<N> roots(Graph<N> graph) { |
| 103 | ImmutableSet.Builder<N> builder = ImmutableSet.builder(); |
| 104 | for (N node : graph.nodes()) { |
| 105 | if (graph.predecessors(node).isEmpty()) { |
| 106 | builder.add(node); |
| 107 | } |
| 108 | } |
| 109 | return builder.build(); |
| 110 | } |
| 111 | } |