Graph.fromAdjacencyList constructor

Graph.fromAdjacencyList(
  1. AdjacencyList adjacencyList
)

Create a Graph using its adjacency list.

Implementation

Graph.fromAdjacencyList(AdjacencyList adjacencyList) {
  // TODO: Assert that this is a valid adjacency list? - ie adjacencyList[a] contains b <=> adjacencyList[b] contains a
  this.adjacencyList = _unmodifiableAdjacencyList(adjacencyList);

  // Populate this.edgeList by iterating over all the members of the Set
  // adjacencyList.values . Note that this means we see each edge twice, but
  // this is OK because edges does not allow duplicates.
  final edges = <Edge>{};
  for (final adjacencyEntry in adjacencyList.entries) {
    edges.addAll(adjacencyEntry.value.map(
        (rightNode) => Edge(left: adjacencyEntry.key, right: rightNode)));
  }
  edgeList = _unmodifiableEdgeList(edges);
}