Dynamic programing
-
Upload
abdur-rehman-muhammadi -
Category
Education
-
view
210 -
download
1
description
Transcript of Dynamic programing
![Page 1: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/1.jpg)
Advanced Algorithm Analysis
![Page 2: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/2.jpg)
Dynamic Programming
Problem having multiple solution
Dynamic Programming used to solve Optimization Problem
Dynamic Programming is used to find solution with optimal value.
![Page 3: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/3.jpg)
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
![Page 4: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/4.jpg)
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
![Page 5: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/5.jpg)
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
![Page 6: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/6.jpg)
Assembly Line Scheduling
![Page 7: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/7.jpg)
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.
![Page 8: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/8.jpg)
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
![Page 9: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/9.jpg)
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
![Page 10: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/10.jpg)
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
![Page 11: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/11.jpg)
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
![Page 12: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/12.jpg)
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
![Page 13: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/13.jpg)
Solution- Step 01
![Page 14: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/14.jpg)
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
![Page 15: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/15.jpg)
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
![Page 16: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/16.jpg)
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
![Page 17: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/17.jpg)
Solution-Step 03
Optimal Value
› f* = 38
› l* = 1
![Page 18: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/18.jpg)
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
![Page 19: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/19.jpg)
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
![Page 20: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/20.jpg)
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
![Page 21: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/21.jpg)
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
![Page 22: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/22.jpg)
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
![Page 23: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/23.jpg)
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
![Page 24: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/24.jpg)
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
![Page 25: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/25.jpg)
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
![Page 26: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/26.jpg)
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
![Page 27: Dynamic programing](https://reader036.fdocuments.net/reader036/viewer/2022062312/5554a953b4c905fd608b50c1/html5/thumbnails/27.jpg)
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