Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor Neelima Gupta Instructor: Ms. Neelima Gupta.

Similar presentations


Presentation on theme: "Instructor Neelima Gupta Instructor: Ms. Neelima Gupta."— Presentation transcript:

1 Instructor Neelima Gupta ngupta@cs.du.ac.in

2 Instructor: Ms. Neelima Gupta

3 Interval/Job Scheduling –a greedy approach

4 Weighted Interval Scheduling – Each job i has (s i, f i, p i ) starting time s i, finishing time f i, and profit p i 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 iS i F i P i 224 3 115 10 346 4 458 20 569 2 Thanks to Neha (16)

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

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

8 Greedy does not work Time 0 P(1)=10 P(4)=20 P(2)=3 1 23456 7 89 P(5)=2 Optimal schedule Schedule chosen by greedy app 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 j th jobs, (jobs are sorted in the increasing order of their finishing times) p j =profit of j th 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] = p j + profit when considering the jobs before and including the last job compatible with j i.e. m[j] = p j + m[p(j)] If it is not, then m[j] = m[j-1] Thus, m[j]= max(p j + 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 INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 Thanks to : Nikita Khanna(19)

13 INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 P[2]=0

14 Thanks to : Nikita Khanna(19) INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 P[2]=0 P[3]=1

15 Thanks to : Nikita Khanna(19) INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 P[2]=0 P[3]=1 P[4]= 0

16 Thanks to : Nikita Khanna(19) INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 P[2]=0 P[3]=1 P[4]= 0 P[5]=3

17 Thanks to : Nikita Khanna(19) INDE X 1 2 3 4 5 6 V 1 =2 V 2 =4 V 3 =4 V 4 =7 V 5 =2 V 6 =1 P[1]=0 P[2]=0 P[3]=1 P[4]= 0 P[5]=3 P[6]=3

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 Recursive Solution (Optimal Substructure Property) Overlapping Subproblems Total Number of Subproblems is polynomial

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

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

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

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

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

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

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

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

29 Definition:- are the matrix of order respectively. e.g. are the matrix of order (2 x 3), (3 x 4), ( 4 x 5) respectively. = (2 x 3 x 4 )+ (2 x 4 x 5) TOTAL MULTIPLICATION= 24+ 40= 64 = (3 x 4 x 5)+ (2 x 3x 5) =90 So, we have seen that by changing the evaluation sequence cost of operation also changes. Matrix Chain Multiplication THANKS TO:OM JI(26)

30 Recursive Solution In general,

31 Overlapping Subproblems m[1………4] m[1],m[2,3,4] m[1,2],m[3,4] m[1,2,3],m[4] m[2],m[3,4] m[2,3],m[4] m[1],m[2,3] m[1,2],m[3] THANKS TO: OM JI(26)

32 Number of sub-problems M[i……………j] i <= j n choices n choices Number of subproblems= n 2 …..polynomial THANKS TO: OM JI(26)

33 Matrix dimensions: A 1 : 3 × 5 A 2 : 5 × 4 A 3 : 4 × 2 A 4 : 2 × 7 A 5 : 7 × 3 A 6 : 3 × 8 Example: THANKS TO: OM JI(26)

34 Problem of parenthesization A1A1 A2A2 A3A3 A4A4 A5A5 A6A6 A1A1 0 60 A2A2 -0 40 A3A3 - -0 56 A4A4 - - -0 42 A5A5 - - - -0 168 A6A6 - - - - -0 A 1 *A 2 = 3*5*4 = 60 A 2 *A 3 = 5*4*2 = 40 A 3 *A 4 = 4*2*7 = 56 A 4 *A 5 = 2*7*3 = 42 A 5 *A 6 = 7*3*8 = 168 THANKS TO : OM JI(26)

35 Problem of parenthesization A 1 _A 3 = A 1 *A 2 *A 3 = min( (A 1.A 2 ).A 3 =60+ 3*4*2 =84 or A 1.(A 2.A 3 )=40+ 3*5*2=70) =70 A 2 _A 4 = A 2 *A 3 *A 4 = min( (A 2.A 3 ).A 4 =40+ 5*2*7=110 or A2.(A3.A4)=56+ 5*4*7 =196) =110 A 3 _A 5 = A 3 *A 4 *A 5 = min( (A 3.A 4 ).A 5 =56+4*7*3=140 or A 3.(A 4.A 5 )=42+ 4*2*3 =66)= 66 A 4 _A 6 = A 4 *A 5 *A 6 = min( (A 4 *A 5 )*A 6 =42+2*3*8=90 or A 4 *(A 5 *A 6 )=168+ 2*7*8 =312 ) =90 A1A1 A2A2 A3A3 A4A4 A5A5 A6A6 A1A1 0 6070 A2A2 -0 40 1 10 A3A3 - -0 56 66 A4A4 - - -0 42 90 A5A5 - - - -0 168 A6A6 - - - - -0 THANKS TO: OM JI(26)

36 Problem of parenthesization A1A1 A2A2 A3A3 A4A4 A5A5 A6A6 A1A1 0 6070 K=1 112 K=3 130 K=3 202 K=5 A2A2 -0 40 1 10 K=3 112 K=3 218 K=3 A3A3 - -0 56 66 K=3 154 K=3 A4A4 - - -0 42 90 K=5 A5A5 - - - -0 168 A6A6 - - - - -0 THANKS TO: OM JI(26)

37 Running Time Ο(n 2 ) entries each takes O(n) time to compute The running time of this procedure is Ο(n 3 ). Thanks to: OM JI(26)

38 Segmented Least Squares: Multi-way Choices

39 Given a set P of n points in a plane denoted by (x 1,y 1 ), (x 2,y 2 ),..., (x n,y n ) and line L defined by the equation y = ax +b, error of line L with respect to P is the sum of squares of the distances of these points from L. Error(L, P) = ……Parila….. A line of ‘best fit’

40 Line of Best Fit Line of Best Fit is the one that minimizes this error. This can be computed in O(n) time using the following formula: Parila The formula is obtained by differentiating the error wrt a and equating to zero and wrt b and equating to zero

41 Consider the following scenario: Clearly approximating these points with a single line is not a good idea. A set of points that lie approximately on two lines Using two lines clearly gives a better approximation.

42 (contd.) A set of points that lie approximately on three lines In this case, 3 lines will give us a better approximation.

43 Segmented Least Square Error In all these cases, we are able to tell the number of lines we must use by looking at the points. In general, we don’t know this number. That is we don’t know what is the minimum number of lines we must use to get a good approximation. Thus our aim becomes to minimize the number of lines that minimize the least square error.

44 DESIGNING THE ALGORITHM We know that the last point p n belongs to a single segment in the optimal partition and this segment begins at some point, say p i. Thus, if we know the identity of the last segment, then we can recursively solve the problem on the remaining points p i … p i-1 as illustrated below:

45 WRITING THE RECURRENCE If the last segment in the optimal is p i, …, p n, then the value of optimal is, SLS(n)= SLS(i-1) + c + e in where c is the cost of using the segment and e in is the least square error of this segment. But since we don’t know i, we’ll compute it as follows: SLS(n)= min i { SLS(i-1) + c + e ij } General recurrence: For the sub-problem p 1, …, p j, SLS(j)= min i { SLS(i-1) + c + e ij }

46 Time Analysis: e ij values can be pre-computed in O(n 3 ) time. Additional Time: n entries, each entry computes the minimum of at most n values each of which can be computed in constant time. Thus a total of O(n 2 ).

47 FRACTIONAL KNAPSACK PROBLEM Given a set S of n items, with value v i and weight w i 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.

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

49 Example knapsack capacity 50 Item 2 item 3 Item 1 ● v i = 60 v i = 100 v i = 120 v i/ w i = 6 v i/ w i = 5 v i/ w i = 4 10 20 30 Thanks to: Neha Katyal

50 Example knapsack capacity 50 Item 2 item 3 ● ● 60 v i = 100 v i = 120 v i/ w i = 5 v i/ w i = 4 20 30 10 Thanks to: Neha Katyal

51 Example knapsack capacity 50 item 3 100 ● + ● 60 v i = 120 v i/ w i = 4 30 10 20 Thanks to: Neha Katyal

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

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

54 GREEDY APPROACH DOESN’T WORK FOR 0-1 KNAPSACK Counter Example: knapsack Item 2 item 3 Item 1 ● v i = $60 v i = $100 v i = $120 v i/ w i = 6 v i/ w i = 5 v i/ w i = 4 10 20 30 Thanks to: Neha Katyal

55 Example knapsack Item 2 item 3 ● ● $60 v i = $100 v i = $120 v i/ w i = 5 v i/ w i = 4 20 30 10 Thanks to: Neha Katyal

56 Example knapsack item 3 $100 ● + ● $60 v i = $120 = $160 v i/ w i = 4 suboptimal 30 10 20 Thanks to: Neha Katyal

57 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

58 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)

