I'm trying to get the hang of dynamic programming, and was reading the tutorial here:
https://www.topcoder.com/community/data ... -advanced/
For the first beginner exercise, I tried to just think of the problem at first, before reading ahead, and think of how I might solve the problem. I found that my approach was not the same as what's described, and I was hoping to get advice for how to recognize the benefits of constructing a solution "bottom-up" vs. "top-down".
Specifically, the first question is a DP problem such that, given a list of N coins with values (V1, V2, ..., Vn) and a total sum S, find the minimum number of coins that can be used to get a sum of S.
I stopped here, and thought about how I'd try to solve the problem.
Taking their example of values (1, 3, 5) and S=11, my general approach would be as follows:
1.) See if there are any values V that are less than 11. If not, return -1.
2.) Check a hash table (for storing intermediate results) to see if I already have minimum sums for S=10, S=8, or S=6. If the hash table has no values, recursively get the minimum number of coins required for sum (11-1=10), (11-3=8), and (11-5=6), and store the results in the hash table. If the return value of any of these were unsatisfiable (i.e. -1), omit from the set.
3.) Get the minimum of the values returned from the recursive calls, and add 1. That's my minimum for the current sum S=11.
Obviously, this is different from the approach given in the article. Intuitively, I try to approach the problem "top-down". I try to start at the top sum S=11, simulate using that coin, and get the result for S-Vi for each value Vi.
The article, rather, describes a "bottom-up" approach of using states: first identify state S=0, then build on that to create state S=1, all the way up to the state for the final solution.
I have two questions:
1.) Is my approach infeasible? What are the benefits of starting "bottom-up" rather than from the "top-down"?
2.) Intuitively, I see the problem as one that requires a "top-down" approach. What aspects of a problem can I look for that would suggest a "bottom-up" approach is better?
dynamic programming: noob question
-
Kirby
- Honinbo
- Posts: 9553
- Joined: Wed Feb 24, 2010 6:04 pm
- GD Posts: 0
- KGS: Kirby
- Tygem: 커비라고해
- Has thanked: 1583 times
- Been thanked: 1707 times
-
dfan
- Gosei
- Posts: 1598
- Joined: Wed Apr 21, 2010 8:49 am
- Rank: AGA 2k Fox 3d
- GD Posts: 61
- KGS: dfan
- Has thanked: 891 times
- Been thanked: 534 times
- Contact:
Re: dynamic programming: noob question
Your approach is fine. It also avoids computing information that you don't need (you solve only the subproblems relevant to your actual problem). When I used DP for a couple of problems in the first L19 programming contest, I used your top-down approach.
Which approach I would use on any given problem varies. If I know that I'm going to need to solve every subproblem anyway, I might go bottom-up and not worry about memoization and recursion. If I knew that only a very small number of subproblems were relevant to any given top-level problem, I would definitely go top-down. For example, I would never go bottom-up on the peg-jumping problem from the first week.
Which approach I would use on any given problem varies. If I know that I'm going to need to solve every subproblem anyway, I might go bottom-up and not worry about memoization and recursion. If I knew that only a very small number of subproblems were relevant to any given top-level problem, I would definitely go top-down. For example, I would never go bottom-up on the peg-jumping problem from the first week.
- Solomon
- Gosei
- Posts: 1848
- Joined: Tue Apr 20, 2010 9:21 pm
- Rank: AGA 5d
- GD Posts: 0
- KGS: Capsule 4d
- Tygem: 치킨까스 5d
- Location: Bellevue, WA
- Has thanked: 90 times
- Been thanked: 835 times
Re: dynamic programming: noob question
I'd highly recommend getting "Competitive Programming 3" by Steven and Felix Halim for answers to these kinds of questions
. Here is what they wrote regarding top-down vs bottom-up approaches to DP (screenshot is taken from the first edition, which is available for free on their site so it should be legal):


-
Kirby
- Honinbo
- Posts: 9553
- Joined: Wed Feb 24, 2010 6:04 pm
- GD Posts: 0
- KGS: Kirby
- Tygem: 커비라고해
- Has thanked: 1583 times
- Been thanked: 1707 times
Re: dynamic programming: noob question
Thanks. It's a relief to know that my method might still work. I think it's a good point to watch out for stack overflows with top down approach.
be immersed
- EdLee
- Honinbo
- Posts: 8859
- Joined: Sat Apr 24, 2010 6:49 pm
- GD Posts: 312
- Location: Santa Barbara, CA
- Has thanked: 349 times
- Been thanked: 2070 times
- fwiffo
- Gosei
- Posts: 1435
- Joined: Tue Apr 20, 2010 6:22 am
- Rank: Out of practice
- GD Posts: 1104
- KGS: fwiffo
- Location: California
- Has thanked: 49 times
- Been thanked: 168 times