AlgoPath Python · Book 5

Python A Level · Part 2DFS, Backtracking and Sorting

Explore one branch at a time with depth-first search. Build backtracking solutions, order dependent tasks and use quicksort before creating a tested study planner.

By AlgoPath Coding Academy
First edition, 2026 · 12 chapters

Python A Level · Part 2: DFS, Backtracking and Sorting — front cover

Who this book is for

Students ready to explore depth-first search, backtracking, graph reasoning and sorting through Python.

Before you begin

Use functions with return values, loops, lists, dictionaries and grid indices confidently. Python GCSE Advanced or equivalent experience provides the starting skills.

An independent programming resource covering selected A level and extension algorithms. Use your own exam board's specification to identify assessment requirements. The online A level course is still coming soon.

Inside the book

The 12 chapters

  1. Understand depth-first search
  2. Follow tree paths and undo choices
  3. Use DFS to work with binary search trees
  4. Generate subsets without repeating results
  5. Build combinations with rules
  6. Arrange values with permutations
  7. Explore graphs and find a route
  8. Find all paths and search word grids
  9. Check connectivity and detect cycles
  10. Order tasks with depth-first search
  11. Partition and sort with quicksort
  12. Build and test a study planner

Work through explanations and examples, try the independent practice, then use the explained answers to review your reasoning.

Try the approach

Explore a branch before returning

This tiny tree is stored as a dictionary of child lists. Children are visited in the order written.

tree = {'A': ['B', 'C'], 'B': ['D'], 'C': [], 'D': []}

def visit(node):
    print(node)
    for child in tree[node]:
        visit(child)

visit('A')

What it prints

A
B
D
C

The search finishes B's branch, including D, before returning to A and visiting C. This example assumes a tree with no cycles; general graphs need additional visited-state handling.

This short public example introduces an idea explored in the book.