Dynamic Programming
2016-07-28 21:09
459 查看
Those who cannot remember the past are condemned to repeat 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 is more efficient than recursive function.One main advantages of DP is that it can store the intermediate states for future use, which means it does not need to recompute some middle results like in the recursive function. So DP is much more efficient.
A small example is about Fibonacci numbers:
Fibonacci (n) = 1; if n = 0
Fibonacci (n) = 1; if n = 1
Fibonacci (n) = Fibonacci(n-1) + Fibonacci(n-2)
A code for pure recursion:
int fib (int n) { if (n < 2) return 1; return fib(n-1) + fib(n-2); }
A code for DP:
void fib () { fibresult[0] = 1; fibresult[1] = 1; for (int i = 2; i<n; i++) fibresult[i] = fibresult[i-1] + fibresult[i-2]; }
Next, I will give a basic example and an elementary example for introduction
1. Coin Change (basis)
QuestionGiven a list of N coins, their values (V1, V2, … , 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.
Analysis
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
Set Min[i] equal to Infinity for all of i Min[0]=0 For i = 1 to S For j = 0 to N - 1 If (Vj<=i AND Min[i-Vj]+1<Min[i]) Then Min[i]=Min[i-Vj]+1 Output Min[S]
C++ code:
static int minCoins(int coins[], int m, int V) { // base case if (V == 0) return 0; // Initialize result int res = Integer.MAX_VALUE; // Try every coin that has smaller value than V for (int i=0; i<m; i++) { if (coins[i] <= V) { int sub_res = minCoins(coins, m, V-coins[i]); // Check for INT_MAX to avoid overflow and see if // result can minimized if (sub_res != Integer.MAX_VALUE && sub_res + 1 < res) res = sub_res + 1; } } return res; }
2. Longest non-decreasing sequence (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.Question
Given a sequence of N numbers – A[1] , A[2] , …, A
. Find the length of the longest non-decreasing sequence.
As descri
4000
bed 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.
Concretely, we have to find the value of longest subsequences at every index point. Then largest LSi would be the longest subsequence in the given sequence. To begin LSi is assigned to be one since Ai is element of the sequence(Last element). Then for all j such that j
for i=0 to n-1 LS[i]=1; for j=0 to i-1 if (a[i] > a[j] and LS[i]<LS[j]); LS[i] = LS[j]+1 for i=0 to n-1 if (largest < LS[i]) largest = LS[i]
More examples on : Click me
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- 浏览器运行java插件报错:Algorithm constraints check failed: MD5withRSA
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- Ubuntu SSH Algorithm negotiation failed
- [转]可视化的数据结构和算法
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- 使用他人的MD5编码类,修改形成密码串
- Extracting Structured Data from Web Pages
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