CodeRigor

LESSON

Export a traversal and replay it visually

Serialize real search snapshots into bounded JSON and drive a step/pause/reset visualizer without executing code in the browser.

An animation should explain the algorithm that actually ran. Export its copied snapshots instead of implementing a second search inside the renderer. This capstone produces versioned JSON that the inline visualizer and offline viewer can replay.

Choose BFS or DFS and a supplied scenario, then use Step, Play/Pause and Reset. The fork intentionally shows a two-move BFS route and an eight-move DFS route. The unreachable scenario shows exploration without inventing a solution. The one-cell scenario shows a zero-move path.

Tested model → replay data → viewer
start / 3
The Python model exports actual snapshots and a parent route. The viewer parses bounded JSON and displays it without executing Python.
Tested model → replay data → viewer
The Python model exports actual snapshots and a parent route. The viewer parses bounded JSON and displays it without executing Python.

JSON supports arrays, objects with string keys, numbers, strings, booleans and null. Convert coordinate tuples to arrays. Represent the parent map as an ordered list of cell/parent records; a tuple-keyed dict cannot be JSON object keys without changing its meaning. Keep root parent and no-current fields as None, which serializes as null.

Each event includes frontier, expanded and discovered arrays. Because snapshots were copied at capture time, ready remains ready after the algorithm finishes. The viewer reveals the reconstructed path only at the final result.

Python
import json
parents = {(0,0):None, (0,1):(0,0)}
records = [{"cell":list(c), "parent":list(p) if p is not None else None} for c,p in parents.items()]
print(json.dumps({"version":1,"parents":records},separators=(",",":")))

The lab includes a standard-library CLI, fixtures, tests and an offline HTML viewer. Export with python traversal_cli.py --algorithm bfs --output trace.json, open viewer.html and load that JSON. Repeat with dfs. There is no network dependency, GUI package, account or cloud cleanup.

Uploaded files are data. The shared viewer validates size, schema, coordinate bounds, snapshot partitions, parent edges and path legality, and renders labels as text. It never evaluates uploaded strings or starts a process. Structural validation is not an independent proof that arbitrary uploaded data was produced by a correct BFS or DFS; the hosted model tests supply that algorithm evidence.

Text labels accompany colors: S start, G goal, # wall, F frontier, E expanded, D discovered and P path. The status reports event index, current cell, expansion count and final path moves. Animation starts only when you choose Play, and resets/selection changes stop it.

Controls change the replay index
start / 3
The viewer changes which exported snapshot is visible. It cannot modify the original graph search or grant course mastery.
Controls change the replay index
The viewer changes which exported snapshot is visible. It cannot modify the original graph search or grant course mastery.
Python
frames = [{"kind":"ready"},{"kind":"expand"},{"kind":"done"}]
index = 0
for _ in range(5):
    index = min(index + 1, len(frames) - 1)
    print(index, frames[index]["kind"])

Complete the exporter, round-trip it through json.dumps/loads, then load the result in the viewer. Run the model and viewer tests. Check start/goal, legal moves, exact path length and no duplicate expansions. Reset must return to the initial frame; pause must leave the index still.

Extend the lab with a new wall fixture and an independently justified expected distance. For weighted terrain, choose and teach a cost-aware algorithm before claiming shortest total cost.

Download the complete lab. The archive includes runnable code, tests, fixtures and a README.

Python BFS and DFS export discovery, expansion and parent-path evidence for an interactive replay.

Enable JavaScript to step, play/pause and reset the supplied search traces, or download the Python lab and offline viewer.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Optional graph-search companion; all grid code and explanations are included here.