Skip to content

Depth-first search

networks

Level 2 of 3

Also called DFS.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

The walk starts at A on the map A-B, A-E, B-F, E-F, B-C, C-G and F-G. How many looks does it make?

A1BCEFGA
the answer, and why

the answer is D

The map is in one piece with 7 roads, and each is looked along from both ends, 14 in all.

if you chose another

  • That counts each road once, but each road gets a look from both ends.
  • That counts the places, A, B, C, E, F and G, not the looks.
  • That adds the 6 places coming off, but coming off is no look.

question 2

On the town of eight from A, how many places has the walk reached when it first steps back?

A1BCDEFGHA
the answer, and why

the answer is B

It reaches A, B, C and D, four places. D's only road leads back to C, so D comes off, and the walk steps back to C.

if you chose another

  • 3 is the stack after the step back, A, B and C. D was reached too.
  • 8 is the whole town, but E, F, G and H are not reached yet.
  • The fifth place, G, is reached after the step back.

question 3

Depth-first and breadth-first search each start at A on the same map in one piece. Which is true?

the answer, and why

the answer is C

On a map in one piece each makes 2m2m looks, by section 4's first question, lines 6 and 7.

this tests where the formula comes from, on the learn page

if you chose another

  • A step back takes a place off the stack, and is no look.
  • A queue's length changes what is waiting, not how many looks there are.
  • Only breadth-first's numbers are fewest roads. Depth-first's say the order reached.

question 4

The walk starts at the place after the semicolon on each map. On which map does it make the most looks?

the answer, and why

the answer is A

A-B, B-C, C-D, A-E, B-F is in one piece with 5 roads, 10 looks. A-B, B-C, C-G has 3 roads, 6 looks, and A-B, A-E, A-F, E-F 4 roads, 8.

if you chose another

  • 3 roads, 6 looks.
  • 4 roads, 8 looks.
  • It has 6 roads, but E's piece holds only E-F, so the walk makes 2 looks.

pen and paper

question 5about 8 minutes

The map A-B, A-E, C-D and F-G is in three pieces. The walk starts at E.

  1. Write the stack after each place goes on or comes off.
  2. What number does each place get?
  3. How would you number C, D, F and G too, and how many looks do all the walks make?
a hint

E has one road, to A. Places on the other pieces are never reached from E.

a worked answer

Stacks, bottom first: E; then E, A; then E, A, B; then E, A; then E; then empty. E is 1, A 2, B 3. C, D, F and G get no number from this walk. Start a new walk at C, numbering from 1: C 1, D 2. Then another at F: F 1, G 2. Three walks, three pieces. Each of the 4 roads gets 2 looks, 8 looks in all.

A2B3C1D2E1F1G2
Each place shows its letter and its number. The new walks, from C and from F, number the shaded pieces.

this tests how it works, on the learn page

what a full answer includes
  • Stacks E; E, A; E, A, B; E, A; E; empty.
  • E 1, A 2, B 3; C, D, F and G none.
  • New walks from C and F; 8 looks.

question 6about 6 minutes

A map is a line of nn places, each joined by a road to the next. The walk starts at one end.

  1. How many places are on the stack when it reaches the far end?
  2. How many times does it step back?
  3. Why does it make 2(n−1)2(n - 1) looks?
a hint

Nothing comes off before the far end. Then use the degrees.

a worked answer

Nothing comes off before the far end, so all nn are on the stack. Then each comes off in turn, and every one but the start steps back to the place below: n−1n - 1 step backs. Each place looks along all its roads, so each of the n−1n - 1 roads gets 2 looks. Twice n−1n - 1 is 2(n−1)2(n - 1).

Why does a line of nn places make 2(n−1)2(n - 1) looks?
  1. the move: a line's roads

    the line:
    a line of n places:n−1 roads\begin{gathered} \text{a line of } n \text{ places:} \\ n - 1 \text{ roads} \end{gathered}

    why it is allowed: The first place needs no road, and each place after it adds one.

  2. the move: twice each road

    the line:
    K=2+2+⋯+2K= 2 + 2 + \dots + 2

    why it is allowed: KK is the looks. Every place comes off with no road left, so each of line 1's roads is looked along from both ends.

  3. the move: add the twos

    the line:
    K=2(n−1)K= 2(n - 1)

    why it is allowed: n−1n - 1 twos make twice n−1n - 1. For 5 places, 8.

try it with a small nn: at n=5n = 5, line 2 gives K=8K = 8, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • All nn places.
  • n−1n - 1 step backs.
  • Every road from both ends, twice n−1n - 1.