Algorithm analysis Design and Microprocessor - DynamicProgramming, Study notes of Design and Analysis of Algorithms

Detail Summery about Dynamic Programming (DP), Application domain of DP, Typical steps of DP, DP Example – Assembly Line Scheduling (ALS), Brute Force Solution, ALS FAST-WAY algorithm.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Dynamic Programming (DP)
Like divide-and-conquer, solve problem by
combining the solutions to sub-problems.
Differences between divide-and-conquer and DP:
Independent sub-problems, solve sub-problems
independently and recursively, (so same
sub(sub)problems solved repeatedly)
Sub-problems are dependent, i.e., sub-problems share
sub-sub-problems, every sub(sub)problem solved just
once, solutions to sub(sub)problems are stored in a
table and used for solving higher level sub-problems.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Algorithm analysis Design and Microprocessor - DynamicProgramming and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

Dynamic Programming (DP)

  • (^) Like divide-and-conquer, solve problem by combining the solutions to sub-problems.
  • (^) Differences between divide-and-conquer and DP:
    • (^) Independent sub-problems, solve sub-problems independently and recursively, (so same sub(sub)problems solved repeatedly)
    • (^) Sub-problems are dependent, i.e., sub-problems share sub-sub-problems, every sub(sub)problem solved just once, solutions to sub(sub)problems are stored in a table and used for solving higher level sub-problems.

Application domain of DP

  • (^) Optimization problem: find a solution with

optimal (maximum or minimum) value.

  • (^) An optimal solution, not the optimal

solution, since may more than one optimal

solution, any one is OK.

DP Example – Assembly Line Scheduling (ALS)

Concrete Instance of ALS

ALS --DP steps: Step 1

  • (^) Step 1: find the structure of the fastest way through factory - (^) Consider the fastest way from starting point through station S1, j (same for S2, j ) - (^) j =1, only one possibility - (^) j =2,3,…, n , two possibilities: from S1, j -1 or S2, j - - from S1, j -1, additional time a1, j - (^) from S2, j -1, additional time t2, j -1 + a1, j - suppose the fastest way through S1, j is through S1, j -1, then the chassis must have taken a fastest way from starting point through S1, j -1. Why??? - Similarly for S2, j -1.

DP step 1: Find Optimal Structure

  • (^) An optimal solution to a problem contains

within it an optimal solution to subproblems.

  • (^) the fastest way through station S

i , j contains

within it the fastest way through station S1, j -1 or

S2, j -.

  • (^) Thus can construct an optimal solution to a

problem from the optimal solutions to

subproblems.

ALS --DP steps: Step 2

  • (^) Recursive solution:
    • (^) f * = min( f 1 [ n ] + x 1 , f 2 [ n ] + x 2 )
    • f 1 [ j ]= e 1 + a 1,1 if j =
    • (^) min( f 1 [ j -1]+ a 1, j , f 2 [ j -1]+ t 2, j -1+ a 1, j ) (^) if j >
    • (^) f 2 [ j ]= e 2 + a 2,1 if j =
    • (^) min( f 2 [ j -1]+ a 2, j , f 1 [ j -1]+ t 1, j -1+ a 2, j ) (^) if j >
  • (^) fi [ j ] ( i =1,2; j =1,2,…, n ) records optimal values to the subproblems.
  • (^) To keep the track of the fastest way, introduce li [ j ] to record the line number (1 or 2), whose station j -1 is used in a fastest way through Si , j.
  • (^) Introduce l * to be the line whose station n is used in a fastest way through the factory.

ALS --DP steps: Step 3

  • (^) Step 3: Computing the fastest time
    • (^) One option: a recursive algorithm.
      • (^) Let ri ( j ) be the number of references made to fi [ j ]
        • r 1 ( n ) = r 2 ( n ) = 1
        • r 1 ( j ) = r 2 ( j ) = r 1 ( j +1)+ r 2 ( j +1)
        • ri ( j ) = 2 n - j.
        • (^) So f 1 [1] is referred to 2 n -1^ times.
        • (^) Total references to all fi [ j ] is (2 n ).
      • (^) Thus, the running time is exponential.
    • (^) Non-recursive algorithm.

ALS --DP steps: Step 4

  • (^) Step 4: Construct the fastest way through

the factory

Matrix-chain multiplication (MCM) -DP

  • (^) Problem: given A 1 , A 2 , …,A n , compute the product: A 1 A 2 …A n , find the fastest way (i.e., minimum number of multiplications) to compute it.
  • (^) Suppose two matrices A( p , q ) and B( q , r ), compute their product C( p , r ) in pqr multiplications - (^) for i= 1 to p for j= 1 to r C[i,j]= - (^) for i= 1 to p - (^) for j= 1 to r - (^) for k= 1 to q C[i,j] = C[i,j]+ A[i,k]B[k,j]

Matrix-chain multiplication –MCM DP

  • (^) Intuitive brute-force solution: Counting the number of parenthesizations by exhaustively checking all possible parenthesizations.
  • (^) Let P( n ) denote the number of alternative parenthesizations of a sequence of n matrices: - (^) P( n ) = 1 if n =  k =1 n -1^ P( k )P( n - k ) if n  2
  • (^) The solution to the recursion is (2 n ).
  • (^) So brute-force will not work.

MCP DP Steps

  • (^) Step 1: structure of an optimal parenthesization
    • Let A i .. j ( ij ) denote the matrix resulting from A i A i +1…A j
    • (^) Any parenthesization of A i A i +1…A j must split the product between A k and A k +1 for some k , ( ik < j ). The cost = # of computing A i..k + # of computing A k +1.. j + # A i..k  A k +1.. j.
    • (^) If k is the position for an optimal parenthesization, the parenthesization of “prefix” subchain A i A i +1…A k within this optimal parenthesization of A i A i +1…A j must be an optimal parenthesization of A i A i +1…A k.
    • (^) A i A i +1…A k  A k +1…A j

MCM DP Steps

  • (^) Step 3, Computing the optimal cost
    • (^) If by recursive algorithm, exponential time (2 n ) (ref. to P.346 for the proof.), no better than brute-force.
    • (^) Total number of subproblems: + n = ( n^2 )
    • (^) Recursive algorithm will encounter the same subproblem many times.
    • (^) If tabling the answers for subproblems, each subproblem is only solved once.
    • (^) The second hallmark of DP: overlapping subproblems and solve every subproblem just once.

2 n

MCM DP Steps

  • (^) Step 3, Algorithm,
    • (^) array m [1.. n ,1.. n ], with m [ i , j ] records the optimal cost for A i A i +1…A j.
    • (^) array s [1.. n ,1.. n ], s [ i , j ] records index k which achieved the optimal cost when computing m [ i , j ].
    • (^) Suppose the input to the algorithm is p =< p 0 , p 1 , …, pn >.