Backtracking
A way to find every solution by trying options one at a time: you move forward with a choice and, if it leads nowhere, undo it and try another.
Example
To open a 3-digit lock without the code, you try 0-0-0, 0-0-1... and each time it fails you change the last digit: up to 1,000 tries.
Test yourself: Backtracking
Backtracking
Backtracking is an ___ search: it tries every possible option.
A 3-digit lock has 1,000 possible codes; backtracking can try them all until it hits the right one.
Backtracking
Building a word with tiles, you place a letter; if it does not work, you ___ it and try another.
With «CA» and 3 free tiles, you try «CAB», it fails, you remove it and try «CAT»: the word goes back to «CA».
Backtracking
To find paths that continue below a 7, the return after saving the solution must be removed.
With return it would stop at the first 7; without it, if there is a 7 below another 7, both paths are saved.
0/3
â–¶ PlayLessons that use it
Iteration and recursion
2 ways to repeat
Walking a graph
BFS and DFS, now with cycles
Backtracking
Try, and if it fails, go back
Permutations
Every possible order
Subset sum
Groups that add up exactly
N-queens
None attacks another
When DP works
3 properties
Solving with DP
State, transition and borders
Greedy algorithms
The best right now
