Skip to content

Breadth-first search

networks

Level 2 of 3

Also called BFS.

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 and F-G. How many roads does it look along?

A0BEFGA
the answer, and why

the answer is C

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

if you chose another

  • That counts each road once, but the walk looks along it from both ends.
  • That counts the places found, B, E, F and G, not the roads looked along.
  • That misses one look. G has degree 1, so it looks along its road too.

question 2

On the town of eight from A, which place leaves the queue fourth?

A0BCDEFGHA
the answer, and why

the answer is B

A leaves first and finds B, then E. B leaves second and finds C, then F. E leaves third, so C is fourth.

if you chose another

  • E joins third, after A and B, so it leaves third.
  • F joins after C, so it leaves after C.
  • D is 3 roads from A, found only when C leaves.

question 3

Which is true of breadth-first search on a map of nn places in one piece?

the answer, and why

the answer is B

In one piece the map has at least n−1n - 1 roads, and each is looked along twice, the last line of section 4's question on a line.

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

if you chose another

  • A line looks along 2(n−1)2(n - 1), and every pair joined n(n−1)n(n - 1).
  • Each road is looked along from both its ends.
  • Every pair joined costs n(n−1)n(n - 1), which is Θ(n2)\Theta(n^2).

question 4

The walk starts at A on each map. Which one makes it look along the most roads?

the answer, and why

the answer is C

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

if you chose another

  • 3 roads, 6 looks.
  • 4 roads, 8 looks.
  • It has 5 roads, but A's piece holds only A-B, so the walk looks along 2.

pen and paper

question 5about 8 minutes

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

  1. Write the queue after each place is taken.
  2. What number does each place get?
  3. How would you number C, D, G and H too?
a hint

E has one road, to A. Places on the other piece are never found from E.

a worked answer

E is taken and finds A: queue A. A finds B: queue B. B finds F: queue F. F finds nothing new: queue empty. E is 0, A 1, B 2, F 3. C, D, G and H get no number. Start a new walk at C, the first place not found: C 0, D 1, G 1, H 2.

ABCDEFGH
The second walk, from C, numbers the shaded piece.

this tests how it works, on the learn page

what a full answer includes
  • Queues A, then B, then F, then empty.
  • E 0, A 1, B 2, F 3; C, D, G and H none.
  • A new walk from C: C 0, D 1, G 1, H 2.

question 6about 6 minutes

A map is a line of nn places, each joined by a road to the next.

  1. How many roads does it have?
  2. Why does the walk look along each road twice?
  3. Why does it look along 2(n−1)2(n - 1) roads?
a hint

Each place after the first adds one road. Then use the degrees.

a worked answer

The first place needs no road, and each place after it adds one: n−1n - 1 roads. In one piece every place is taken, and each looks along all its roads, so each road is looked along from both ends. Twice n−1n - 1 is 2(n−1)2(n - 1).

