Dynamic programing
-
Upload
abdur-rehman-muhammadi -
Category
Education
-
view
190 -
download
1
description
Transcript of Dynamic programing
Advanced Algorithm Analysis
Dynamic Programming
Problem having multiple solution
Dynamic Programming used to solve Optimization Problem
Dynamic Programming is used to find solution with optimal value.
Dynamic Programming
Solve the problem by combining the solution of sub-problems
Divide and Conquer approach re-compute the solution on each step
Dynamic Programming solve sub-problem only once
Developing DP Algorithm
It has 4-steps1. Characterize the structure of Optimal
Solution2. Recursively define the value of an
Optimal Solution3. Compute the value of Optimal Solution,
typically in bottom-up fashion4. Construct the Optimal Solution from
computed information
Developing DP Algorithm
Step 1-3 are the basis of dynamic programming solution to a problem.
› It gives the optimal value only
Step 4 is used to construct the Optimal Solution
Assembly Line Scheduling
Step-1: Characterize the Optimal Solution
If we look at a fastest way through station S1,j, it must go through station j-1 on either line 1 or line 2. Thus the fastest way through station S1,j is either › The fastest way through S1,j-1 and then
directly through station S1,j or › The fastest way through station S2,j-1, a
transfer from line 2 to line 1, and then through station S1,j.
Step-1: Characterize the Optimal Solution
Using symmetric reasoning, the fastest way through stations S2,j is either › The fastest way through S2,j-1 and then
directly through station S2,j or › The fastest way through station S1,j-1, a
transfer from line 1 to line 2, and then through station S2,j
Step 2: Recursive Solution
f* = min(f1[n] + x1, f2[n] + x2) f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
f1[j] = e1 + a1,1 if j = 1 = min(f1[j-1] + a1,j, f2[j-1] + t2,j-1 + a1,j)
if j ≥ 2 f2[j] = e2 + a2,1 if j = 1 = min(f2[j-1] + a2,j, f1[j-1] + t1,j-1 + a2,j)
if j ≥ 2
Step 3: Computing Fastest Time
FASTEST-WAY(a, t, e, x, n) 1 f1[1] ← e1 + a1,1
2 f2[1] ←e2 + a2,1
3 for j ← 2 to n 4 do if f1[j - 1] + a1,j ≤ f2[j - 1] + t2,j-1 + a1,j
5 then f1[j] ← f1[j - 1] + a1, j
6 l1[j] ← 1 7 else f1[j] ← f2[j - 1] + t2,j-1 + a1,j
8 l1[j] ← 2 9 if f2[j - 1] + a2,j ≤ f1[j - 1] + t1,j-1 + a2,j
10 then f2[j] ← f2[j - 1] + a2,j 11 l2[j] ← 2 12 else f2[j] ∞ f1[j - 1] + t1,j-1 + a2,j
13 l2[j] ← 1 14 if f1[n] + x1 ≤ f2[n] + x2
15 then f* = f1[n] + x1
16 l* = 1 17 else f* = f2[n] + x2
18 l* = 2
Step 4: Construct the Fastest Way
PRINT-STATIONS(l, n) 1 i ← l* 2 print "line " i ", station " n 3 for j ← n downto 2 4 do i ← li[j] 5 print "line " i ", station " j - 1
Problem Specification
1 2 3 4 5 6
S1,i 7 9 3 4 8 4
S2,i 8 5 6 4 5 7
t1,i 2 3 1 3 4 -
t2,i 2 1 2 2 1 -
ei 2 4
xi 3 2
Solution- Step 01
Solution-Step 02
f* = min(f1[n] + x1, f2[n] + x2) f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
f1[j] = e1 + a1,1 if j = 1 = min(f1[j-1] + a1,j, f2[j-1] + t2,j-1 + a1,j)
if j ≥ 2 f2[j] = e2 + a2,1 if j = 1 = min(f2[j-1] + a2,j, f1[j-1] + t1,j-1 + a2,j)
if j ≥ 2
Step 3: Computing Fastest Time FASTEST-WAY(a, t, e, x, n) 1 f1[1] ← e1 + a1,1
2 f2[1] ←e2 + a2,1
3 for j ← 2 to n 4 do if f1[j - 1] + a1,j ≤ f2[j - 1] + t2,j-1 + a1,j
5 then f1[j] ← f1[j - 1] + a1, j
6 l1[j] ← 1 7 else f1[j] ← f2[j - 1] + t2,j-1 + a1,j
8 l1[j] ← 2 9 if f2[j - 1] + a2,j ≤ f1[j - 1] + t1,j-1 + a2,j
10 then f2[j] ← f2[j - 1] + a2,j 11 l2[j] ← 2 12 else f2[j] ∞ f1[j - 1] + t1,j-1 + a2,j
13 l2[j] ← 1 14 if f1[n] + x1 ≤ f2[n] + x2
15 then f* = f1[n] + x1
16 l* = 1 17 else f* = f2[n] + x2
18 l* = 2
Solution-Step 03
Cost and LineJ 1 2 3 4 5 6
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
J 2 3 4 5 6
l1[j] 1 2 1 1 2
l2[j] 1 2 1 2 2
Solution-Step 03
Optimal Value
› f* = 38
› l* = 1
Step 4: Construct the Fastest Way
PRINT-STATIONS(l, n) 1 i ← l* 2 print "line " i ", station " n 3 for j ← n downto 2 4 do i ← li[j] 5 print "line " i ", station " j - 1
J 2 3 4 5 6
l1[j] 1 2 1 1 2
l2[j] 1 2 1 2 2
Solution-Step 04
line 1, station 6 line 2, station 5 line 2, station 4 line 1, station 3 line 2, station 2 line 1, station 1
J 1 2 3 4
a1,I 3 29 3 8
a2,I 6 5 6 5
t1,I 2 3 6 4
t2,I 2 1 12 5
ei 3 1
Xi 4 9
Example 1
J 1 2 3 4 5
f1[j] 6 35 15 23 27
f2[j] 7 11 17 22 72
J 2 3 4 5
l1[j]
1 2 1 1
l2[j]
2 2 2 2
F* = 31 l*=1
J 1 2 3 4
a1,I 6 7 15 3
a2,I 9 4 8 7
t1,I 3 5 2
t2,I 1 2 1
ei 2 3
Xi 1 4
J 1 2 3 4
f1[j] 8 15 20 27
f2[j] 12 15 23 30
J 2 3 4
l1[j] 1 1 2
l2[j] 1 2 2
F* 28 l*=1S
J 1 2 3 4 5 6
a1,I 7 9 3 4 8 4
a2,I 8 5 6 4 5 7
t1,I 2 3 1 3 4
t2,I 2 1 2 2 1
ei 2 4
Xi 3 3
Example 3
J 1 2 3 4 5 6
f1[j] 9 18 20 24 32 35
f2[j] 12 16 22 25 30 37
J 2 3 4 5 6
l1[j] 1 2 1 1 2
l2[j] 1 `2 1 2 2
F* 38 l*=1
Line 1 station 6Line 2 station 5Line 2 station 4Line 1 station3Line 2 station 2Line 1 station 1
J 1 2 3 4
a1,I 9 3 4 8
a2,I 5 6 5 7
t1,I 3 1 4
t2,I 1 2 1
ei 2 4
Xi 3 2
Example with four station
J 1 2 3 4
f1[j] 11 13 17 25
f2[j] 9 15 19 26
J 2 3 4
l1[j]
2 1 1
l2[j]
2 1 2
F* = 28 l*=2
Line 2 station 4Line 2 station3Line 1 station 2Line 2 station 1