{
  "id": "graphs/graph-bfs",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/graphs/graph-bfs",
  "topic": {
    "slug": "graphs",
    "title": "Graphs"
  },
  "title": "Breadth-First Search",
  "tagline": "Explore a network in rings, nearest first — using a queue.",
  "vizKind": "graph",
  "complexity": {
    "time": {
      "best": "O(V + E)",
      "average": "O(V + E)",
      "worst": "O(V + E)"
    },
    "space": "O(V)",
    "growth": "linear",
    "note": "It touches every node (V) once and looks along every edge (E) once, so the cost is V + E. The space is the queue plus the 'seen' set, which in the worst case holds every node: O(V)."
  },
  "code": {
    "javascript": "function bfs(graph, start) {\n  const queue = [start];\n  const seen = new Set([start]);\n  while (queue.length > 0) {\n    const node = queue.shift();\n    visit(node);\n    for (const next of graph[node]) {\n      if (!seen.has(next)) {\n        seen.add(next);\n        queue.push(next);\n      }\n    }\n  }\n}",
    "python": "from collections import deque\n\ndef bfs(graph, start):\n    queue = deque([start])\n    seen = {start}\n    while queue:\n        node = queue.popleft()\n        visit(node)\n        for nxt in graph[node]:\n            if nxt not in seen:\n                seen.add(nxt)\n                queue.append(nxt)\n",
    "java": "void bfs(Map<String,List<String>> graph, String start) {\n  Deque<String> queue = new ArrayDeque<>(List.of(start));\n  Set<String> seen = new HashSet<>(List.of(start));\n  while (!queue.isEmpty()) {\n    String node = queue.pollFirst();\n    visit(node);\n    for (String next : graph.get(node)) {\n      if (!seen.contains(next)) {\n        seen.add(next);\n        queue.addLast(next);\n      }\n    }\n  }\n}",
    "cpp": "void bfs(map<string,vector<string>>& graph, string start) {\n  queue<string> q; q.push(start);\n  set<string> seen{start};\n  while (!q.empty()) {\n    string node = q.front(); q.pop();\n    visit(node);\n    for (auto& next : graph[node]) {\n      if (!seen.count(next)) {\n        seen.insert(next);\n        q.push(next);\n      }\n    }\n  }\n}"
  },
  "defaultInput": [
    1
  ],
  "defaultTarget": null,
  "inputHint": "Pick a start node: 1 = A, up to 8 = H. Watch it explore in rings.",
  "pitfalls": [
    {
      "wrong": "Forgetting to mark nodes as seen when you add them to the queue.",
      "right": "Mark a node the moment it enters the queue, not when you visit it. Otherwise the same node gets queued many times and you may loop forever on a cycle."
    },
    {
      "wrong": "Using a stack instead of a queue.",
      "right": "A stack turns BFS into DFS — it goes deep instead of wide. If you want rings, you need first-in-first-out: a queue."
    },
    {
      "wrong": "Checking 'seen' only when popping, not when pushing.",
      "right": "Too late — the duplicates are already in the queue. Check and mark before pushing, so a node can only ever be added once."
    }
  ],
  "quiz": [
    {
      "q": "What data structure drives breadth-first search?",
      "options": [
        "A stack",
        "A queue",
        "A sorted list",
        "Another graph"
      ],
      "answer": 1,
      "why": "A queue is first-in-first-out, so nodes are visited in the order they were discovered — which means nearer nodes always come out before farther ones. That's what produces the ring-by-ring exploration."
    },
    {
      "q": "In an unweighted graph, what does BFS find?",
      "options": [
        "The longest path",
        "The shortest path from the start to every node",
        "A random path",
        "Nothing useful"
      ],
      "answer": 1,
      "why": "Because BFS reaches nodes in order of distance, the first time it arrives at a node is by the fewest steps possible — that's the shortest path."
    },
    {
      "q": "Why must you mark a node 'seen' as soon as it enters the queue?",
      "options": [
        "To make it look nicer",
        "So it isn't added again and again, which could loop forever on a cycle",
        "To count the nodes",
        "You don't need to mark it at all"
      ],
      "answer": 1,
      "why": "Graphs have cycles. Without marking on entry, a node reachable by several paths gets queued multiple times, wasting work and risking an infinite loop."
    }
  ],
  "problems": [
    {
      "name": "Number of Islands",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/number-of-islands/"
    },
    {
      "name": "Rotting Oranges",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/rotting-oranges/"
    },
    {
      "name": "Binary Tree Level Order Traversal",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/binary-tree-level-order-traversal/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 21,
    "frames": [
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "pivot"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "A"
            ]
          },
          "visited": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 2,
        "variables": {
          "start": "A",
          "queue": "A"
        },
        "explanation": "Breadth-first search fans out in rings from A, using a queue. A queue is first-in-first-out, so nearer nodes always come off before farther ones — that's why BFS explores level by level.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": []
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "A",
          "order": "A"
        },
        "explanation": "Take A off the front of the queue and visit it. Visit order so far: A.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "compare"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B",
              "role": "active"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "A",
          "discovered": "B",
          "queue": "B"
        },
        "explanation": "B is a new neighbour of A. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "active"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "compare"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C",
              "role": "active"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "B",
              "C"
            ]
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "A",
          "discovered": "C",
          "queue": "B C"
        },
        "explanation": "C is a new neighbour of A. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "pivot"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "C"
            ]
          },
          "visited": [
            "A",
            "B"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "B",
          "order": "A · B"
        },
        "explanation": "Take B off the front of the queue and visit it. Visit order so far: A · B.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "pivot"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "compare"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D",
              "role": "active"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "C",
              "D"
            ]
          },
          "visited": [
            "A",
            "B"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "B",
          "discovered": "D",
          "queue": "C D"
        },
        "explanation": "D is a new neighbour of B. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "pivot"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "compare"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E",
              "role": "active"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "C",
              "D",
              "E"
            ]
          },
          "visited": [
            "A",
            "B"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "B",
          "discovered": "E",
          "queue": "C D E"
        },
        "explanation": "E is a new neighbour of B. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "D",
              "E"
            ]
          },
          "visited": [
            "A",
            "B",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "C",
          "order": "A · B · C"
        },
        "explanation": "Take C off the front of the queue and visit it. Visit order so far: A · B · C.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "compare"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F",
              "role": "active"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "D",
              "E",
              "F"
            ]
          },
          "visited": [
            "A",
            "B",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "C",
          "discovered": "F",
          "queue": "D E F"
        },
        "explanation": "F is a new neighbour of C. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "compare"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G",
              "role": "active"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "D",
              "E",
              "F",
              "G"
            ]
          },
          "visited": [
            "A",
            "B",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "C",
          "discovered": "G",
          "queue": "D E F G"
        },
        "explanation": "G is a new neighbour of C. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "active"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "E",
              "F",
              "G"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "D",
          "order": "A · B · C · D"
        },
        "explanation": "Take D off the front of the queue and visit it. Visit order so far: A · B · C · D.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "E",
              "F",
              "G"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "visiting": "D",
          "note": "no new neighbours"
        },
        "explanation": "D has no unseen neighbours, so there's nothing to add. Back to the queue.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "active"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "F",
              "G"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "E",
          "order": "A · B · C · D · E"
        },
        "explanation": "Take E off the front of the queue and visit it. Visit order so far: A · B · C · D · E.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "active"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "compare"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H",
              "role": "active"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "F",
              "G",
              "H"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "E",
          "discovered": "H",
          "queue": "F G H"
        },
        "explanation": "H is a new neighbour of E. Mark it seen and add it to the queue.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "active"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "pivot"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "G",
              "H"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "F",
          "order": "A · B · C · D · E · F"
        },
        "explanation": "Take F off the front of the queue and visit it. Visit order so far: A · B · C · D · E · F.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "pivot"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "pivot"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "G",
              "H"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "visiting": "F",
          "note": "no new neighbours"
        },
        "explanation": "F has no unseen neighbours, so there's nothing to add. Back to the queue.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "active"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "pivot"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "H"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "G",
          "order": "A · B · C · D · E · F · G"
        },
        "explanation": "Take G off the front of the queue and visit it. Visit order so far: A · B · C · D · E · F · G.",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "pivot"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": [
              "H"
            ]
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "visiting": "G",
          "note": "no new neighbours"
        },
        "explanation": "G has no unseen neighbours, so there's nothing to add. Back to the queue.",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "active"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": []
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "H",
          "order": "A · B · C · D · E · F · G · H"
        },
        "explanation": "Take H off the front of the queue and visit it. Visit order so far: A · B · C · D · E · F · G · H.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": []
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "visiting": "H",
          "note": "no new neighbours"
        },
        "explanation": "H has no unseen neighbours, so there's nothing to add. Back to the queue.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        },
        "accent": "sorted"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "A",
              "label": "A",
              "x": 0.5,
              "y": 0.12,
              "role": "sorted"
            },
            {
              "id": "B",
              "label": "B",
              "x": 0.24,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "F",
              "label": "F",
              "x": 0.6,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "id": "H",
              "label": "H",
              "x": 0.5,
              "y": 0.92,
              "role": "sorted"
            }
          ],
          "edges": [
            {
              "from": "A",
              "to": "B"
            },
            {
              "from": "A",
              "to": "C"
            },
            {
              "from": "B",
              "to": "D"
            },
            {
              "from": "B",
              "to": "E"
            },
            {
              "from": "C",
              "to": "F"
            },
            {
              "from": "C",
              "to": "G"
            },
            {
              "from": "E",
              "to": "H"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "queue",
            "ids": []
          },
          "visited": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "order": "A · B · C · D · E · F · G · H",
          "visited": 8
        },
        "explanation": "The queue is empty, so every reachable node has been visited: A · B · C · D · E · F · G · H. Notice the order sweeps outward level by level.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        },
        "accent": "sorted"
      }
    ]
  }
}