Backtracking, greedy and dynamic programming

Three ways of attacking a problem: try every choice with undo, commit to the best-looking choice, or store the answers to smaller problems so that nothing is solved twice.

About 12 hours of study

Learning objectives

Lessons

  1. 1

    Recursion and backtracking

    Try every choice, undo it, try the next: all subsets, all permutations, and placing queens that never attack.

  2. 2

    Greedy algorithms

    Take the best-looking choice at each step: activity selection works, coin change sometimes fails, and how to tell.

  3. 3

    Dynamic programming

    Solve each subproblem once and store it: Fibonacci with a memo, and the fewest coins for any amount.

  4. 4

    Knapsack and longest common subsequence

    Two classic 2D tables: the 0/1 knapsack (take or leave each item) and the LCS of two strings.

Module practice

Module quiz

1. Why does backtracking undo a choice after the recursive call?
2. How many permutations do 4 different items have?
3. What does dynamic programming do that plain recursion does not?
4. Activities with (start, finish) are (1,4), (3,5), (0,6), (5,7) and (8,9). How many can one room hold with the earliest-finish rule?
5. With coins {4, 3, 1} and amount 6, how many coins does the greedy rule use?
6. What is the length of the longest common subsequence of AGGT and GXTX?
7. A bag holds weight 5. Items (weight, value) are (2,3), (3,4) and (4,5). What is the best total value for the 0/1 knapsack?

‹ SortingGraph algorithms ›