MCA 301: Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

MCA 301: Design and Analysis of Algorithms
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
Analysis of Algorithms
Instructor Neelima Gupta Table of Contents Greedy Algorithms.
Greedy vs Dynamic Programming Approach
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Week 2: Greedy Algorithms
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
Instructor Neelima Gupta Table of Contents Five representative problems.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Instructor Neelima Gupta Instructor: Ms. Neelima Gupta.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Dynamic Programming … Continued 0-1 Knapsack Problem.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms General principle of greedy algorithm
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Lecture on Design and Analysis of Computer Algorithm
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Weighted Interval Scheduling
CS 3343: Analysis of Algorithms
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
Seminar on Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
CS38 Introduction to Algorithms
Algorithm Design Methods
Prepared by Chen & Po-Chuan 2016/03/29
CS 3343: Analysis of Algorithms
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Greedy Algorithm Enyue (Annie) Lu.
Dynamic Programming General Idea
Exam 2 LZW not on syllabus. 73% / 75%.
Advanced Algorithms Analysis and Design
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Lecture 6 Topics Greedy Algorithm
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Dynamic Programming General Idea
Algorithm Design Methods
Instructor Neelima Gupta
Dynamic Programming.
CSC 413/513- Intro to Algorithms
Lecture 4 Dynamic Programming
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
MCS 312: NP Completeness and Approximation algorthms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Analysis of Algorithms CS 477/677
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Advance Algorithm Dynamic Programming
Dynamic Programming.
Seminar on Dynamic Programming.
Revisit Dynamic Programming
Presentation transcript:

MCA 301: Design and Analysis of Algorithms Instructor Neelima Gupta ngupta@cs.du.ac.in

Instructor: Ms. Neelima Gupta Dynamic Programming Instructor: Ms. Neelima Gupta

Table of Contents Weighted Interval Scheduling 0-1 Knapsack

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)

Greedy Approaches

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

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

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

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

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

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

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

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

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

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)

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)

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)

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)

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)

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

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.

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)

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)

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)

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)

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)

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)

Recursive Paradigm : tree of recursion for WIS

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

Principles of DP

m[j] = max{m[j-1], m[p[j] ] + pj} Thanks to : Nikita Khanna(19)

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

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

m[j] = max{m[j-1], m[p[j] ] + pj} 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)

m[j] = max{m[j-1], m[p[j] ] + pj} 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)

m[j] = max{m[j-1], m[p[j] ] + pj} 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)

m[j] = max{m[j-1], m[p[j] ] + pj} 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)

m[j] = max{m[j-1], m[p[j] ] + pj} 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)

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.

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

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

Example Item 2 item 3 vi = 100 vi = 120 vi/ wi = 5 vi/ wi = 4 30 20 10 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

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

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

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

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

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

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

Fractional Knapsack –a greedy approach

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.

Let the items be x1 , x2 , x3 , …………, xn Let m[i,w] = optimal solution with [x1 , x2 , …… xi] with weight exactly equal to w

Example n = 4 W = 5 Elements (weight, value): (2,3), (3,4), (4,5), (5,6)

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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.

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3

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

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7

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.

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7 5

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.

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 4 7 5

Time Complexity Trying out all possible subsets to compute its total weight is exponential.

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

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 5 {x3}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}

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 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}

(2,3), (3,4), (4,5), (5,6) W 0 1 2 3 4 5 i {x1} {x1 , x2} {x1 , x2 , x3} {x1 , x2 , x3, x4} 3 {x1} 4 {x2} 7 {x1 , x2} 5 {x3}

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.

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.

Thank You!

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

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

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