Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Dynamic programing

165

Published on

Published in: Education, News & Politics
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
165
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1.  Problem having multiple solution  Dynamic Programming used to solve Optimization Problem  Dynamic Programming is used to find solution with optimal value.
• 2.  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
• 3.  It has 4-steps 1. Characterize the structure of Optimal Solution 2. Recursively define the value of an Optimal Solution 3. Compute the value of Optimal Solution, typically in bottom-up fashion 4. Construct the Optimal Solution from computed information
• 4.  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
• 5.  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.
• 6.  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
• 7.  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
• 8.  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 
• 9.  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
• 10. 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
• 11.  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
• 12.  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 
• 13.  Cost and Line 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
• 14.  Optimal Value › f* = 38 › l* = 1
• 15.  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
• 16.  line 1, station 6  line 2, station 5  line 2, station 4  line 1, station 3  line 2, station 2  line 1, station 1
• 17. 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
• 18. 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
• 19. 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
• 20. 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
• 21. 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
• 22. 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 6 Line 2 station 5 Line 2 station 4 Line 1 station3 Line 2 station 2 Line 1 station 1
• 23. 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
• 24. 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 4 Line 2 station3 Line 1 station 2 Line 2 station 1