Overview
As with the Division and Control Law, dynamic planning solves the entire problem by combining sub-problems.
Four steps for dynamic planning:
1. Describe the optimal solution structure;
2. recursively define the value of the optimal solution;
3. Calculate the optimal solution based on the bottom-up mode;
4. Construct the optimal solution based on the calculated results. (Steps 1-3 are the foundation and steps 1 can be omitted)
Dynamic Planning features, including replay issues. You can use the graph structure to save intermediate results without repeated computation.
1. assembly line Scheduling
(PS: This figure is too embarrassing. There is no way. The introduction to algorithms of the original web version is available on the PKU server, and access is not allowed now. Unfortunately, the Nb school is Nb, I can only look up to P people ~ Please wait for a while ~)
Q: production time is required for each workshop. How long does it take to deploy production lines from different production lines to another? How long does it take to calculate the minimum time?
1. Describe the optimal solution structure (molecular Partitioning Problem, sub-structure): divided by production line length;
2. recursively define the value of the optimal solution: recursively solve the optimal solution of each length;
3. Calculate the optimal solution based on the bottom-up mode and solve the problem from short to long by length;
Input: production line length, production time of each workshop, scheduling time between each workshop,
Output: shortest length;
Example:
Input:
6
7 9 3 4 8 4
8 5 6 4 5 7
2 3 1 3 4
2 1 2 2 1
Output
38
#include <iostream>using namespace std;int minT[2][100];int a[2][100], t[2][100];int e[2]={2,4}, x[2]={3,2};int cal( int k, int num){if (num==1) {minT[k][1] = e[k]+a[k][1];return minT[k][1];}if(minT[k][num]>0) return minT[k][num];else{minT[k][num] = min(cal(k, num-1), cal((k+1)%2, num-1)+t[(k+1)%2][num-1] )+a[k][num];return minT[k][num];}}int main(){memset(minT, 0, sizeof(minT));int count;cin>>count;cout<<"a & t: "<<endl;for(int i=1 ;i<=count; i++) cin>>a[0][i];for(int i=1 ;i<=count; i++) cin>>a[1][i];for(int i=1 ;i<=count-1; i++) cin>>t[0][i];for(int i=1 ;i<=count-1; i++) cin>>t[1][i];int minCost = min(cal(0,count)+x[0] ,cal(1,count)+x[1]);cout<<"MIN:"<<minCost<<endl;}
2. Matrix chain Multiplication
The order of multiplication of N matrices, A1, A2, A3,... an; is different,
A1, A2, A3, and A4 can be combined into five different combinations:
1. (A1 (A2 (A3 * A4) 2. (A1 (A2 * A3) A4) 3. (A1 * A2) (A3 * A4) 4. (A1 (A2 * A3) A4) 5. (A1 * A2) A3) A4)
The number of times of multiplication varies according to different combinations.
The minimum number of times of output multiplication is required.
Input
Number of matrices; (one value n)
X-axis and Y-axis of the matrix: (n + 1 Value) because the number of rows in the rear matrix is equal to the number of columns in the front Coordinate
Calculation interval required:
Output:
Minimum interval count
For example:
Input:
6
30 35 15 5 10 20 25
1 6
Output:
15125
1. Describe the optimal solution structure (molecular Partitioning Problem, sub-structure): divided by combination;
2. recursively define the value of the optimal solution: recursively solve the optimal solution of each combination (and save it in the corresponding position of the matrix)
3. Calculate the optimal solution based on the bottom-up mode and solve the problem from short to long by range. (You can call the matrix continuously, if any in the matrix)
Dynamic Planning Equation
S [I, j] = 0 (if I = J );
S [I, j] = s [I, K] + s [k + 1, J] + P [I-1] P [k] P [J];
#include <iostream>using namespace std;#define INF 1000000000;double s[100][100];int p[101];double minMul(int low, int high){if(low==high) return 0;if(s[low][high]>0) return s[low][high];double minTmp = INF;double tmp;for (int k=low; k
3. Longest Common subsequence
Two sequences are given. The longest common sub-sequence length of these two sequences must be given. They can be discontinuous, but must be sequential.
The longest common sub-sequence of a = "abcbdab", B = "bdcaba" has a length of 4 and is "bcba"
Typical use of a matrix to save the median value and continuously solve the optimal solution (same type: the shortest distance between strings)
Dynamic Planning equation:
If (I = 0 or J = 0)
When F (a [I] = B [J]), a [I] [J] = 1; otherwise, a [I] [J] = 0;
Else:
When F (a [I] = B [J]) A [I] [J] = A [I-1] [J-1] + 1; otherwise a [I] [J] = max (A [I-1] [J], a [I] [J-1]);
#include <iostream>#include <string>using namespace std;string a="ABCBDAB", b="BDCABA";int A[100][100];int main(){int la = a.length();int lb = b.length();for(int i=0; i<la; i++){for(int j=0; j<lb; j++){if(i==0 || j==0){if(a[i]==b[j]) A[i][j]=1;else A[i][j]=0;}else{if(a[i]!=b[j]) A[i][j] = max(A[i-1][j], A[i][j-1]);else A[i][j]=A[i-1][j-1]+1;}}}cout<<A[la-1][lb-1]<<endl;}
4. Construct the optimal binary search tree
Each node has an expected P, and each leaf node has an expected Q. A binary tree is constructed to minimize the overall search expectation.
Each node has an expected P, and each leaf node has an expected Q. A binary tree is constructed to minimize the overall search expectation.
Input
6
0.15 0.1 0.05 0.1 0.2
0.05 0.1 0.05 0.05 0.05 0.1
Dynamic Planning equation:
Probability and:
W (I, j) = Q (j) (if I> J leaf node)
W (I, j) = W (I, R-1) + P (r) + W (R + 1, J)
Search expectation:
E (I, j) = W (I, j) (if I> J leaf node)
E (I, j) = E (I, R-1) + W (I, j) + E (R + 1, J)
#include <iostream>#define INF 1000000;using namespace std;float p[100],q[100];float w[100][100], e[100][100];float calW(int i, int j){if(w[i][j]>0) return w[i][j];if(i > j){w[i][j] = q[j];return w[i][j];}float minw=INF;for (int r=i; r<=j; r++){w[i][j] = calW(i,r-1)+p[r]+calW(r+1,j);if(w[i][j]<minw) minw = w[i][j];}w[i][j] = minw;return w[i][j];}float calE(int i, int j){if(e[i][j]>0) return e[i][j];if(i>j) {e[i][j] = calW(i,j);return e[i][j];}float minE=INF;for (int r=i; r<=j; r++){e[i][j] = calE(i, r-1)+calW(i, j)+calE(r+1, j);if(e[i][j]<minE) minE = e[i][j];}e[i][j] = minE;return e[i][j];}int main(){int n;memset(p,0,sizeof(p));memset(q,0,sizeof(q));cin>>n;for (int i=1; i<n; i++) cin>>p[i]; //non-leaffor (int j=0; j<n; j++) cin>>q[j]; //leaffloat all15=calE(1,5);cout<<all15<<endl;}