DirectedGraph<T>.fromData constructor

DirectedGraph<T>.fromData(
  1. Map<T, List<T>> data,
  2. {Comparator<Vertex<T>> comparator}
)

Constructs a directed graph from a map of type Map<T, List<T>>.

  • Each key and list entry of type T is converted to a Vertex<T>.

Implementation

DirectedGraph.fromData(
  Map<T, List<T>> data, {
  Comparator<Vertex<T>> comparator,
}) : _comparator = comparator {
  // Map each distinct data object to a vertex.
  final vertexMap = <T, Vertex<T>>{};
  data ??= {};
  _edges = {};
  data.forEach((key, value) {
    vertexMap[key] = Vertex<T>(key);
    for (final t in value ?? <T>[]) {
      vertexMap[t] ??= Vertex<T>(t);
    }
  });
  // Construct the map of graph edges.
  data.forEach((key, value) {
    _edges[vertexMap[key]] = [];
    for (final t in value ?? <T>[]) {
      _edges[vertexMap[key]].add(vertexMap[t]);
    }
  });
  _inDegreeMap = _createInDegreeMap();
  _vertices = MutableLazy<UnmodifiableListView<Vertex<T>>>(_sortedVertices);
}