Definition
动态规划是一种常用的优化问题求解策略。动态规划算法是一种递归算法,他的本质是通过子问题执行顺序的调度来降低算法的代价。
The core implementation of DP
Those who cannot remember the past are condemened to repeat it. |
动态规划的核心是记住已经计算过的解,动态规划的方法有两种:
- 自顶向下的备忘录法
- 自底向上
An example for understanding DP
- Recursion and DP
In order to calculate the value of Fibonacci, both recursion and DP are optional.
Recursion:
def fib(n):
if n<=0: return 0
if n==1: return 1
return fib(n-1) + fib(n-2)
Too many repetitive computation have been done and lead to inefficiency. Fortunately, DP can avoid the redundancy computation. For each f i b ( i ) fib(i) fib(i), it needs to be computated once.
**However, we should notice that it will increase space complexity.** |
- Top-down variation
def fib(k):
if k==1: return 1
if k==0: return 0
ans = [-1 for i in range(k+1)]
ans[1] = 1
ans[0] = 0
def fcci(ans, n):
if n==1 or n==0 or ans[n]!=-1: return ans[n]
if ans[n-1]!=-1 and ans[n-2]!=-1:
ans[n] = ans[n-1] + ans[n-2]
elif ans[n-1]!=-1:
ans[n] = ans[n-1] + fcci(ans,n-2)
elif ans[n-2]!=-1:
ans[n] = ans[n-2] + fcci(ans,n-1)
else:
ans[n] = fcci(ans,n-2) + fcci(ans,n-1)
return ans[n]
return fcci(ans,k)
- Bottom-up variation
def fib(k):
if k==0: return 0
if k==1: return 1
ans = [-1 for i in range(k+1)]
ans[1] = 1
ans[0] = 0
for i in range(2,k+1):
ans[i] = ans[i-1] + ans[i-2]
return ans[k]
From the code above, we can find that the process of DP also uses the recursion.