Path: blob/master/python/algorithms/recursion.ipynb
1480 views
Recursion, Greedy Algorithm, Dynamic Programming
Following the online book, Problem Solving with Algorithms and Data Structures. Chapter 5 discusses recursion.
Recursion
Is a method of breaking problems down into smaller and smaller sub-problems until we get to a problem small enough that it can be solved trivially. Recursion must follow three important laws.
It must have a base case
It must call itself recursively
It must change it's state and move towards the base case
The first problem will be Converting an Integer to a String in Any Base.
Greedy Algorithm
Changing money is an optimization problem involves making change using the fewest coins. e.g. The answer for making a change for 63 cents will be 6 coins: two quarters, one dime, and three pennies. How did we arrive at the answer of six coins? Well, one approach will be using a greedy method. Meaning we start with the largest coin in our arsenal (a quarter) and use as many of those as possible, then we go to the next lowest coin value and use as many of those as possible and keep going until we've arrived at our solution. This first approach is called a greedy method because we try to solve as big a piece of the problem as possible right away.
The greedy method works fine when we are using U.S. coins, but suppose, in addition to the usual 1, 5, 10, and 25 cent coins we now have a 21 cent coin. In this instance our greedy method fails to find the optimal solution for 63 cents in change. With the addition of the 21 cent coin the greedy method would still find the solution to be 6 coins when the optimal answer should be 3 21 cent pieces.
Dynamic Programming - Changing Coin
Let’s look at a method called dynamic programming, where we could be sure that we would find the optimal answer to the problem. Dynamic programming solution is going to start with making change for one cent and systematically work its way up to the amount of change we require. This guarantees us that at each step of the algorithm we already know the minimum number of coins needed to make change for any smaller amount.
Let’s look at how we would fill in a table of minimum coins to use in making change for 11 cents. The following figure illustrates the process.
We start with one cent. The only solution possible up till this point is one coin (a penny). The next row shows the minimum for one cent and two cents. Again, the only solution is two pennies. The fifth row is where things get interesting. Now we have two options to consider, five pennies or one nickel. How do we decide which is best? We consult the table and see that the number of coins needed to make change for four cents is four, plus one more penny to make five, equals five coins. Or we can look at zero cents plus one more nickel to make five cents equals 1 coin. Since the minimum of one and five is one we store 1 in the table. Fast forward again to the end of the table and consider 11 cents. The three options that we have to consider:
A penny plus the minimum number of coins to make change for 11−1=10 cents (1)
A nickel plus the minimum number of coins to make change for 11−5=6 cents (2)
A dime plus the minimum number of coins to make change for 11−10=1 cent (1)
Either option 1 or 3 will give us a total of two coins which is the minimum number of coins for 11 cents.
Dynamic Programming - 0/1 Knapsack
The following blog has a nice introduction on this topic. Blog: Dynamic Programming: Knapsack Optimization