Code

The harness and the tests

Three files: the harness, the tests, and two new commands.

command what it does
algo net builds the network and compares BFS with DFS over every reachable destination
algo dense adjacency list against matrix: memory and the cost of a scan

graphbench.go also carries buildTrails — the generator that makes reading trails rather than random pairs. That matters: a random-pair graph has a completely different shape and this lesson's comparison with transit would not hold.

The tests

test what it requires
TestBFSFindsTheShortestPath for every reachable node, BFS's path equals the true distance
TestDFSFindsAPathButNotTheShortest DFS's path is real, never shorter, and sometimes longer
TestBothSearchesAgreeOnReachability all three agree on WHAT is reachable
TestTheTwoDFSVersionsDisagreeOnPaths the two DFS versions find different paths
TestComponentsPartitionTheGraph the components cover every node
TestMatrixAgreesWithList both representations describe the same graph

The first checks against independently computed distances — a separate bfsDistances function in the test file, not against your own BFS's answer. Checking an algorithm with itself is checking nothing.

The second and fourth again demand that something is not the case — a shape that has been familiar since lesson 7. "Fix" DFS so that it returns shortest paths and you have written BFS with extra steps, and the test says exactly that:

DFS found the shortest path every time — that is BFS, not DFS