Why does a line of nn places cost 2(n−1)2(n - 1) looks, the fewest for a map in one piece?
  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}
    ABCDE
    5 places, 4 roads, in one piece. Take any road away and it falls into two pieces.

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

  2. the move: twice each road

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

    why it is allowed: KK is the looks on a line. By the first question each road is looked along twice, and by line 1 there are n−1n - 1 roads.

  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.

  4. the move: open the bracket

    the line:
    K=2n−2K= 2n - 2

    why it is allowed: Twice nn, less twice 1. For 5, 10 less 2.

  5. the move: a ceiling

    the line:
    K≤2nK\le 2n

    why it is allowed: By line 4, without the 2 taken away.

  6. the move: read it as a bound

    the line:
    K∈O(n)K\in O(n)

    why it is allowed: By line 5, with the fixed multiple 2.

  7. the move: a floor

    the line:
    K≥nK\ge n

    why it is allowed: By line 4, 2n−22n - 2 is nn plus n−2n - 2, which is not below 0 from 2 places on. For 5, 8 is at least 5.

  8. the move: read it as a bound

    the line:
    K∈Ω(n)K\in \Omega(n)

    why it is allowed: By line 7, with the fixed multiple 1, from 2 places on.

  9. the move: ceiling and floor together

    the line:
    K∈Θ(n)K\in \Theta(n)

    why it is allowed: Lines 6 and 8 bound KK by the same growth, nn.

  10. the move: a line has the fewest roads

    the line:
    K=2(n−1)K= 2(n - 1)
    where does this come from?
    question 3 of the page graphs and their wordsnn places in one piece, on two-way roads. How few roads can join them?
    1. the move: start with no roads

      the line:
      no roads:n pieces\begin{gathered} \text{no roads:} \\ n \text{ pieces} \end{gathered}

      why it is allowed: With no roads, each place is a piece on its own.

    2. the move: a road joins two pieces at most

      the line:
      each road:at most one piece fewer\begin{gathered} \text{each road:} \\ \text{at most one piece fewer} \end{gathered}
      ABCDEFG
      7 places and 6 roads, yet two pieces. Laid after A to B, A to C and B to D, the road C to D joins two places already in one piece, so it joins nothing new.

      why it is allowed: A road between two pieces makes them one. A road inside one piece joins nothing new.

    3. the move: count the pieces left

      the line:
      after k roads:at least n−k pieces\begin{gathered} \text{after } k \text{ roads:} \\ \text{at least } n - k \text{ pieces} \end{gathered}
      roads laidroadpieces
      0none yet7
      1A to B6
      2A to C5
      3B to D4
      4C to D4
      5E to F3
      6E to G2
      The 7 places above, their 6 roads laid one at a time. The fourth, C to D, takes no piece away. 6 roads leave 2 pieces.

      why it is allowed: Lay the roads one at a time, and let kk be how many are laid. Start at nn pieces and take off at most 1 for each road.

    4. the move: ask for one piece

      the line:
      one piece:n−k is at most 1\begin{gathered} \text{one piece:} \\ n - k \text{ is at most 1} \end{gathered}

      why it is allowed: Line 3's pieces can be 1 only if n−kn - k is 1 or less.

    5. the move: turn it round

      the line:
      k is at least n−1\begin{gathered} k \text{ is at least } n - 1 \end{gathered}

      why it is allowed: When n−kn - k is at most 1, kk falls short of nn by at most 1. So fewer than n−1n - 1 roads always leave two pieces or more. For 6 places, 4 roads take off at most 4 of the 6 pieces, so at least 2 are left.

    6. the move: a line of places is enough

      the line:
      join each place to the next:n−1 roads, one piece\begin{gathered} \text{join each place to the next:} \\ n - 1 \text{ roads, one piece} \end{gathered}
      ABCDEF
      6 places, each joined to the next, drawn heavy. 5 roads hold them in one piece.

      why it is allowed: Each place after the first has one road back to the one before it. That is n−1n - 1 roads, and every place reaches the first.

    7. the move: the fewest

      the line:
      R=n−1R = n - 1

      why it is allowed: Let RR be the fewest roads. Line 5 says no fewer than n−1n - 1 will do, and line 6 that n−1n - 1 do.

    the whole page: graphs and their words

    why it is allowed: Line 3 again. This line's fold shows that nn places in one piece need at least n−1n - 1 roads, and by line 1 a line has exactly n−1n - 1, so it has the fewest.

  11. the move: any map in one piece

    the line:
    any map in one piece:m is at least n−1\begin{gathered} \text{any map in one piece:} \\ m \text{ is at least } n - 1 \end{gathered}

    why it is allowed: By line 10's fold.

  12. the move: double both

    the line:
    2m is at least 2(n−1)2m \text{ is at least } 2(n - 1)

    why it is allowed: Twice each side of line 11.

  13. the move: compare with the line

    the line:
    its 2m looks areat least K\begin{gathered} \text{its } 2m \text{ looks are} \\ \text{at least } K \end{gathered}

    why it is allowed: By the first question it looks along 2m2m roads, and by line 3 KK is 2(n−1)2(n - 1). So by line 12 no map in one piece costs less than a line.

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
  • n−1n - 1 roads.
  • Every place taken, every road from both ends.
  • Twice the roads.