1 / 66

MCS 101: Algorithms

MCS 101: Algorithms. Instructor Neelima Gupta ngupta@cs.du.ac.in. Dynamic Programming. Instructor: Ms. Neelima Gupta. Interval/Job Scheduling –a greedy approach. Weighted Interval Scheduling –. Each job i has (s i , f i , p i ) starting time s i , finishing time f i , and

talia
Download Presentation

MCS 101: Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MCS 101: Algorithms Instructor Neelima Gupta ngupta@cs.du.ac.in

  2. Dynamic Programming Instructor: Ms. Neelima Gupta

  3. Interval/Job Scheduling –a greedy approach

  4. Weighted Interval Scheduling – • Each jobi has (si , fi , pi) starting time si , finishing time fi, and profit pi • Aim: Find an optimal schedule of job that makes the maximum profit. • Greedy approach : choose the job which finishes first…..does not work. So, DP Thanks to Neha (16)

  5. i Si Fi Pi • 2 2 4 3 • 1 1 5 10 • 3 4 6 4 • 4 5 8 20 • 5 6 9 2 Thanks to Neha (16)

  6. Weighted Interval Scheduling P(1)=10 P(2)=3 P(3)=4 P(4)=20 P(5)=2 Time 0 1 7 2 3 4 5 6 8 9 Thanks to Neha (16)

  7. Greedy Approach P(1)=10 P(2)=3 P(3)=4 P(4)=20 P(5)=2 Time 0 1 7 2 3 4 5 6 8 9 . Thanks to Neha (16)

  8. Greedy does not work P(1)=10 P(2)=3 Optimal schedule P(3)=4 P(4)=20 Schedule chosen by greedy app P(5)=2 Time 0 1 7 2 3 4 5 6 8 9 Greedy approach takes job 2, 3 and 5 as best schedule and makes profit of 7. While optimal schedule is job 1 and job4 making profit of 30 (10+20). Hence greedy will not work Thanks to Neha (16)

  9. DP Solution for WIS • Let m[j]= optimal schedule solution from the first jth jobs, (jobs are sorted in the increasing order of their finishing times) pj=profit of jth job. p[j] =largest index i<j , such that interval i and j are disjoint i.e. i is the rightmost interval that ends before j begins or the last interval compatible with j and is before j. Thanks to : Neha Mishra (roll no:18)

  10. DP Solution for WIS • Either j is in the optimal solution or it is not. • If it is, then m[j] = pj + profit when considering the jobs before and including the last job compatible with j i.e. m[j] = pj + m[p(j)] • If it is not, then m[j] = m[j-1] • Thus, m[j]= max(pj + m[p(j)], m[j-1])

  11. Recursive Paradigm • Write a recursive function to compute m[n]…afterall we are only interested in that. • some of the problems are solved several times leading to exponential time.

  12. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 V2=4 V3=4 V4=7 V5=2 V6=1 Thanks to : Nikita Khanna(19)

  13. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 P[2]=0 V2=4 V3=4 V4=7 V5=2 V6=1 Thanks to : Nikita Khanna(19)

  14. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 P[2]=0 V2=4 P[3]=1 V3=4 V4=7 V5=2 V6=1 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  15. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 P[2]=0 V2=4 P[3]=1 V3=4 P[4]=0 V4=7 V5=2 V6=1 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  16. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 P[2]=0 V2=4 P[3]=1 V3=4 P[4]=0 V4=7 V5=2 P[5]=3 V6=1 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  17. INDEX 1 2 3 4 5 6 P[1]=0 V1=2 P[2]=0 V2=4 P[3]=1 V3=4 P[4]=0 V4=7 V5=2 P[5]=3 V6=1 P[6]=3 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  18. Recursive Paradigm : tree of recursion for WIS

  19. Recursive Paradigm • Sometimes some of the problems are solved several times leading to exponential time. For example: Fibonacci numbers. • DP Solution: Solve a problem only once and use it as and when required • Top-down DP …..Memoization….generally storage cost is large • Bottom-Up DP …….Iterative

  20. Principles of DP

  21. m[j] = max{m[j-1], m[p[j] ] + pj} m 0 1 2 3 4 5 6 Thanks to : Nikita Khanna(19)

  22. m[j] = max{m[j-1], m[p[j] ] + pj} 0 m 0 1 2 3 4 5 6 Thanks to : Nikita Khanna(19)

  23. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} 0 2 m 0 1 2 3 4 5 6 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  24. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} Max{2,0+4} 0 2 4 m 0 1 2 3 4 5 6 Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  25. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} Max{2,0+4} 0 2 4 6 m 0 1 2 3 4 5 6 Max{4,2+4} Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) )

  26. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} Max{6,0+7} Max{2,0+4} 0 2 4 6 7 m 0 1 2 3 4 5 6 Max{4,2+4} Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  27. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} Max{6,0+7} Max{2,0+4} 0 2 4 6 7 8 m 0 1 2 3 4 5 6 Max{7,6+2} Max{4,2+4} Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  28. m[j] = max{m[j-1], m[p[j] ] + pj} Max{0,0+2} Max{8,6+1} Max{6,0+7} Max{2,0+4} 0 2 4 6 7 8 8 m 0 1 2 3 4 5 6 Max{7,6+2} Max{4,2+4} Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)

  29. FRACTIONAL KNAPSACK PROBLEM Given a set S of n items, with value vi and weight wi and a knapsack with capacity W. Aim: Pick items with maximum total value but with weight at mostW. You may choose fractions of items.

  30. GREEDY APPROACH • Pick the items in the decreasing order of value per unit weight i.e. highest first.

  31. Example knapsack capacity 50 Item 2 item 3 Item 1 • vi = 60vi = 100 vi = 120 vi/wi = 6 vi/wi = 5 vi/wi = 4 30 20 10 Thanks to: Neha Katyal

  32. Example knapsack capacity 50 Item 2 item 3 • 60 vi = 100 vi = 120 vi/wi = 5 vi/wi = 4 30 20 10 Thanks to: Neha Katyal

  33. Example knapsack capacity 50 item 3 100 • + • 60 vi = 120 vi/wi = 4 20 30 10 Thanks to: Neha Katyal

  34. Example knapsack capacity 50 $80 + 100 + 60 = 240 20/30 20 10 Thanks to: Neha Katyal

  35. 0-1 Kanpsack • example to show that the above greedy approach will not work, So, DP

  36. GREEDY APPROACH DOESN’T WORK FOR 0-1 KNAPSACK Counter Example: knapsack Item 2 item 3 Item 1 • vi = $60vi = $100 vi = $120 vi/wi = 6 vi/wi = 5 vi/wi = 4 30 20 10 Thanks to: Neha Katyal

  37. Example knapsack Item 2 item 3 • $60 vi = $100 vi = $120 vi/wi = 5 vi/wi = 4 30 20 10 Thanks to: Neha Katyal

  38. Example knapsack item 3 $100 • + • $60 vi = $120 = $160 vi/wi = 4 suboptimal 20 30 10 Thanks to: Neha Katyal

  39. Fractional Knapsack –a greedy approach

  40. DP Solution for 0-1KS Let m[I,w] be the optimal value obtained when considering objects upto I and filling a knapsack of capacity w • m[0,w] = 0 • m[i,0] = 0 • m[i,w] = m[i-1,w] if wi > w • m[i,w] = max{m[i-1, w-wi] + vi , m[i-1, w]} if wi <= w Running Time : Pseudo-polynomial

  41. Example • n = 4 • W = 5 • Elements (weight, value): (2,3), (3,4), (4,5), (5,6) Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  42. (2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  43. (2,3), (3,4), (4,5), (5,6) As w<w1 ; m[1,1] = m[1-1,1] = m[0,1] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  44. (2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,1] = m[2-1,1] = m[1,1] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  45. (2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,1] = m[3-1,1] = m[2,1] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  46. (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,1] = m[4-1,1] = m[3,1] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  47. (2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,2] = max{m[1-1,2] , m[1-1,2-2]+3} =max{ 0,0+3} W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  48. (2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,2] = m[2-1,2] = m[1,2] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  49. (2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,2] = m[3-1,2] = m[2,2] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

  50. (2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,2] = m[4-1,2] = m[3,2] W 0 1 2 3 4 5 i 0 1 2 3 4 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11)

More Related