Download presentation

1
**MCA 301: Design and Analysis of Algorithms**

Instructor Neelima Gupta

2
**Greedy Approach A tool to design algorithms for optimization problems**

Table Of Contents 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 = {a1, a2, …, an} that need to use some resource. Each activity ai has a possible start time si & finish time fi, such that 0 si < fi < 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 ai and aj are said to be compatible, if the interval they span do not overlap. ..i.e. fi sj or fj si Example: Consider activities: a1, a2, a3, a4 s f1 s f2 s f3 s f4 Here a1 is compatible with a3 & a4 a2 is compatible with a3 & a4 But a3 and a4 themselves are not compatible.

9
**Activity Selection Problem**

Solution: Applying the general greedy algorithm Select the current best choice, a1 add it to the solution set. Construct a subset S’ of all activities compatible with a1, 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
**Shortest Job First job1 job2 job3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012

12
**Shortest Job First job1 job2 job3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Time Thanks to: Navneet Kaur(22), MCA 2012

13
**Shortest Job First job1 job2 job3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15**

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

14
**Increasing Start Times**

job1 job2 job3 2 4 6 8 10 12 14 16 18 20 Time Thanks to: Navneet Kaur(22), MCA 2012

15
**Increasing Start Times**

job1 job2 job3 2 4 6 8 10 12 14 16 18 20 Time Thanks to: Navneet Kaur(22), MCA 2012

16
**Increasing Start Times**

job1 job2 job3 2 4 6 8 10 12 14 16 18 20 SCHEDULE CHOSEN BY THIS APPROACH Time OPTIMAL SCHEDULE Thanks to: Navneet Kaur(22), MCA 2012

17
**Increasing Finishing Times**

i Si Fi Pi Thanks to Neha (16)

18
**Increasing Finishing Times**

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)

19
**Increasing Finishing Times**

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)

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′ {a1} is an optimal solution of S. PROOF: Suppose an imaginary solution B″, which is optimal and includes a1 . 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 a1 Proof: Let A = {ak,…} be an optimal solution. Let ak be the first activity in A i.e. the finishing time of ak is the least. Construct another solution: B = A – {ak} {a1} = {a1,…}

29
**Activity Selection Problem**

Proof continued… Clearly, f1 fk 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 = {a1…} has an optimal solution containing k+1 elements. (a1 being the first element) Clearly, B – {a1} has an optimal solution with k elements.

31
**Activity Selection Problem**

Proof continued… Now, suppose for B’ = B - {a1} another optimal solution containing more than k elements. Then we can construct another optimal solution B* = B’ {a1} 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 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.

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

34
**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

35
**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

36
**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

37
**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

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

OK

Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.

Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google