Graph.fromEdgeList constructor

Graph.fromEdgeList(
  1. EdgeList edgeList
)

Create a Graph using its edge list.

Implementation

Graph.fromEdgeList(EdgeList edgeList) {
  // TODO: assert that this is a valid edge list? - ie no edge loops are present
  this.edgeList = _unmodifiableEdgeList(edgeList);

  // Populate this.adjacencyList by adding data to adjacencies[edge.left] and
  // adjacencies[edge.right], for every edge.
  final adjacencies = <Node, Set<Node>>{};
  for (final edge in edgeList) {
    if (adjacencies.containsKey(edge.left)) {
      adjacencies[edge.left]!.add(edge.right);
    } else {
      adjacencies[edge.left] = {edge.right};
    }

    if (adjacencies.containsKey(edge.right)) {
      adjacencies[edge.right]!.add(edge.left);
    } else {
      adjacencies[edge.right] = {edge.left};
    }
  }

  adjacencyList = _unmodifiableAdjacencyList(adjacencies);
}