Theory

Two searches on an eight-node graph

There is no code here. This is what the two searches do — step by step, on the same small graph.

First, how to store a graph. There are two ways and they are not equivalent.

The graph we will watch

Eight books and eight edges:

   0 ───── 1
   │       │
   2       6
   │       │
   3 ─ 4 ─ 5
   │
   7

A ring of seven nodes and one dead end — 7 hangs off 3.

An adjacency list

For each node, a list of its neighbours:

adj[0] = [1 2]
adj[1] = [0 6]
adj[2] = [0 3]
adj[3] = [2 4 7]
adj[4] = [3 5]
adj[5] = [4 6]
adj[6] = [5 1]
adj[7] = [3]

Memory: O(V + E). Visiting the neighbours: as many steps as there are neighbours.

The order of the neighbours matters here: it is the order the edges were added in. Both searches will walk neighbours in exactly that order — or backwards.

An adjacency matrix

One bit for every pair of nodes, edge or no edge:

     0 1 2 3 4 5 6 7
  0  . 1 1 . . . . .
  1  1 . . . . . 1 .
  2  1 . . 1 . . . .
  3  . . 1 . 1 . . 1
  4  . . . 1 . 1 . .
  5  . . . . 1 . 1 .
  6  . 1 . . . 1 . .
  7  . . . 1 . . . .

Memory: O(V²). The question "is there an edge A–B?" takes one step, with no searching. But visiting the neighbours takes V steps, even when there are none.

In lesson 2 you worked out what slice headers would cost transit. That was an argument, not a measurement, and that lesson's gotcha said so plainly. Step 8 of this lesson measures the same question.

How we count

Both searches here look for a path from 0 to 6. Each row shows what was taken, what is left waiting, and how many edges were looked at. It is the same counter you will see in code as c.Hit(): one edge, one hit, whether or not its other end was already seen.

BFS — breadth-first

Take from the front, put neighbours at the back. A node is marked seen the moment it is put in.

start            queue [0]

take 0           neighbours 1 2    both new      queue [1 2]       2
take 1           neighbours 0 6    0 seen        queue [2 6]       2
take 2           neighbours 0 3    0 seen        queue [6 3]       2
take 6           that is the target — STOP
                                                 total:            6
path             0 → 1 → 6        2 hops

A queue visits in layers. [1 2] is everything one hop away; [6 3] is everything two hops away. By the time 6 reaches the front, every shorter layer has been exhausted.

DFS — depth-first

Take from the back, put neighbours at the back as well. A node is marked seen when it is taken.

start            stack [0]

take 0           neighbours 1 2    both new      stack [1 2]       2
take 2           neighbours 0 3    0 seen        stack [1 3]       2
take 3           neighbours 2 4 7  2 seen        stack [1 4 7]     3
take 7           neighbour 3       3 seen        stack [1 4]       1   dead end
take 4           neighbours 3 5    3 seen        stack [1 5]       2
take 5           neighbours 4 6    4 seen        stack [1 6]       2
take 6           that is the target — STOP
                                                 total:            12
path             0 → 2 → 3 → 4 → 5 → 6      5 hops

A stack goes as deep as it can: 0 → 2 → 3, then into the dead end 7, back to 4, and only then 5, 6.

The same numbers side by side

0 → 6 hops edges looked at the waiting collection
BFS 2 6 queue
DFS 5 12 stack

Three observations. Worth not skipping, because the next steps are made of nothing else.

1. BFS's path is the shortest, and the table shows why. A queue visits in layers: everything one hop away first, then everything two hops away. When the target first reaches the front, there is no shorter layer left. The first find is the shortest.

2. DFS took the long way round although 1 was right next door. Node 1 is a direct neighbour of 0, and it sits at the bottom of the stack from the very first step. The stack reaches it last: everything above it has to be explored first. Five hops against two, twice the edges — on a graph of eight nodes. In the library the same thing is 214 hops against 1.

3. The cost is the same: every edge is looked at at most twice — once from each end. This graph has eight edges, so even the worst case would be 16 looks; BFS made 6, DFS made 12. The total cost for both is O(V + E).

The complexity is the same. What differs is not the cost but the answer.

What comes next

Now you know what both searches do. In the next step you write one of them as Go code — the one that answers "in how many hops".