59 000000 0 0 0 0 W012345W012345 i01234i01234 (2,3), (3,4), (4,5), (5,6)

60 000000 00 0 0 0 W012345W012345 i01234i01234 As w<w 1 ; m[1,1] = m[1-1,1] = m[0,1] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

61 000000 00 00 0 0 W012345W012345 i01234i01234 As w<w 2 ; m[2,1] = m[2-1,1] = m[1,1] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

62 000000 00 00 00 0 W012345W012345 i01234i01234 As w<w 3 ; m[3,1] = m[3-1,1] = m[2,1] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

63 000000 00 00 00 00 W012345W012345 i01234i01234 As w<w 4 ; m[4,1] = m[4-1,1] = m[3,1] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

64 000000 003 00 00 00 W012345W012345 i01234i01234 As w>=w 1 ; m[1,2] = max{m[1-1,2], m[1-1,2-2]+3} =max{ 0,0+3} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

65 000000 003 003 00 00 W012345W012345 i01234i01234 As w<w 2 ; m[2,2] = m[2-1,2] = m[1,2] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

66 000000 003 003 003 00 W012345W012345 i01234i01234 As w<w 3 ; m[3,2] = m[3-1,2] = m[2,2] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

67 000000 003 003 003 003 W012345W012345 i01234i01234 As w<w 4 ; m[4,2] = m[4-1,2] = m[3,2] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

