Code

Dijkstra without a heap, path cost, BFS — write them yourself

Dijkstra on the heap works. The remaining three things come without holes this time: empty skeletons, and you fill them in completely.

Dijkstra without a heap

The same Dijkstra, with no priority queue at all. To find the cheapest, every node is looked at every time.

DijkstraScan(from, to):
    dist[*] ← ∞;  dist[from] ← 0
    repeat:
        best ← the unsettled node with the smallest dist     // looks at ALL
        if there is none (dist = ∞): return "no route"
        done[best] ← true
        if best = to: return the path and dist[best]
        for each edge (best → w, cost c):
            if dist[best] + c < dist[w]:
                dist[w] ← dist[best] + c
                prev[w] ← best

The difference from DijkstraHeap is only how the cheapest is found: no heap, and no stale entries. So there is no done check before that.

The counter: one c.Hit() for every node looked at, settled ones included — every choice costs Nodes(). That is its "work". The test requires the table's number: 6 choices of 6 nodes = 36.

DijkstraScan is not for fixing. It is slow on purpose: step 6 is about how slow, and why the clock and the counter disagree.

Path cost and BFS on a weighted graph

PathCost adds up the edge costs along a path. It is the same comparison the first step made: lesson 14's answer "in how many hops" and this lesson's "in how many days", in the same unit.

BFSW is lesson 14's BFS moved unchanged onto WGraph. It still counts hops and ignores the costs entirely. That is exactly why it is here: so you can show what the ignoring costs. In the table: 0 → 4 — route [0 2 4], 13 days, when the cheapest takes 11.

Your job

wgraph.go (in the panel) has grown: DijkstraScan, PathCost and BFSW have been added, each with panic("not implemented").

DijkstraHeap in the panel still shows its holes — the panel shows the skeleton, not your file. In your copy they are already filled in; leave them that way.

wgraph_test.go has grown too:

test what it requires
TestDijkstraScanReproducesTheTable 0 → 4: path [0 2 1 3 4], cost 11, 6 nodes, work 36
TestBFSWReproducesTheTable 0 → 4: path [0 2 4], cost 13
TestBothFrontiersReturnTheSameCost both queues return an identical cost for every destination; the path adds up and matches
TestFewestHopsIsNotCheapest BFS's route is never cheaper and sometimes dearer

The third matters most, not for what it checks but for what it licenses. In step 6 you compare two queues — the comparison is meaningful only if they give the same answer. So that is checked first.

Run go test ./...; as you write, it stops at the first unfinished skeleton with panic: not implemented, so check one at a time:

$ go test -run 'Scan' ./...

When it does not pass

  • work=21, when 36 was wanted (DijkstraScan) — you count only unsettled nodes. The scan walks all of them, settled ones too; that is its price.
  • path=[4] cost=11 — the cost is right, the route is not: you did not record prev[w] = best.
  • BFSW 0 to 4: … cost=2, when 13 was wanted — PathCost is counting hops, not days. An edge's cost is e.Cost.