Graph traversal (DFS)

Computer Science · Algorithms

Question 1 of 10

0

    Graph (undirected). Each neighbour list is in alphabetical order: A: B, C B: A, D C: A, D, E D: B, C, E E: C, D Start at A. Recursive depth-first search visits neighbours in alphabetical order and only calls itself on a neighbour that is not yet visited. A node counts as visited when its call starts. In what order are the nodes visited by recursive depth-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 (DFS) explained

    Depth-first search (DFS) explores a graph by going as deep as it can along one path before backtracking to try other branches. From the start node it moves to an unvisited neighbour, then to a neighbour of that node, and keeps going until it reaches a node with no unvisited neighbours.

    DFS is often written with recursion, which uses the call stack, or with an explicit stack. Like BFS, it needs a record of visited nodes so that it does not loop forever on cycles. DFS is useful for checking whether a path exists, detecting cycles, and ordering tasks that depend on each other. It does not find the shortest route in an unweighted graph, because it may follow a long path when a shorter one exists.

    A common mistake is to think DFS always finds the shortest path. For that, BFS is the right choice.

    Worked example

    Use the same graph as the BFS example: edges A to B, A to C, B to D, C to E, D to F and E to F. Start at A, take neighbours in alphabetical order, and use recursion.

    1. Visit A. Its first unvisited neighbour is B, so go to B.
    2. Visit B. Its neighbours are A (visited) and D, so go to D.
    3. Visit D. Its neighbours are B (visited) and F, so go to F.
    4. Visit F. Its neighbours are D (visited) and E, so go to E.
    5. Visit E. Its neighbour C is unvisited, so go to C.
    6. Visit C. Both of its neighbours are already visited, so the search backtracks and finishes.

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

    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, D B: A, C C: B D: A

      Start at A. Recursive depth-first search visits neighbours in alphabetical order and only calls itself on a neighbour that is not yet visited. A node counts as visited when its call starts. In what order are the nodes visited by recursive depth-first search?

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

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

      Start at A. Recursive depth-first search visits neighbours in alphabetical order and only calls itself on a neighbour that is not yet visited. A node counts as visited when its call starts. Which node is the 4th node visited by recursive depth-first search (A is the 1st)?

      • D
      • E
      • B
      • C
    • Hard

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

      Start at A. Recursive depth-first search visits neighbours in alphabetical order and only calls itself on a neighbour that is not yet visited. A node counts as visited when its call starts. The call for A is at depth 1, and each nested call is one level deeper. What is the greatest depth reached?