68 000000 0033 003 003 003 W012345W012345 i01234i01234 As w>=w 1 ; m[1,3] = max{m[1-1,3], m[1-1,3-2]+3} =max{ 0,0+3} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

69 000000 0033 0034 003 003 W012345W012345 i01234i01234 As w>=w 2 ; m[2,3] = max{m[2-1,3], m[2-1,3-3]+4} =max{ 3,0+4} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

70 000000 0033 0034 0034 003 W012345W012345 i01234i01234 As w<w 3 ; m[3,3] = m[3-1,3] = m[2,3] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

71 000000 0033 0034 0034 0034 W012345W012345 i01234i01234 As w<w 4 ; m[4,3] = m[4-1,3] = m[3,3] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

72 000000 00333 0034 0034 0034 W012345W012345 i01234i01234 As w>=w 1 ; m[1,4] = max{m[1-1,4], m[1-1,4-2]+3} =max{ 0,0+3} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

73 000000 00333 00344 0034 0034 W012345W012345 i01234i01234 As w>=w 2 ; m[2,4] = max{m[2-1,4], m[2-1,4-3]+4} =max{ 3,0+4} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

74 000000 00333 00344 00345 0034 W012345W012345 i01234i01234 As w>=w 3 ; m[3,4] = max{m[3-1,4], m[3-1,4-4]+5} =max{ 4,0+5} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

75 000000 00333 00344 00345 00345 W012345W012345 i01234i01234 As w<w 4 ; m[4,4] = m[4-1,4] = m[3,4] Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

