Download presentation
Presentation is loading. Please wait.
1
MCA 301: Design and Analysis of Algorithms
Instructor Neelima Gupta
2
Instructor: Ms. Neelima Gupta
Dynamic Programming Instructor: Ms. Neelima Gupta
3
Table of Contents Weighted Interval Scheduling 0-1 Knapsack
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. Thanks to Neha (16)
5
Greedy Approaches
6
Increasing Finishing Times
Job1=20 Job2=2 Job3=5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012
7
Increasing Finishing Times
Job1=20 Job2=2 Job3=5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012
8
Increasing Finishing Times
Job1=20 Job2=2 Job3=5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 SCHEDULE CHOSEN BY THIS APPROACH Time OPTIMAL SCHEDULE Thanks to: Navneet Kaur(22), MCA 2012
9
Decreasing Order of Profit/Processing Time
job1 (Profit=100) job2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 Time Thanks to: Navneet Kaur(22), MCA 2012
10
Decreasing Order of Profit/Processing Time
job1 (Profit=100) job2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 Time Thanks to: Navneet Kaur(22), MCA 2012
11
Decreasing Order of Profit/Processing Time
job1 (Profit=100) job2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 SCHEDULE CHOSEN BY THIS APPROACH Time OPTIMAL SCHEDULE Thanks to: Navneet Kaur(22), MCA 2012
12
Maximum Profit First Job1=15 Job2=10 Job3=10 Job4=10 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012
13
Maximum Profit First Job1=15 Job2=10 Job3=10 Job4=10 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012
14
Maximum Profit First Job1=15 Job2=10 Job3=10 Job4=10 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 SCHEDULE CHOSEN BY THIS APPROACH Time OPTIMAL SCHEDULE Thanks to: Navneet Kaur(22), MCA 2012
15
i Si Fi Pi Thanks to Neha (16)
16
Weighted Interval Scheduling
P(1)=10 P(2)=3 P(3)=4 P(4)=20 P(5)=2 1 Time 2 3 4 5 6 7 8 9 Thanks to Neha (16)
17
Greedy Approach Thanks to Neha (16) P(1)=10 P(2)=3 P(3)=4 P(4)=20
1 Time 2 3 4 5 6 7 8 9 . Thanks to Neha (16)
18
Greedy does not work Optimal schedule Schedule chosen by greedy app
1 Time 2 3 4 5 6 7 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)
19
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)
20
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])
21
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.
22
P[1]=0 INDEX 1 V1=2 2 V2=4 3 4 V3=4 5 6 V4=7 V5=2 V6=1
Thanks to : Nikita Khanna(19)
23
P[1]=0 P[2]=0 INDEX 1 V1=2 2 V2=4 3 4 V3=4 5 6 V4=7 V5=2 V6=1
Thanks to : Nikita Khanna(19)
24
Thanks to : Nikita Khanna(19)
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)
25
P[1]=0 P[2]=0 P[3]=1 P[4]=0 INDEX 1 V1=2 2 V2=4 3 4 V3=4 5 6 V4=7 V5=2
Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
26
P[1]=0 P[2]=0 P[3]=1 P[4]=0 P[5]=3 INDEX 1 V1=2 2 V2=4 3 4 V3=4 5 6
Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
27
P[1]=0 P[2]=0 P[3]=1 P[4]=0 P[5]=3 P[6]=3 INDEX 1 V1=2 2 V2=4 3 4 V3=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
Recursive Paradigm : tree of recursion for WIS
29
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
30
Principles of DP
31
m[j] = max{m[j-1], m[p[j] ] + pj}
Thanks to : Nikita Khanna(19)
32
m[j] = max{m[j-1], m[p[j] ] + pj}
m Thanks to : Nikita Khanna(19)
33
m[j] = max{m[j-1], m[p[j] ] + pj}
2 m Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
34
m[j] = max{m[j-1], m[p[j] ] + pj}
2 4 m Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
35
m[j] = max{m[j-1], m[p[j] ] + pj}
2 4 6 m Max{4,2+4} ) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
36
m[j] = max{m[j-1], m[p[j] ] + pj}
2 4 6 7 m Max{4,2+4} Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19) Thanks to : Nikita Khanna(19)
37
m[j] = max{m[j-1], m[p[j] ] + pj}
2 4 6 7 8 m 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)
38
m[j] = max{m[j-1], m[p[j] ] + pj}
2 4 6 7 8 8 m 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)
39
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 most W. You may choose fractions of items.
40
GREEDY APPROACH Pick the items in the decreasing order of value per unit weight i.e. highest first.
41
Example Item 2 item 3 vi = 60 vi = 100 vi = 120
knapsack capacity 50 Item item 3 Item 1 vi = vi = vi = 120 vi/ wi = vi/ wi = vi/ wi = 4 30 20 10 Thanks to: Neha Katyal
42
Example Item 2 item 3 vi = 100 vi = 120 vi/ wi = 5 vi/ wi = 4 30 20 10
knapsack capacity 50 Item item 3 60 vi = vi = 120 vi/ wi = vi/ wi = 4 30 20 10 Thanks to: Neha Katyal
43
Example item 3 vi = 120 20 vi/ wi = 4 30 10 Thanks to: Neha Katyal 100
knapsack capacity 50 item 3 100 + 60 vi = 120 vi/ wi = 4 20 30 10 Thanks to: Neha Katyal
44
Example $80 + = 240 20/30 20 10 Thanks to: Neha Katyal 100
knapsack capacity 50 $80 + 100 60 = 240 20/30 20 10 Thanks to: Neha Katyal
45
0-1 Kanpsack example to show that the above greedy approach will not work, So, DP
46
GREEDY APPROACH DOESN’T WORK FOR 0-1 KNAPSACK
Counter Example: knapsack Item item 3 Item 1 vi = $ vi = $100 vi = $120 vi/ wi = vi/ wi = vi/ wi = 4 30 20 10 Thanks to: Neha Katyal
47
Example Item 2 item 3 vi = $100 vi = $120 vi/ wi = 5 vi/ wi = 4 30 20
knapsack Item item 3 $60 vi = $100 vi = $120 vi/ wi = vi/ wi = 4 30 20 10 Thanks to: Neha Katyal
48
Example item 3 vi = $120 = $160 20 vi/ wi = 4 30 suboptimal 10
knapsack item 3 $100 + $60 vi = $ = $160 vi/ wi = 4 suboptimal 20 30 10 Thanks to: Neha Katyal
49
Fractional Knapsack –a greedy approach
50
Brute force Approach for 0-1 KS
We want to try solution for all possible weights. Let B be the capacity of knapsack, so we generate those subsets with weights less than or equal to B, i.e. subsets for weights 1,2,3,……..,B and among those subsets choose the one wid the maximum value.
51
Let the items be x1 , x2 , x3 , …………, xn Let m[i,w] = optimal solution with [x1 , x2 , …… xi] with weight exactly equal to w
52
Example n = 4 W = 5 Elements (weight, value):
(2,3), (3,4), (4,5), (5,6)
53
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
54
For the first row and first column, all the entries are filled with zero.
For the second row, the only item is x1 with weight 2 and value 3 hence it is put in the table.
55
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3
56
For the third row items available are x1 , x2 hence the possible subsets are :
{x1} with weight 2 and value 3 {x2} with weight 3 and value 4 {x1 , x2} with weight 2+3 =5 and value 3+4 =7
57
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7
58
For the fourth row items available are x1 , x2 and x3 hence the possible subsets are :
{x1} with weight 2 and value 3 {x2} with weight 3 and value 4 {x1 , x2} with weight 2+3 =5 and value 3+4 =7 {x3} with weight 4 and value 5 {x1 , x3} with weight 6 (This is greater than the capacity of knapsack, W=5 hence discard this subset) {x2 , x3} with weight 7 (Again greater than W so discard) {x1 , x2 , x3} with weight 9 (>5 so discard) Hence the only value added will be for the subset with weight 4 and rest all values will be the same as for the previous row because the subsets of the previous row are subsets of the current row as well.
59
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7 5
60
For the fifth row items available are x1 , x2 , x3 and x4 hence the possible subsets are the previous ones and, {x4} with weight 5 and value 6 {x1 , x4} with weight 7 (>5 so discard) {x2 , x4} with weight 8 (>5 so discard) {x1 , x2 , x4} with weight 10 (>5 so discard) {x3 , x4} with weight 9 (>5 so discard) Now for weight 5 the value present in the table was 7 and the value obtained by the subset {x4} is 6. Since 7>6 hence the value 7 is retained in the table.
61
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7 5
62
Time Complexity Trying out all possible subsets to compute its total weight is exponential.
63
DP Solution for 0-1KS The recursive approach to the problem is :
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-w(xi)] + v(xi) , m[i-1, w] } if wi <= w Running Time : ѳ (n*W) i.e. the size of the table
64
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
65
As w<w1 ; m[1,1] = m[1-1,1] = m[0,1]
(2,3), (3,4), (4,5), (5,6) As w<w1 ; m[1,1] = m[1-1,1] = m[0,1] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
66
As w<w2 ; m[2,1] = m[2-1,1] = m[1,1]
(2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,1] = m[2-1,1] = m[1,1] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
67
As w<w3 ; m[3,1] = m[3-1,1] = m[2,1]
(2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,1] = m[3-1,1] = m[2,1] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
68
As w<w4 ; m[4,1] = m[4-1,1] = m[3,1]
(2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,1] = m[4-1,1] = m[3,1] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}
69
As w>=w1 ; m[1,2] = max{m[1-1,2] , m[1-1,2-2]+3} =max{ 0,0+3}
(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 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}
70
As w<w2 ; m[2,2] = m[2-1,2] = m[1,2]
(2,3), (3,4), (4,5), (5,6) As w<w2 ; m[2,2] = m[2-1,2] = m[1,2] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}
71
As w<w3 ; m[3,2] = m[3-1,2] = m[2,2]
(2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,2] = m[3-1,2] = m[2,2] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}
72
As w<w4 ; m[4,2] = m[4-1,2] = m[3,2]
(2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,2] = m[4-1,2] = m[3,2] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}
73
As w>=w1 ; m[1,3] = max{m[1-1,3] , m[1-1,3-2]+3} =max{ 0,0+3}
(2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,3] = max{m[1-1,3] , m[1-1,3-2]+3} =max{ 0,0+3} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}
74
As w>=w2 ; m[2,3] = max{m[2-1,3] , m[2-1,3-3]+4} =max{ 3,0+4}
(2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,3] = max{m[2-1,3] , m[2-1,3-3]+4} =max{ 3,0+4} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}
75
As w<w3 ; m[3,3] = m[3-1,3] = m[2,3]
(2,3), (3,4), (4,5), (5,6) As w<w3 ; m[3,3] = m[3-1,3] = m[2,3] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}
76
As w<w4 ; m[4,3] = m[4-1,3] = m[3,3]
(2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,3] = m[4-1,3] = m[3,3] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}
77
As w>=w1 ; m[1,4] = max{m[1-1,4] , m[1-1,4-2]+3} =max{ 0,0+3}
(2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,4] = max{m[1-1,4] , m[1-1,4-2]+3} =max{ 0,0+3} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}
78
As w>=w2 ; m[2,4] = max{m[2-1,4] , m[2-1,4-3]+4} =max{ 3,0+4}
(2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,4] = max{m[2-1,4] , m[2-1,4-3]+4} =max{ 3,0+4} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}
79
As w>=w3 ; m[3,4] = max{m[3-1,4] , m[3-1,4-4]+5} =max{ 4,0+5}
(2,3), (3,4), (4,5), (5,6) As w>=w3 ; m[3,4] = max{m[3-1,4] , m[3-1,4-4]+5} =max{ 4,0+5} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}
80
As w<w4 ; m[4,4] = m[4-1,4] = m[3,4]
(2,3), (3,4), (4,5), (5,6) As w<w4 ; m[4,4] = m[4-1,4] = m[3,4] W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}
81
As w>=w1 ; m[1,5] = max{m[1-1,5] , m[1-1,5-2]+3} =max{ 0,0+3}
(2,3), (3,4), (4,5), (5,6) As w>=w1 ; m[1,5] = max{m[1-1,5] , m[1-1,5-2]+3} =max{ 0,0+3} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}
82
As w>=w2 ; m[2,5] = max{m[2-1,5] , m[2-1,5-3]+4} =max{ 3,3+4}
(2,3), (3,4), (4,5), (5,6) As w>=w2 ; m[2,5] = max{m[2-1,5] , m[2-1,5-3]+4} =max{ 3,3+4} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}
83
As w>=w3 ; m[3,5] = max{m[3-1,5] , m[3-1,5-4]+5} =max{ 7,0+5}
(2,3), (3,4), (4,5), (5,6) As w>=w3 ; m[3,5] = max{m[3-1,5] , m[3-1,5-4]+5} =max{ 7,0+5} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}
84
As w>=w4 ; m[4,5] = max{m[4-1,5] , m[4-1,5-5]+6} =max{ 7,0+6}
(2,3), (3,4), (4,5), (5,6) As w>=w4 ; m[4,5] = max{m[4-1,5] , m[4-1,5-5]+6} =max{ 7,0+6} W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}
85
(2,3), (3,4), (4,5), (5,6) W i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}
86
Pseudo-polynomial algorithm
An algorithm that is polynomial in the numeric value of the input (which is actually exponential in the size of the input – the number of digits). Thus O(n) time algorithm to test whether n is prime or not is pseudo-polynomial. DP solution to 0-1 Knapsack is pseudo-polynomial as it is polynomial in K, the capacity (one of the inputs) of the Knapsack.
87
Weakly/Strongly NPC problems
An NP complete problem with a known pseudo-polynomial solution is stb weakly NPC. An NPC problem for which it has been proved that it cannot admit a pseudo-polynomial solution unless P= NP is stb strongly NPC.
88
Thank You!
89
WEIGHTED INTERVAL SCHEDULING
Options that could be followed while scheduling the jobs so as to maximise profit: Taking jobs with increasing order of finish time Eg. Three jobs to be scheduled: Job1- start=5, end=10, profit=20 Job2- start=1, end=7, profit=2 Job3- start=8, end=15, profit=5 This option would schedule 2 jobs - job2 and job3 with total profit of 7. But if job1 would have been scheduled, then the profit would have been 20. Hence, jobs that finishes first may have less profit. So this approach is not working. Thanks to: Navneet Kaur(22), MCA 2012
90
WEIGHTED INTERVAL SCHEDULING
Next option that could be followed while scheduling the jobs: Profit/Processing time Eg. Job1- start=1, end=3, profit=10 profit/processing time = 10/2 = 5 Job2- start=2, end=27, profit=100 profit/processing time = 100/25 = 4 This option would schedule job1. But job2 has higher profit. So, this approach is also not working. Thanks to: Navneet Kaur(22), MCA 2012
91
WEIGHTED INTERVAL SCHEDULING
Another option: Selecting job with maximum profit Eg. Job1- start=1, end=15, profit=15 Job2- start=2, end=4, profit=10 Job3- start=5, end=7, profit=10 Job4- start=8, end=15, profit=10 This option would only schedule job1 which has a maximum profit of 15. But, the optimal would have scheduled job2, job3 and job4, yielding a profit of 30. So, this approach is also not working. Thanks to: Navneet Kaur(22), MCA 2012
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.