BFS explores a graph layer by layer, so the first time it reaches a vertex is along a shortest path. Track dist[v] and parent[v] while exploring, then walk parents back from the target to reconstruct the route.

Algorithm

On the canonical graph from graph-adjacency-list, the shortest path from 1 to 6 is [1, 2, 4, 5, 6] with distance 4. The path is rebuilt from parent: 6 -> 5 -> 4 -> 2 -> 1, reversed.

layers equal distance BFS order equals distance in an unweighted graph.

Basic Implementation

basic.dart
Replay: real traced execution (multi-file project)
import 'dart:collection';
void main() {
  final adj = <int, List<int>>{
    1: [2, 3],
    2: [1, 4],
    3: [1, 4],
    4: [2, 3, 5],
    5: [4, 6],
    6: [5],
  };
  const src = 1;
  const dst = 6;
  final dist = <int, int>{src: 0};
  final parent = <int, int>{src: 0};
  final queue = Queue<int>.from([src]);
  while (queue.isNotEmpty) {
    final v = queue.removeFirst();
    for (final nb in adj[v]!) {
      if (!dist.containsKey(nb)) {
        dist[nb] = dist[v]! + 1;
        parent[nb] = v;
        queue.add(nb);
      }
    }
  }
  final path = <int>[];
  var node = dst;
  while (node != 0) {
    path.add(node);
    node = parent[node]!;
  }
  final ordered = path.reversed.toList();
  print(ordered);
  print(dist[dst]);
}
  1. dist ← {1: 0}

    12const dst = 6;13final dist = <int, int>{src: 0};14final parent = <int, int>{src: 0};
    values this step{1: 0}dist
  2. parent ← {1: null}

    13final dist = <int, int>{src: 0};14final parent = <int, int>{src: 0};15final queue = Queue<int>.from([src]);
    values this step{1: null}parent
  3. dist ← {1: 0, 2: 1, 3: 1}, parent ← {1: null, 2: 1, 3: 1}, queue ← [2, 3]

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1}dist{1: null, 2: 1, 3: 1}parent[2, 3]queue1dequeue
  4. dist ← {1: 0, 2: 1, 3: 1, 4: 2}, parent ← {1: null, 2: 1, 3: 1, 4: 2}

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1, 4: 2}dist{1: null, 2: 1, 3: 1, 4: 2}parent[3, 4]queue2dequeue
  5. dist ← {1: 0, 2: 1, 3: 1, 4: 2}, parent ← {1: null, 2: 1, 3: 1, 4: 2}

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1, 4: 2}dist{1: null, 2: 1, 3: 1, 4: 2}parent[4]queue3dequeue
  6. dist ← {1: 0, 2: 1, 3: 1, 4: 2, 5: 3}, parent ← {1: null, 2: 1, 3: 1, 4: 2, 5: 4}

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1, 4: 2, 5: 3}dist{1: null, 2: 1, 3: 1, 4: 2, 5: 4}parent[5]queue4dequeue
  7. dist ← {1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4}, parent ← {1: null, 2: 1, 3: 1, 4: 2, 5: 4, 6: 5}

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4}dist{1: null, 2: 1, 3: 1, 4: 2, 5: 4, 6: 5}parent[6]queue5dequeue
  8. dist ← {1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4}, parent ← {1: null, 2: 1, 3: 1, 4: 2, 5: 4, 6: 5}

    16while (queue.isNotEmpty) {17  final v = queue.removeFirst();18  for (final nb in adj[v]!) {
    values this step{1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4}dist{1: null, 2: 1, 3: 1, 4: 2, 5: 4, 6: 5}parent[]queue6dequeue
  9. path ← [1, 2, 4, 5, 6]

    26final path = <int>[];27var node = dst;28while (node != 0) {
    values this step[1, 2, 4, 5, 6]path{1: null, 2: 1, 3: 1, 4: 2, 5: 4, 6: 5}parent
  10. stdout ← [1, 2, 4, 5, 6]

    32final ordered = path.reversed.toList();33print(ordered);34print(dist[dst]);
    values this step[1, 2, 4, 5, 6]stdout[1, 2, 4, 5, 6]path
  11. stdout ← 4

    33  print(ordered);34  print(dist[dst]);35}
    values this step4stdout4dist[6]
  12. BFS path ← 1 -> 2 (1 edge, cost 10), cheaper weighted path ← 1 -> 3 -> 2 (2 edges, cost 2)

    33  print(ordered);34  print(dist[dst]);35}
    values this step1 -> 2 (1 edge, cost 10)BFS path1 -> 3 -> 2 (2 edges, cost 2)cheaper weighted pathuse Dijkstra with a priority queueweighted algorithm1->2 weight 10, 1->3 weight 1, 3->2 weight 1edge weights

Complexity

  • Time: O(V + E)
  • Space: O(V)

Implementation notes

  • Dart: a dist Map doubles as the visited check, parent records predecessors (0 marks the source), and a Queue gives FIFO order.
  • The replay shows dist, parent, and the queue filling in, then the reconstructed path. It also contrasts that unweighted result with a weighted graph where Dijkstra with a priority queue is required.