Breadth-First and Depth-First Search · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Two ways to walk a graph

Computing · Algorithms & Data Structures · ages 17-18
Name ______________________   Date ____________
  1. How does depth first search order its visits?

    • Level by level in waves
    • Down one path as far as it goes
    • Alphabetical by node name
  2. How does breadth first search order its visits?

    • Down one path, then backtrack
    • Level by level, neighbours first
    • Largest nodes first
  3. Which structure forces the level by level order?

    • A stack
    • A visited set alone
    • A queue
  4. Visited nodes must be recorded, or a cycle loops the search forever.

    Circle one:   True   False

  5. You swap the queue for a stack in the same search loop. What changes?

    • Breadth first becomes depth first
    • The graph gains new edges
    • Cycles vanish on their own
  6. Breadth first search reaches a node for the first time. What do you know?

    • It arrived by a shortest hops route
    • It arrived by the longest route
    • It arrived with heaviest weights
  7. You need the fewest hops route on an unweighted graph. Which do you pick?

    • Depth first search
    • Dijkstra on weights
    • Breadth first search
  8. Start at A. Neighbours: A: B, C. B: D. C: D. D: none. Which order is breadth first from A?

    • A, B, D, C
    • A, B, C, D
    • A, D, C, B
LightMySky · lightmysky.comW1-mt_q8IEzoBhzy-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Two ways to walk a graph W1-mt_q8IEzoBhzy-s1

  1. Down one path as far as it goes · A stack keeps serving the newest node, which dives deep.
  2. Level by level, neighbours first · Waves out from the start cover near nodes before far ones.
  3. A queue · Serving the oldest waiting node spreads in waves.
  4. True · The record stops the same nodes queueing again and again.
  5. Breadth first becomes depth first · Newest first dives, oldest first spreads.
  6. It arrived by a shortest hops route · Waves reach near nodes before any longer route can.
  7. Breadth first search · Only the wave order promises fewest hops first.
  8. A, B, C, D · Both neighbours of A come before the next level.
Worksheet · LightMySky