Dynamic programming algorithm is very important.
The basic idea of dynamic programming is: will to solve the problem into several sub-questions, the first sub-problem solving, solution of the original problem then the solution obtained from these sub-issues. The difference is with divide and conquer, dynamic programming to solve the problem of fit, sub-problems by decomposition is often not independent of each other. The dynamic programming problem into sub-problems, but not sub-problems are independent, but dependent on each other, help each other, good use of the information sub-structure problem.
Dynamic programming steps are:
1. Find the optimal solution characteristics, and to characterize the structure wherein;
2. The optimal value defined recursively;
3. In a bottom-up fashion calculate the optimal value;
4. The optimal solution calculating the optimum value of the obtained information, configuration.
The effectiveness of dynamic programming algorithm depends on the two most important properties: optimal substructure property and overlapping sub-problems nature.
Here is an chestnuts, matrix continually multiply problems.
1 // Dynamic Programming - matrix multiplication problem even 2 #include <the iostream> . 3 #include <CString> . 4 . 5 the using namespace STD; . 6 int P [ . 7 ] = { 30 , 35 , 15 , . 5 , 10 , 20 is , 25 }; // 6 matrix . 7 int m [ . 6 ] [ . 6 ]; // to store the calculated amount of the matrix . 8 int S [ . 6 ] [ . 6 ]; // store division point matrix 9 10 void matrixChain () // Third step: calculating the optimal value (two steps in front of reflected code not then, the process is contemplated) 11 // function to generate the breakpoint matrix, calculate the optimal value, the matrix returned s 12 is { 13 is int length = . 6 ; 14 Memset (m, 0 , the sizeof (m)); 15 Memset (S, 0 , the sizeof (S)); 16 for ( int R & lt = 2 ; R & lt <= length; R & lt ++) / / R & lt represents the calculated range, calculation amount calculated starting only two matrices, and sequentially calculates three, four up to six targets 17 // upward dynamic programming which is reflected from the bottom, from the smallest unit counted 18 is { . 19 for (int I = . 1 ; I <= length-R & lt + . 1 ; I ++) // I, J respectively currently computed range 20 is { 21 is int J = I + R- . 1 ; 22 is = m m [I] [J] [ I] [I] + m [I + . 1 ] [J] + P [I] * P [I- . 1 ] * P [J]; // since the latter requires the optimum value, so there is actually just find a value 23 S [I] [J] = I; 24 for ( int K = I + . 1 ; K <J; K ++) // in the range of optimization, find the minimum value of 25 { 26 is int TEMP = m [I] [K] + m [K + . 1 ] [J] + P [I- . 1 ] * P [K] *P [J]; 27 IF (TEMP < m [I] [J]) 28 { 29 m [I] [J] = TEMP; 30 S [I] [J] = K; 31 is } 32 } 33 is } 34 is } 35 } 36 void TRACEBACK ( int I, int J) // step 4: optimum configuration. S input matrix and solving desired range, the output of the best embodiment of the brackets 37 [ { 38 is IF (I == J) 39 { 40 COUT << " M"<<"["<<i<<"]"; 41 return; 42 } 43 cout<<"("; 44 TraceBack(i,s[i][j]); 45 TraceBack(s[i][j]+1,j); 46 cout<<")"; 47 } 48 int main() 49 { 50 matrixChain(); 51 TraceBack(1,6); 52 cout<<m[1][6]<<endl; 53 return 0; 54 }