{
  "id": "graphs/graph-dfs",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/graphs/graph-dfs",
  "topic": {
    "slug": "graphs",
    "title": "Graphs"
  },
  "title": "Depth-First Search",
  "tagline": "Plunge down one path to the end, then back up and try the next.",
  "vizKind": "graph",
  "complexity": {
    "time": {
      "best": "O(V + E)",
      "average": "O(V + E)",
      "worst": "O(V + E)"
    },
    "space": "O(V)",
    "growth": "linear",
    "note": "Same cost as BFS: every node once, every edge once, so V + E. The space is the stack — O(V) in the worst case. With recursive DFS, that stack is the call stack, which is why very deep graphs can overflow it."
  },
  "code": {
    "javascript": "function dfs(graph, start) {\n  const stack = [start];\n  const seen = new Set([start]);\n  while (stack.length > 0) {\n    const node = stack.pop();\n    visit(node);\n    for (const next of graph[node]) {\n      if (!seen.has(next)) {\n        seen.add(next);\n        stack.push(next);\n      }\n    }\n  }\n}",
    "python": "def dfs(graph, start):\n    stack = [start]\n    seen = {start}\n    while stack:\n        node = stack.pop()\n        visit(node)\n        for nxt in graph[node]:\n            if nxt not in seen:\n                seen.add(nxt)\n                stack.append(nxt)\n",
    "java": "void dfs(Map<String,List<String>> graph, String start) {\n  Deque<String> stack = new ArrayDeque<>(List.of(start));\n  Set<String> seen = new HashSet<>(List.of(start));\n  while (!stack.isEmpty()) {\n    String node = stack.pop();\n    visit(node);\n    for (String next : graph.get(node)) {\n      if (!seen.contains(next)) {\n        seen.add(next);\n        stack.push(next);\n      }\n    }\n  }\n}",
    "cpp": "void dfs(map<string,vector<string>>& graph, string start) {\n  vector<string> stack{start};\n  set<string> seen{start};\n  while (!stack.empty()) {\n    string node = stack.back(); stack.pop_back();\n    visit(node);\n    for (auto& next : graph[node]) {\n      if (!seen.count(next)) {\n        seen.insert(next);\n        stack.push_back(next);\n      }\n    }\n  }\n}"
  },
  "defaultInput": [
    1
  ],
  "defaultTarget": null,
  "inputHint": "Pick a start node: 1 = A, up to 8 = H. Watch it dive deep first.",
  "pitfalls": [
    {
      "wrong": "Using a queue instead of a stack.",
      "right": "A queue makes it explore in rings — that's BFS. DFS needs last-in-first-out so it dives into the newest node: a stack (or recursion)."
    },
    {
      "wrong": "Recursing on a huge graph without thinking about the call stack.",
      "right": "Recursive DFS uses the call stack, and a very deep graph can overflow it. For deep graphs, use an explicit stack, like the code here does."
    },
    {
      "wrong": "Not tracking visited nodes.",
      "right": "Without a 'seen' set, a cycle sends DFS around forever. Mark nodes as you discover them, just like BFS."
    }
  ],
  "quiz": [
    {
      "q": "What data structure gives DFS its 'go deep first' behaviour?",
      "options": [
        "A queue",
        "A stack",
        "A sorted list",
        "A heap"
      ],
      "answer": 1,
      "why": "A stack is last-in-first-out, so DFS always continues from the most recently discovered node — plunging deeper before it spreads sideways."
    },
    {
      "q": "How do the nodes visited by DFS and BFS compare on the same graph?",
      "options": [
        "DFS visits fewer nodes",
        "They visit the same nodes, in a different order",
        "BFS visits fewer nodes",
        "They visit completely different nodes"
      ],
      "answer": 1,
      "why": "Both reach every node connected to the start, exactly once, at the same O(V + E) cost. Only the order differs — and that order is what makes each suited to different problems."
    },
    {
      "q": "Why can a recursive DFS crash on a very deep graph?",
      "options": [
        "It runs out of nodes",
        "Each level of depth adds a frame to the call stack, which can overflow",
        "Recursion is always slow",
        "It visits nodes twice"
      ],
      "answer": 1,
      "why": "Recursive DFS rides the call stack — one frame per level of depth. A long enough path fills the stack and overflows it, which is why deep graphs often use an explicit stack instead."
    }
  ],
  "problems": [
    {
      "name": "Flood Fill",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/flood-fill/"
    },
    {
      "name": "Number of Islands",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/number-of-islands/"
    },
    {
      "name": "Max Area of Island",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/max-area-of-island/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 20,
    "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": "stack",
            "ids": [
              "A"
            ]
          },
          "visited": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 2,
        "variables": {
          "start": "A",
          "stack": "A"
        },
        "explanation": "Depth-first search plunges as deep as it can from A, using a stack. A stack is last-in-first-out, so it always dives into the most recently found node — that's why DFS goes deep before wide.",
        "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": "stack",
            "ids": []
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "A",
          "order": "A"
        },
        "explanation": "Pop A off the top of the stack 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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "A",
          "discovered": "B",
          "stack": "B"
        },
        "explanation": "B is a new neighbour of A. Mark it seen and add it to the stack.",
        "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": "stack",
            "ids": [
              "B",
              "C"
            ]
          },
          "visited": [
            "A"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "A",
          "discovered": "C",
          "stack": "B C"
        },
        "explanation": "C is a new neighbour of A. Mark it seen and add it to the stack.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "C",
          "order": "A · C"
        },
        "explanation": "Pop C off the top of the stack and visit it. Visit order so far: A · C.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "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,
              "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": "stack",
            "ids": [
              "B",
              "F"
            ]
          },
          "visited": [
            "A",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "C",
          "discovered": "F",
          "stack": "B F"
        },
        "explanation": "F is a new neighbour of C. Mark it seen and add it to the stack.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "active"
            },
            {
              "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,
              "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": "stack",
            "ids": [
              "B",
              "F",
              "G"
            ]
          },
          "visited": [
            "A",
            "C"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "C",
          "discovered": "G",
          "stack": "B F G"
        },
        "explanation": "G is a new neighbour of C. Mark it seen and add it to the stack.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "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,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "active"
            },
            {
              "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": "stack",
            "ids": [
              "B",
              "F"
            ]
          },
          "visited": [
            "A",
            "C",
            "G"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "G",
          "order": "A · C · G"
        },
        "explanation": "Pop G off the top of the stack and visit it. Visit order so far: A · C · G.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "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,
              "role": "pivot"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "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": "stack",
            "ids": [
              "B",
              "F"
            ]
          },
          "visited": [
            "A",
            "C",
            "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 stack.",
        "stats": {
          "comparisons": 3,
          "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "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,
              "role": "active"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "F",
          "order": "A · C · G · F"
        },
        "explanation": "Pop F off the top of the stack and visit it. Visit order so far: A · C · G · F.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "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,
              "role": "active"
            },
            {
              "id": "G",
              "label": "G",
              "x": 0.88,
              "y": 0.66,
              "role": "sorted"
            },
            {
              "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"
            },
            {
              "from": "F",
              "to": "H",
              "role": "active"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "stack",
            "ids": [
              "B",
              "H"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "F",
          "discovered": "H",
          "stack": "B H"
        },
        "explanation": "H is a new neighbour of F. Mark it seen and add it to the stack.",
        "stats": {
          "comparisons": 4,
          "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "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,
              "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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "H",
          "order": "A · C · G · F · H"
        },
        "explanation": "Pop H off the top of the stack and visit it. Visit order so far: A · C · G · F · H.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "compare"
            },
            {
              "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",
              "role": "active"
            },
            {
              "from": "F",
              "to": "H"
            }
          ],
          "directed": false,
          "frontier": {
            "label": "stack",
            "ids": [
              "B",
              "E"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "H",
          "discovered": "E",
          "stack": "B E"
        },
        "explanation": "E is a new neighbour of H. Mark it seen and add it to the stack.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "id": "E",
              "label": "E",
              "x": 0.4,
              "y": 0.66,
              "role": "active"
            },
            {
              "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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "E",
          "order": "A · C · G · F · H · E"
        },
        "explanation": "Pop E off the top of the stack and visit it. Visit order so far: A · C · G · F · H · E.",
        "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": "pivot"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "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": "stack",
            "ids": [
              "B"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 7,
        "variables": {
          "visiting": "E",
          "note": "no new neighbours"
        },
        "explanation": "E has no unseen neighbours, so there's nothing to add. Back to the stack.",
        "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": "active"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66
            },
            {
              "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": "stack",
            "ids": []
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E",
            "B"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "B",
          "order": "A · C · G · F · H · E · B"
        },
        "explanation": "Pop B off the top of the stack and visit it. Visit order so far: A · C · G · F · H · E · B.",
        "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": "active"
            },
            {
              "id": "C",
              "label": "C",
              "x": 0.76,
              "y": 0.37,
              "role": "sorted"
            },
            {
              "id": "D",
              "label": "D",
              "x": 0.12,
              "y": 0.66,
              "role": "compare"
            },
            {
              "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",
              "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": "stack",
            "ids": [
              "D"
            ]
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E",
            "B"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "from": "B",
          "discovered": "D",
          "stack": "D"
        },
        "explanation": "D is a new neighbour of B. Mark it seen and add it to the stack.",
        "stats": {
          "comparisons": 7,
          "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": "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": "stack",
            "ids": []
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E",
            "B",
            "D"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 6,
        "variables": {
          "visiting": "D",
          "order": "A · C · G · F · H · E · B · D"
        },
        "explanation": "Pop D off the top of the stack and visit it. Visit order so far: A · C · G · F · H · E · B · D.",
        "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": "stack",
            "ids": []
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E",
            "B",
            "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 stack.",
        "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": "stack",
            "ids": []
          },
          "visited": [
            "A",
            "C",
            "G",
            "F",
            "H",
            "E",
            "B",
            "D"
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "order": "A · C · G · F · H · E · B · D",
          "visited": 8
        },
        "explanation": "The stack is empty, so every reachable node has been visited: A · C · G · F · H · E · B · D. Notice the order plunges down one branch before backing up.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        },
        "accent": "sorted"
      }
    ]
  }
}