Graph traversal (BFS)

Computer Science · Algorithms

Question 1 of 10

0

    Graph (undirected). Each neighbour list is in alphabetical order: A: B, C, D B: A, C C: A, B, E D: A, E E: C, D, F F: E Start at A. Neighbours are taken in alphabetical order, and a node counts as visited when it is taken off the queue. In what order are the nodes visited by breadth-first search?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Graph traversal (BFS) explained

    Breadth-first search (BFS) explores a graph level by level. It starts at one node, visits all of its neighbours, then visits the neighbours of those neighbours, and so on, moving outward one step at a time.

    BFS uses a queue, which is first in, first out. A node is added to the back of the queue when it is discovered and removed from the front when it is processed. A record of visited nodes stops the search from going round in circles when the graph has loops. In an unweighted graph, BFS finds the route with the fewest edges from the start to every reachable node, which is why it is used for problems such as finding the shortest chain of links between two people.

    A common mistake is to use a stack instead of a queue. That gives depth-first order, not breadth-first order.

    Worked example

    The graph has the edges A to B, A to C, B to D, C to E, D to F and E to F. Start at A and take neighbours in alphabetical order.

    1. Queue: [A]. Visit A and add B and C. Queue: [B, C].
    2. Remove B, visit it and add D. Queue: [C, D].
    3. Remove C, visit it and add E. Queue: [D, E].
    4. Remove D, visit it and add F. Queue: [E, F].
    5. Remove E. Its neighbour F is already queued, so nothing is added. Remove F.

    The visit order is A, B, C, D, E, F.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Graph (undirected). Each neighbour list is in alphabetical order: A: B, C, D B: A, E C: A, D, E D: A, C E: B, C

      Start at A. Neighbours are taken in alphabetical order, and a node counts as visited when it is taken off the queue. In what order are the nodes visited by breadth-first search?

      • B, A, C, D, E
      • A, B, E, C, D
      • A, D, C, B, E
      • A, B, C, D, E
    • Medium

      Graph (undirected). Each neighbour list is in alphabetical order: A: B, C, E B: A C: A, D, E D: C E: A, C

      Start at A. Neighbours are taken in alphabetical order, and a node counts as visited when it is taken off the queue. Which node is the 5th node visited by breadth-first search?

      • D
      • C
      • E
      • A
    • Hard

      Graph (undirected). Each neighbour list is in alphabetical order: A: B, C, D B: A, C, E C: A, B D: A, F E: B F: D

      The graph is undirected. What is the fewest number of edges on any path from A to F?