Dynamic Programming
for Novice
Intro
WHAT is it?
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.
Fibonacci
Problem definition
Optimal substructure
Overlapping sub-problems
Problem definition
F5
F4
F3
F2
F1
Fibonacci
Kruskal's algorithm
Prim's algorithm
Problem definition
Q
R
T
S
Longest path problem
(No-optimal substructure)
Flow
1) Describe optimal structure
2) Make recurrent relationship (connect optimal values)
3) Bottom-up approach
4) Use information and build solution
...
PROFIT!!!
Recurring formulas
F(n) = F(n-1) + F(n-2)
F0 = F1 = 1
F(2n) = F(n) + F(n-1),
F(2n+1) = F(n) – F(n-1),
F0 = F1 = 1
Recursion or Loop
Recursion or Loop
Recursion:
+ Easy implementation
- Slow (little bit)
Loop:
+ Fast
- Memory consumption
private static int f(int n){
if(n==0 || n==1) return 1;
if(n%2==0){
return f(n/2)+f(n/2-1);
}else{
return f((n-1)/2)-f((n-1)/2-1);
}
}
if(n<2) return 1;
int[] fs = int[n];
fs[0]=fs[1]=1;
for(int i=2; i<n; i++){
if(i%2==0){
fs[i]=fs[i/2]+fs[i/2-1];
}else{
fs[i]=fs[(i-1)/2]+fs[(i-1)/2-1]
}
}
return fs[n-1];
Caching
f(6)+f(5)
f(6)=f(3)+f(2)
f(5)=f(2)-f(1)
f(2)
# | 3 |
---|---|
# | 4 |
# | 5 |
... |
Typical task
N steps
1) 1 step
2) 2 steps
3) 3 steps
Typical task
F(N) = F(N-1)+F(N-2)+F(N-3)
private static int f(int n){
if(n==1) return 1;
if(n==2) return 2;
if(n==3) return 4;
return f(n-1)+f(n-2)+f(n-3);
}
int[] arr = new int[3];
arr[0]=1; arr[1]=2; arr[2]=4;
for(int i=3; i<N; i++){
vars[i%3] = arr[0]+arr[1]+arr[2];
}
}
2-d Dynamic
N
M
2-d Dynamic
(x, y-1)
(x-1, y)
F(x,y) = F(x-1, y)+F(x,y-1)
2-d Dynamic
F(x,y) = F(x-1, y)+F(x,y-1)
for(int i=0; i<Imax; i++){
for(int j=0; j<Jmax; j++){
if(i==0 || j==0){
dp[i][j]=1;
}else{
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
}
Explosion hazard task
A type
(especially dangerous)
B type
(non-hazardous)
N containers
A |
---|
B |
B |
A |
... |
Explosion hazard task
BBB |
BBA |
BAB |
ABB |
ABA |
BB |
BA |
AB |
B |
A |
∆
B type => 2 combinations
A type => comes to B type (in 1 iteration)
Fibonacci numbers
Broken calculator task
1) Add 1
2) Multiply by 2
3) Multiply by 3
Input: X (int)
Output: min sequence of operations (1231123)
Broken calculator task
F(N) = min(F(N-1), F(N/2), F(N/3)) + 1
int min;
for(int i=2; i<N+1; i++){
min=a[i-1]+1;
if(i%2==0) min=Math.min(min,a[i/2]+1);
if(i%3==0) min=Math.min(min,a[i/3]+1);
a[i] = min;
}
Golden pyramid
Golden pyramid
def golden_pyramid(triangle, row=0, column=0, total=0):
global count
count += 1
if row == len(triangle) - 1:
return total + triangle[row][column]
return max(golden_pyramid(triangle, row + 1, column, total + triangle[row][column]),
golden_pyramid(triangle, row + 1, column + 1, total + triangle[row][column]))
Golden pyramid
def golden_pyramid_d(triangle):
tr = [row[:] for row in triangle] # copy
for i in range(len(tr) - 2, -1, -1):
for j in range(i + 1):
tr[i][j] += max(tr[i + 1][j], tr[i + 1][j + 1])
return tr[0][0]
What's next?
Amortized analysis
Greedy algorithms
Thomas H. Cormen
The End
Be happy and learn algorithms!
You are awesome!
Dynamic Programming
By vladborsh
Dynamic Programming
- 1,563