76 000000 003333 00344 00345 00345 W012345W012345 i01234i01234 As w>=w 1 ; m[1,5] = max{m[1-1,5], m[1-1,5-2]+3} =max{ 0,0+3} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

77 000000 003333 003447 00345 00345 W012345W012345 i01234i01234 As w>=w 2 ; m[2,5] = max{m[2-1,5], m[2-1,5-3]+4} =max{ 3,3+4} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

78 000000 003333 003447 003457 00345 W012345W012345 i01234i01234 As w>=w 3 ; m[3,5] = max{m[3-1,5], m[3-1,5-4]+5} =max{ 7,0+5} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

79 000000 003333 003447 003457 003457 W012345W012345 i01234i01234 As w>=w 4 ; m[4,5] = max{m[4-1,5], m[4-1,5-5]+6} =max{ 7,0+6} Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

80 000000 003333 003447 003457 003457 W01 2345 i01234i01234 Thanks to : Neha Katyal (17, MCS '11), Shikha Walia (27, MCS'11) (2,3), (3,4), (4,5), (5,6)

81 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.

82 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.

83 SUBMITTED BY: BY PULKIT OHRI(29)

84 84 STRING SIMILARITY: ocurrance occurrence THANKS TO PULKIT ocurrance ccurrenceo - ocurrnce ccurrnceo --a e- o cu rrance ccurrenceo - 6 mismatches, 1 gap 1 mismatch, 1 gap 0 mismatch, 3 gaps

85 PROBLEM: Given two strings X = x 1 x 2... x n and Y = y 1 y 2.... y m GOAL: Find an alignment of minimum cost, where δ is the cost of a gap and ᾳ is the cost of a mismatch. Let OPT(i,j)=min cost of aligning strings X = x 1 x 2... x i and Y = y 1 y 2...y j THANKS TO PULKIT 85

86 4 cases: Case1: x i is aligned with y j in OPT and they match Pay mismatch for x i y j + min cost of aligning two strings Case 2: OPT does not matches x i. Pay gap for x i + min cost of aligning x 1 x 2... x i-1 and y 1 y 2... y j Case 3: OPT does not matches y j. Pay gap for y j + min cost of aligning x 1 x 2... x i and y 1 y 2... Y j THANKS TO PULKIT 86

87 87

88 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e THANKS TO PULKIT 88 0

89 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e Advance name but not mean.So,pay gap cost δ=2. THANKS TO PULKIT 89 0 2

90 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e δ=2+2=4 THANKS TO PULKIT 90 0 2 4

91 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e δ =4+2=6 THANKS TO PULKIT 91 0 2 4 6

92 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e δ =6+2=8 THANKS TO PULKIT 92 0 2 4 6 8

93 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e THANKS TO PULKIT 93 8 6 4 2 0 2 4 6 8

94 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e Advance both name and mean and pay mismatch cost ᾳ=1 -m… -n … Pay mismatch cost ᾳ=1 THANKS TO PULKIT 94 8 6 4 2 (1) 0 2 4 6 8

95 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e Advance mean,pay gap cost δ=2+2=4. - n - - - m 0,2,2 THANKS TO PULKIT 95 8 6 4 2 (4) 0 2 4 6 8

96 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e Advance name,pay gap cost cost δ=2+2=4 - m - - - n 0, 2,2 THANKS TO PULKIT 96 8 6 4 2 (4) 0 2 4 6 8

97 EG: let δ=2 and ᾳ=0…3 mean name n a e m n a m e So, took the min. 1 THANKS TO PULKIT 97 8 6 4 2 1 0 2 4 6 8

98 Similarly, n a e m - - n a m e RUNNING TIME:  (mn) THANKS TO PULKIT 98 8 6 5 4 6 6 5 3 5 5 4 3 2 4 4 2 1 3 4 6 0 2 4 6 8

99


Download ppt "Instructor Neelima Gupta Instructor: Ms. Neelima Gupta."

Similar presentations


Ads by Google