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…
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.
Some problems have no formula: the only way to solve them is to try all the possibilities . Generating all the subsets, orderings…
Two of the most famous combinatorial problems are ordering elements ( permutations ) and choosing a group of them ( combinations…
Backtracking is exhaustive recursion with a decisive improvement: it abandons a branch as soon as it sees it cannot lead to a…
Placing N queens on an N × N board so that none attack each other is the most famous backtracking example. A queen attacks along…
Many problems are posed on a grid (a list of strings or lists): mazes, maps, boards. They are traversed with recursion: from a…
Create a Python program that generates all the subsets of a list. It is the simplest example of exhaustive recursion : for each…
Create a Python program that generates all the permutations (different orderings) of the letters of a word, using backtracking …
Create a Python program that generates all the combinations of k numbers chosen from 1 to n , regardless of order. To avoid…
Create a Python program that finds all the subsets of a list whose sum is a given value, with backtracking and pruning : a branch…
Create a Python program that generates all the strings of well-formed parentheses with n pairs. With pruning , only valid…
Create a Python program that solves the classic N queens problem : place n queens on an n × n board so that none attack each…
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…
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…
Create a Python program that finds a path from the entrance to the exit of a maze with backtracking . The path you find depends…
Create a Python program that colors a map (a graph) with k colors so that two neighboring regions never have the same color…