






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







JOIN LOG IN
DESIGN CHALLENGES
DEVELOPMENT CHALLENGES
DATA SCIENCE CHALLENGES
COMPETITIVE PROGRAMMING
GETTING STARTED
DESIGN
DEVELOPMENT
DATA SCIENCE
COMPETITIVE PROGRAMMING
OVERVIEW
PROGRAMS
FORUMS
STATISTICS
EVENTS
BLOG
By Dumitru — topcoder member Discuss this article in the forums
An important part of given problems can be solved with the help of dynamic programming (DP for short). Being able to tackle problems of this type would greatly increase your skill. I will try to help you in understanding how to solve problems using DP. The article is based on examples, because a raw theory is very hard to understand.
Note: If you’re bored reading one section and you already know what’s being discussed in it – skip it and go to the next one.
What is a dynamic programming, how can it be described?
A DP is an algorithmic technique which is usually based on a recurrent formula and one (or some) starting states. A sub- solution of the problem is constructed from previously found ones. DP solutions have a polynomial complexity which assures a much faster running time than other techniques like backtracking, brute-force etc.
Now let’s see the base of DP with the help of an example:
Given a list of N coins, their values (V 1 , V 2 , … , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way that they sum up to S.
Now let’s start constructing a DP solution:
First of all we need to find a state for which an optimal solution is found and with the help of which we can find the optimal solution for the next state.
What does a "state" stand for?
It’s a way to describe a situation, a sub-solution for the problem. For example a state would be the solution for sum i, where i≤S. A smaller state than state i would be the solution for any sum j, where j<i. For finding a state i, we need to first find all smaller states j (j<i). Having found the minimum number of coins which sum up to i, we can easily find the next state – the solution for i+1.
How can we find it?
As a result we have found a solution of 3 coins which sum up to 11.
Additionally, by tracking data about how we got to a certain sum from a previous one, we can find what coins were used in building it. For example: to sum 11 we got by adding the coin with value 1 to a sum of 10. To sum 10 we got from 5. To 5 – from 0. This way we find the coins used: 1, 5 and 5.
Having understood the basic way a DP is used, we may now see a slightly different approach to it. It involves the change (update) of best solution yet found for a sum i, whenever a better solution for this sum was found. In this case the states aren’t calculated consecutively. Let’s consider the problem above. Start with having a solution of 0 coins for sum 0. Now let’s try to add first coin (with value 1) to all sums already found. If the resulting sum t will be composed of fewer coins than the one previously found – we’ll update the solution for it. Then we do the same thing for the second coin, third coin, and so on for the rest of them. For example, we first add coin 1 to sum 0 and get sum 1. Because we haven’t yet found a possible way to make a sum of 1 – this is the best solution yet found, and we mark S[1]=1. By adding the same coin to sum 1, we’ll get sum 2, thus making S[2]=2. And so on for the first coin. After the first coin is processed, take coin 2 (having a value of 3) and consecutively try to add it to each of the sums already found. Adding it to 0, a sum 3 made up of 1 coin will result. Till now, S[3] has been equal to 3, thus the new solution is better than the previously found one. We update it and mark S[3]=1. After adding the same coin to sum 1, we’ll get a sum 4 composed of 2 coins. Previously we found a sum of 4 composed of 4 coins; having now found a better solution we update S[4] to 2. The same thing is done for next sums – each time a better solution is found, the results are updated.
Elementary
To this point, very simple examples have been discussed. Now let’s see how to find a way for passing from one state to another, for harder problems. For that we will introduce a new term called recurrent relation, which makes a connection between a lower and a greater state.
Let’s see how it works:
Given a sequence of N numbers – A[1] , A[2] , …, A[N]. Find the length of the longest non-decreasing sequence.
As described above we must first find how to define a "state" which represents a sub-problem and thus we have to find a solution for it. Note that in most cases the states rely on lower states and are independent from greater states.
Let’s define a state i as being the longest non-decreasing sequence which has its last number A[i]. This state carries only data about the length of this sequence. Note that for i<j the state i is independent from j, i.e. doesn’t change when we calculate state j. Let’s see now how these states are connected to each other. Having found the solutions for all states lower than i, we may now look for state i. At first we initialize it with a solution of 1, which consists only of the i-th number itself. Now for each j<i let’s see if it’s possible to pass from it to state i. This is possible only when A[j]≤A[i] , thus keeping (assuring) the sequence non-decreasing. So if S[j] (the solution found for state j) + 1 (number A[i] added to this sequence which ends with number A[j] ) is better than a solution found for i (ie. S[j]+1>S[i] ), we make S[i]=S[j]+1. This way we consecutively find the best solutions for each i, until last state N.
Let’s see what happens for a randomly generated sequence: 5, 3, 4, 8, 6, 7:
I The length of the longest non-decreasing sequence of first i numbers
The last sequence i from which we "arrived" to this one
1 1 1 (first number itself) 2 1 2 (second number itself) 3 2 2 4 3 3 5 3 3
6 4 5
Practice problem: Given an undirected graph G having N (1<N<=1000) vertices and positive weights. Find the shortest path from vertex 1 to vertex N, or state that such path doesn’t exist.
Hint: At each step, among the vertices which weren’t yet checked and for which a path from vertex 1 was found, take the one which has the shortest path, from vertex 1 to it, yet found.
Try to solve the following problems from topcoder competitions:
ZigZag – 2003 TCCC Semifinals 3
BadNeighbors – 2004 TCCC Round 4
FlowerGarden – 2004 TCCC Round 1
Intermediate
Let’s see now how to tackle bi-dimensional DP problems.
Problem: A table composed of N x M cells, each having a certain quantity of apples, is given. You start from the upper-left corner. At each step you can go down or right one cell. Find the maximum number of apples you can collect.
shortest path found to vertex i. However in this problem we should also keep information about the money we have. Thus it would be reasonable to extend the array to something like Min[i][j] , which represents the length of the shortest path found to vertex i, with j money being left. In this way the problem is reduced to the original path-finding algorithm. At each step we find the unmarked state (i,j) for which the shortest path was found. We mark it as visited (not to use it later), and for each of its neighbors we look if the shortest path to it may be improved. If so – then update it. We repeat this step until there will remain no unmarked state to which a path was found. The solution will be represented by Min[N-1][j] having the least value (and the greatest j possible among the states having the same value, i.e. the shortest paths to which has the same length).
Pseudocode:
Set states(i,j) as unvisited for all (i,j) Set Min[i][j] to Infinity for all (i,j)
Min[0][M]=
While(TRUE)
Among all unvisited states(i,j) find the one for which Min[i][j] is the smallest. Let this state found be (k,l).
If there wasn't found any state (k,l) for which Min[k][l] is less than Infinity ‐ exit While loop.
Mark state(k,l) as visited
For All Neighbors p of Vertex k. If (l‐S[p]>=0 AND Min[p][l‐S[p]]>Min[k][l]+Dist[k][p]) Then Min[p][l‐S[p]]=Min[k][l]+Dist[k][p] i.e. If for state(i,j) there are enough money left for going to vertex p (l‐S[p] represents the money that will remain after passing to vertex p), and the shortest path found for state(p,l‐S[p]) is bigger than [the shortest path found for state(k,l)] + [distance from vertex k to vertex p)], then set the shortest path for state(i,j) to be equal to this sum. End For
End While
Find the smallest number among Min[N‐1][j] (for all j, 0<=j<=M); if there are more than one such states, then take the one with greater
j. If there are no states(N‐1,j) with value less than Infinity ‐ then such a path doesn't exist.
Here are a few TC problems for practicing:
Jewelry – 2003 TCO Online Round 4
StripePainter – SRM 150 Div 1
QuickSums – SRM 197 Div 2
ShortPalindromes – SRM 165 Div 2
Advanced
The following problems will need some good observations in order to reduce them to a dynamic solution.
Problem StarAdventure – SRM 208 Div 1:
Given a matrix with M rows and N columns (N x M). In each cell there’s a number of apples. You start from the upper-left corner of the matrix. You can go down or right one cell. You need to arrive to the bottom- right corner. Then you need to go back to the upper-left cell by going each step one cell left or up. Having arrived at this upper-left cell, you need to go again back to the bottom-right cell. Find the maximum number of apples you can collect. When you pass through a cell – you collect all the apples left there.
Restrictions: 1 < N, M <= 50 ; each cell contains between 0 and 1000 apples inclusive.
First of all we observe that this problem resembles to the classical one (described in Section 3 of this article), in which you need to go only once from the top-left cell to the bottom-right one, collecting the maximum possible number of apples. It would be better to try to reduce the problem to this one. Take a good look into the statement of the problem – what can be reduced or modified in a certain way to make it possible to solve using DP? First observation is that we can consider the second path (going from bottom-right cell to the top-left cell) as a path which goes from top-left to bottom- right cell. It makes no difference, because a path passed from bottom to top, may be passed from top to bottom just in reverse order. In this way we get three paths going from top to bottom. This somehow decreases the difficulty of the problem. We can consider these 3 paths as left, middle and right. When 2 paths intersect (like in the figure below)
© 2015 Topcoder. All Rights Reserved
SITEMAP ABOUT US CONTACT US HELP CENTER PRIVACY POLICY TERMS
Topcoder is also on
SRM 208 Div I Hard – StarAdventure SRM 178 Div I Hard – MiniPaint
Member Tutorials Read more than 40 data science tutorials written by topcoder members. Problem Set Analysis Read editorials explaining the problem and solution for each Single Round Match (SRM). Data Science Guide New to topcoder's data science track? Read this guide for an overview on how to get started in the arena and how competitions work. Help Center Need specifics about the process or the rules? Everything you need to know about competing at topcoder can be found in the Help Center. Member Forums Join your peers in our member forums and ask questions from the real experts - topcoder members!
More Resources