跳转至

Dynamic Programming

Example:

1. Fibonacci numbers

int fib(int n) {
    if (n <= 1) return n;
    return fib(n-1) + fib(n-2);
}
  • Time complexity: O(2n)

If we use dynamic programming, we can reduce the time complexity to O(n).

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 M1,M2,M3,M4. M1[10×20],M2[20×50],M3[50×1],M4[1×100]

1 * In which order can we compute the product of n matrices with minimal computing time?

2

  • Satisfy the optimal substructure property
  • Time complexity: (1) Go through l : O(n) (2) Computer m[i][l] : O(1) (3) Compute m[i+1][j] : O(1) (4) ri1×rl×rj : O(1)

Total time complexity: O(n3) -- compute all m[i][j]

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.

    F[N][i]=mink(F[ki][i]+F[Nki][k+1]+rki1×ri×rNi) ?

3. Optimal Binary Search Trees

Given N words w1,w2,,wN and their search probabilities p1,p2,,pN . We want to arrange these words in a binary search tree in a way that minimize the expected total access time : i=1N(di+1)×pi , where di is the depth of the node containing wi in the binary search tree.

3

  • Note: ordered by Alphabetical order!

4

4. All Pairs Shortest Paths

  • Given a graph G=(V,E) with edge weights w(u,v) for (u,v)E , we want to compute the shortest path between every pair of vertices in V .

Method 1: Use single source shortest path algorithm N times.

Method 2: Floyd-Warshall Algorithm Define Dk[i][j]= min{length of path ilkj} and D1[i][j]=Cost[i][j].

Then the length of the shortest path from i to j is DN1[i][j].

5

/* 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 ]; 
}

T(N)=O(N3), but faster in a dense graph.

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 9 8


最后更新: 2024年5月5日 21:09:23
创建日期: 2024年4月17日 10:15:44
Is this page helpful?