Download presentation

Presentation is loading. Please wait.

Published byWarren Stott Modified over 2 years ago

1
Instructor Neelima Gupta

2
Greedy Approach A tool to design algorithms for optimization problems

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.

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.

5
General Greedy Approach Step 1: Choose the current best solution. Step 2: Obtain greedy solution on the rest.

6
When to use? There must be a greedy choice to make. The problem must have an optimal substructure.

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

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 f 1 s f 2 s f 3 s 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.

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.

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

11
Time 2 job1 job job Thanks to: Navneet Kaur(22), MCA Shortest Job First

12
Time 2 job1 job job Thanks to: Navneet Kaur(22), MCA Shortest Job First

13
Time 2 job1 job job Thanks to: Navneet Kaur(22), MCA OPTIMAL SCHEDULE SCHEDULE CHOSEN BY THIS APPROACH Shortest Job First

14
Time 0 job1 job job3 Thanks to: Navneet Kaur(22), MCA Increasing Start Times

15
Time 0 job1 job job3 Thanks to: Navneet Kaur(22), MCA Increasing Start Times

16
Time 0 job1 job job3 Thanks to: Navneet Kaur(22), MCA SCHEDULE CHOSEN BY THIS APPROACH OPTIMAL SCHEDULE Increasing Start Times

17
iS i F i P i Thanks to Neha (16) Increasing Finishing Times

18
Time 0 P(1)=10 P(3)=4 P(4)=20 P(2)= P(5)=2 Thanks to Neha (16)

19
Increasing Finishing Times Time 0 P(1)=10 P(4)=20 P(2)= P(5)=2. Thanks to Neha (16)

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

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

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

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

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

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

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

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

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,…}

29
Activity Selection Problem Proof continued… Clearly, f 1 f k thus B is a set of compatible activities, hence an optimal solution too.

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.

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.

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.

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

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 = Thanks to: Neha Katyal

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 = Thanks to: Neha Katyal

36
Example knapsack capacity 50 item v i = 120 v i/ w i = Thanks to: Neha Katyal

37
Example knapsack capacity 50 $ = / 30 Thanks to: Neha Katyal

38
Up Next Dynamic Programming

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

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google