15
Lesson 14 of 44

Search and backtracking

Learn to solve problems by trying possibilities in an orderly way. In this lesson you will use recursion to generate subsets, permutations and combinations, learn the backtracking outline (choose, explore and undo) with pruning, and apply it to subset sums, well-formed parentheses, N queens, traversing grids and mazes and map coloring.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Exhaustive recursion: the decision tree

Some problems have no formula: the only way to solve them is to try all the possibilities . Generating all the subsets, orderings…

Theory
Read more

Permutations and combinations

Two of the most famous combinatorial problems are ordering elements ( permutations ) and choosing a group of them ( combinations…

Theory
Read more

Backtracking: try, check, undo

Backtracking is exhaustive recursion with a decisive improvement: it abandons a branch as soon as it sees it cannot lead to a…

Theory
Read more

The N queens problem and constraint problems

Placing N queens on an N × N board so that none attack each other is the most famous backtracking example. A queen attacks along…

Theory
Read more

Mazes and grids

Many problems are posed on a grid (a list of strings or lists): mazes, maps, boards. They are traversed with recursion: from a…

Theory
Read more

Practice exercises 10

All subsets

Create a Python program that generates all the subsets of a list. It is the simplest example of exhaustive recursion : for each…

Exercise Medium
Solve it

Permutations of a word

Create a Python program that generates all the permutations (different orderings) of the letters of a word, using backtracking …

Exercise Medium
Solve it

Combinations

Create a Python program that generates all the combinations of k numbers chosen from 1 to n , regardless of order. To avoid…

Exercise Medium
Solve it

Subsets with a target sum

Create a Python program that finds all the subsets of a list whose sum is a given value, with backtracking and pruning : a branch…

Exercise Hard
Solve it

Well-formed parentheses

Create a Python program that generates all the strings of well-formed parentheses with n pairs. With pruning , only valid…

Exercise Hard
Solve it

The N queens problem

Create a Python program that solves the classic N queens problem : place n queens on an n × n board so that none attack each…

Exercise Hard
Solve it

Paths in a grid

Create a Python program that counts in how many ways you can go from the top-left to the bottom-right corner of a grid moving…

Exercise Medium
Solve it

Count islands

Create a Python program that counts the islands of a map and computes the size of the largest. An island is a group of land cells…

Exercise Hard
Solve it

Escape the maze

Create a Python program that finds a path from the entrance to the exit of a maze with backtracking . The path you find depends…

Exercise Hard
Solve it

Color a map

Create a Python program that colors a map (a graph) with k colors so that two neighboring regions never have the same color…

Exercise Hard
Solve it