DirectedGraph<T extends Object>.transitiveClosure constructor

DirectedGraph<T extends Object>.transitiveClosure(
  1. DirectedGraph<T> graph
)

Factory constructor returning the transitive closure of graph.

Implementation

factory DirectedGraph.transitiveClosure(DirectedGraph<T> graph) {
  final tcEdges = <T, Set<T>>{};
  void addReachableVertices(T root, T current) {
    for (final vertex in graph.edges(current)) {
      if (tcEdges[root]!.contains(vertex)) continue;
      tcEdges[root]!.add(vertex);
      addReachableVertices(root, vertex);
    }
  }

  for (final root in graph) {
    tcEdges[root] = <T>{};
    addReachableVertices(root, root);
  }
  return DirectedGraph(tcEdges, comparator: graph.comparator);
}