buildGraph function

Adjacency buildGraph(
  1. List<(int, int)> edges,
  2. int nodeCount
)

Builds an unweighted Adjacency list from edges, where nodeCount is the max node index + 1. Edges referencing out-of-range nodes are skipped.

Example:

buildGraph([(0, 1), (1, 2)], 3); // [[1], [2], []]

Implementation

Adjacency buildGraph(List<(int, int)> edges, int nodeCount) {
  final Adjacency adj = List.generate(nodeCount, (_) => <int>[]);
  for (final (int u, int v) in edges) {
    if (u >= 0 && u < nodeCount && v >= 0 && v < nodeCount) {
      adj[u].add(v);
    }
  }
  return adj;
}