Dynamic Programming¶
Example:¶
1. Fibonacci numbers¶
- Time complexity:
If we use dynamic programming, we can reduce the time complexity to
int fib(int n) {
int f[n+1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++) {
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
2. Ordering Matrix Multiplications¶
- Suppose we are to multiply 4 matrice
.
* In which order can we compute the product of n matrices with minimal computing time?
- Satisfy the optimal substructure property
- Time complexity:
(1) Go through l :
(2) Computer m[i][l] : (3) Compute m[i+1][j] : (4) :
Total time complexity:
void OptMatrix( const long r[ ], int N, TwoDimArray M )
{ int i, j, k, L;
long ThisM;
for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0;
for( k = 1; k < N; k++ ) /* k = j - i */
for( i = 1; i <= N - k; i++ ) { /* For each position */
j = i + k; M[ i ][ j ] = Infinity;
for( L = i; L < j; L++ ) {
ThisM = M[ i ][ L ] + M[ L + 1 ][ j ]
+ r[ i - 1 ] * r[ L ] * r[ j ];
if ( ThisM < M[ i ][ j ] ) /* Update min */
M[ i ][ j ] = ThisM;
} /* end for-L */
} /* end for-Left */
}
- More suitable way : Find a order that is increasing.
?
3. Optimal Binary Search Trees¶
Given N words
- Note: ordered by Alphabetical order!
4. All Pairs Shortest Paths¶
- Given a graph
with edge weights for , we want to compute the shortest path between every pair of vertices in .
Method 1: Use single source shortest path algorithm
Method 2: Floyd-Warshall Algorithm
Define
Then the length of the shortest path from i to j is
/* A[ ] contains the adjacency matrix with A[ i ][ i ] = 0 */
/* D[ ] contains the values of the shortest path */
/* N is the number of vertices */
/* A negative cycle exists iff D[ i ][ i ] < 0 */
void AllPairs( TwoDimArray A, TwoDimArray D, int N )
{ int i, j, k;
for ( i = 0; i < N; i++ ) /* Initialize D */
for( j = 0; j < N; j++ )
D[ i ][ j ] = A[ i ][ j ];
for( k = 0; k < N; k++ ) /* add one vertex k into the path */
for( i = 0; i < N; i++ )
for( j = 0; j < N; j++ )
if( D[ i ][ k ] + D[ k ][ j ] < D[ i ][ j ] )
/* Update shortest path */
D[ i ][ j ] = D[ i ][ k ] + D[ k ][ j ];
}
How to design a DP method? 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution in some order. 4. Reconstruct an optimal solution from computed information.
5. Product Assembly¶
- Two assembly lines for the same car
- Different technology (time) for each stage
- One can change lines between stages
- Minimize the total assembly time
创建日期: 2024年4月17日 10:15:44