instructor neelima gupta [email protected]. greedy approach a tool to design algorithms for...

40
MCA 301: Design and Analysis of Algorithms Instructor Neelima Gupta [email protected]

Upload: warren-stott

Post on 31-Mar-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1

Instructor Neelima Gupta [email protected] Slide 2 Greedy Approach A tool to design algorithms for optimization problems Slide 3 What is greedy approach? Choosing a current best solution without worrying about future. In other words the choice does not depend upon future sub-problems. Slide 4 What is greedy approach? Such algorithms are locally optimal, For some problems, as we will see shortly, this local optimal is global optimal also and we are happy. Slide 5 General Greedy Approach Step 1: Choose the current best solution. Step 2: Obtain greedy solution on the rest. Slide 6 When to use? There must be a greedy choice to make. The problem must have an optimal substructure. Slide 7 Activity Selection Problem Given a set of activities, S = {a 1, a 2, , a n } that need to use some resource. Each activity a i has a possible start time s i & finish time f i, such that 0 s i < f i < We need to allocate the resource in a compatible manner, such that the number of activities getting the resource is maximized. The resource can be used by one and only one activity at any given time.. Slide 8 Activity Selection Problem Two activities a i and a j are said to be compatible, if the interval they span do not overlap...i.e. f i s j or f j s i Example: Consider activities: a 1, a 2, a 3, a 4 s 1 --------f 1 s 2 ---------f 2 s 3 ------f 3 s 4 ------f 4 Here a 1 is compatible with a 3 & a 4 a 2 is compatible with a 3 & a 4 But a 3 and a 4 themselves are not compatible. Slide 9 Activity Selection Problem Solution: Applying the general greedy algorithm Select the current best choice, a 1 add it to the solution set. Construct a subset S of all activities compatible with a 1, find the optimal solution of this subset. Join the two. Slide 10 Lets think of some possible greedy solutions Shortest Job First In the order of increasing start times In the order of increasing finish times Slide 11 Time 2 job1 job2 3 45678 9 1011 job3 1 0 1312 Thanks to: Navneet Kaur(22), MCA 2012 1514 Shortest Job First Slide 12 Time 2 job1 job2 3 45678 9 1011 job3 1 0 1312 Thanks to: Navneet Kaur(22), MCA 2012 1514 Shortest Job First Slide 13 Time 2 job1 job2 3 45678 9 1011 job3 1 0 1312 Thanks to: Navneet Kaur(22), MCA 2012 1514 OPTIMAL SCHEDULE SCHEDULE CHOSEN BY THIS APPROACH Shortest Job First Slide 14 Time 0 job1 job2 2 4681012 14 1618 job3 Thanks to: Navneet Kaur(22), MCA 2012 20 Increasing Start Times Slide 15 Time 0 job1 job2 2 4681012 14 1618 job3 Thanks to: Navneet Kaur(22), MCA 2012 20 Increasing Start Times Slide 16 Time 0 job1 job2 2 4681012 14 1618 job3 Thanks to: Navneet Kaur(22), MCA 2012 20 SCHEDULE CHOSEN BY THIS APPROACH OPTIMAL SCHEDULE Increasing Start Times Slide 17 iS i F i P i Thanks to Neha (16) 224 3 115 10 346 4 458 20 569 2 Increasing Finishing Times Slide 18 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) Slide 19 Increasing Finishing Times Time 0 P(1)=10 P(4)=20 P(2)=3 1 23456 7 89 P(5)=2. Thanks to Neha (16) Slide 20 ACTIVITY SELECTION PROBLEM We include a in the solution. And then recurse on S = {a S-{a} : a is compatible with a} where S is input set of activities. Thanks to: Navneet Kaur(22), MCA 2012 Slide 21 ACTIVITY SELECTION PROBLEM CLAIM: If B is an optimal solution of S, then B=B {a 1 } is an optimal solution of S. PROOF: Suppose an imaginary solution B, which is optimal and includes a 1. Suppose length of B, i.e., |B | = k Thanks to: Navneet Kaur(22), MCA 2012 Slide 22 ACTIVITY SELECTION PROBLEM Now, we have to prove two things: I. B is feasible. II. |B| = k OR we can prove that |B | = k - 1 Thanks to: Navneet Kaur(22), MCA 2012 Slide 23 ACTIVITY SELECTION PROBLEM Proof of I. --- B is a subset of S. And S is compatible with a. Hence, B is feasible. Thanks to: Navneet Kaur(22), MCA 2012 Slide 24 ACTIVITY SELECTION PROBLEM Proof of II. --- Consider the set B - {a} i.) Can |B | k ? If yes, then |B {a}| k + 1 Thanks to: Navneet Kaur(22), MCA 2012 Slide 25 ACTIVITY SELECTION PROBLEM But, this is contradiction to a problem that B is optimal because |B | = k And if the size of optimal solution is k, then we cannot have a solution of size greater than k and this is giving a solution of size k +1, which is not possible. Hence, statement (i) is wrong. Thanks to: Navneet Kaur(22), MCA 2012 Slide 26 ACTIVITY SELECTION PROBLEM (ii) Can |B | < k - 1 ? Consider B - {a}. This is a feasible solution of S. This implies that OPT(S ) k - 1 Hence, Statement (ii) is wrong. Thanks to: Navneet Kaur(22), MCA 2012 Slide 27 ACTIVITY SELECTION PROBLEM From (i) and (ii), we get |B | = k - 1 This implies that |B| = k. Hence, B is optimal. Thanks to: Navneet Kaur(22), MCA 2012 Slide 28 Activity Selection Problem Statement: an optimal solution to a problem that contains a 1 Proof: Let A = {a k,} be an optimal solution. Let a k be the first activity in A i.e. the finishing time of a k is the least. Construct another solution: B = A {a k } {a 1 } = {a 1,} Slide 29 Activity Selection Problem Proof continued Clearly, f 1 f k thus B is a set of compatible activities, hence an optimal solution too. Slide 30 Activity Selection Problem Statement: The solution is globally optimal. Proof: Suppose B = {a 1 } has an optimal solution containing k+1 elements. (a 1 being the first element) Clearly, B {a 1 } has an optimal solution with k elements. Slide 31 Activity Selection Problem Proof continued Now, suppose for B = B - {a 1 } another optimal solution containing more than k elements. Then we can construct another optimal solution B * = B {a 1 } with more than k+1 elements. This is a contradiction to our assumption of an optimal solution with k+1 elements. Slide 32 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. Slide 33 GREEDY APPROACH Pick the items in the decreasing order of value per unit weight i.e. highest first. Slide 34 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 Slide 35 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 Slide 36 Example knapsack capacity 50 item 3 100 + 60 v i = 120 v i/ w i = 4 30 10 20 Thanks to: Neha Katyal Slide 37 Example knapsack capacity 50 $80 + 100 + 60 = 240 10 20 20/ 30 Thanks to: Neha Katyal Slide 38 Up Next Dynamic Programming Slide 39 ACTIVITY SELECTION PROBLEM Options that could be followed while scheduling the jobs: Shortest Job First Eg. Three jobs to be scheduled: Job1- start=5, end=10 Job2- start=1, end=7 Job3- start=8, end=15 Our shortest job first would schedule just job1 But the optimal algorithm would have scheduled 2 jobs - job2 and job3. So this approach is not working. Thanks to: Navneet Kaur(22), MCA 2012 Slide 40 Next option that could be followed while scheduling the jobs: Smallest start time first Eg. Three jobs to be scheduled: Job1- start=1, end=20 Job2- start=2, end=7 Job3- start=8, end=15 Our smallest start time first would schedule just job1 But the optimal algorithm would have scheduled 2 jobs - job2 and job3. So this approach is also not working. Thanks to: Navneet Kaur(22), MCA 2012