0% found this document useful (0 votes)
54 views21 pages

CSL 356: Analysis and Design of Algorithms: Ragesh Jaiswal CSE, IIT Delhi

This document discusses greedy algorithms and provides examples. It describes an interval scheduling problem where intervals are given as (start, finish) pairs and the goal is to find the largest subset of non-overlapping intervals. A greedy algorithm is presented that chooses the interval with the smallest finish time at each step. It is proven that this greedy algorithm finds the optimal solution. The document also describes a fractional knapsack problem where items have volume and value, and the goal is to maximize total value in a knapsack of size W. A greedy algorithm is given that chooses items in order of highest value per unit volume.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views21 pages

CSL 356: Analysis and Design of Algorithms: Ragesh Jaiswal CSE, IIT Delhi

This document discusses greedy algorithms and provides examples. It describes an interval scheduling problem where intervals are given as (start, finish) pairs and the goal is to find the largest subset of non-overlapping intervals. A greedy algorithm is presented that chooses the interval with the smallest finish time at each step. It is proven that this greedy algorithm finds the optimal solution. The document also describes a fractional knapsack problem where items have volume and value, and the goal is to maximize total value in a knapsack of size W. A greedy algorithm is given that chooses items in order of highest value per unit volume.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSL 356: Analysis and Design of

Algorithms
Ragesh Jaiswal
CSE, IIT Delhi

Greedy Algorithms

Greedy Algorithms: Introduction


A local (greedy) decision rule leads to a globally optimal solution.

Two ways to show the above property:

Greedy stays ahead.


2. Exchange argument
1.

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping intervals.

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping


intervals.
Candidate greedy choices:
Earliest start time

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping


intervals.
Candidate greedy choices:
Earliest start time
Smallest duration

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping


intervals.
Candidate greedy choices:
Earliest start time

Smallest duration
Least overlapping

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping


intervals.
Candidate greedy choices:

Earliest start time


Smallest duration
Least overlapping
Earliest finish time

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping intervals.


Greedy Algorithm:
GreedySchedule
While is not empty
- Choose an interval ((), ()) from that has the smallest value of ()
- Delete all intervals in that overlap with ((), ())

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping intervals.


Greedy Algorithm:
GreedySchedule
While is not empty
- Choose an interval ((), ()) from that has the smallest value of ()
- Delete all intervals in that overlap with ((), ())

Question: Let denote some optimal subset and be the

subset given by GreedySchedule. Can we show that = ?

Greedy Algorithms: Example


Can we show that || = ||?
Yes we can! We will use the greedy stays ahead method to show

this.
Proof: Let 1, 2, , be the sequence of requests that
GreedySchedule picks and 1, 2, , be the requests in
sorted by finishing time.
Claim: (1) (1)

Greedy Algorithms: Example


Can we show that || = ||?
Yes we can! We will use the greedy stays ahead method to show

this.
Proof: Let 1, 2, , be the sequence of requests that
GreedySchedule picks and 1, 2, , be the requests in
sorted by finishing time.
Claim: (1) (1)
Claim: If (1) (1), (2) (2), , (1 )

(1 ), then ( ) ().

Greedy Algorithms: Example


Can we show that || = ||?
Yes we can! We will use the greedy stays ahead method to show

this.
Proof: Let 1, 2, , be the sequence of requests that
GreedySchedule picks and 1, 2, , be the requests in
sorted by finishing time.
Claim: (1) (1)
Claim: If (1) (1), (2) (2), , (1 )

(1 ), then ( ) ().
GreedySchedule could not have stopped after

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping intervals.


Greedy Algorithm:
GreedySchedule
While is not empty
- Choose an interval ((), ()) from that has the smallest value of ()
- Delete all intervals in that overlap with ((), ())

Running time:

Greedy Algorithms: Example


Interval scheduling: Given a set of intervals of the form

((), ()), find the largest subset of non-overlapping intervals.


Greedy Algorithm:
GreedySchedule
While is not empty
- Choose an interval ((), ()) from that has the smallest value of ()
- Delete all intervals in that overlap with ((), ())

Running time: ( log )

Greedy Algorithms: Example


Fractional Knapsack:You are a thief and you have a sack of

size . There are divisible items. Each item has a


volume () and total value (). How will you maximize
your profit?
Greedy strategies:
Pick items with largest value first.
W = 50

20

25

50

30
10

50

60

30

Greedy Algorithms: Example


Fractional Knapsack:You are a thief and you have a sack of size .

There are divisible items. Each item has a volume () and


total value (). How will you maximize your profit?
Greedy strategies:
Pick items with largest value first.

Pick items with smallest volume first.


W = 50

20

25

50

30
10

50

60

30

Greedy Algorithms: Example


Fractional Knapsack:You are a thief and you have a sack of size .

There are divisible items. Each item has a volume () and


total value (). How will you maximize your profit?
Greedy strategies:
Pick items with largest value first.

Pick items with smallest volume first.


Pick items with largest cost per unit volume.
W = 50

20

25

50

30
10

50

60

30

Greedy Algorithms: Example


Fractional Knapsack:You are a thief and you have a sack of

size . There are divisible items. Each item has a


volume () and total value (). How will you maximize
your profit?

GreedySteal
While Sack is not full
- Choose an item from that has the largest cost per unit volume
- Put as much as you can of this item in the sack and delete from
W = 50

20

25

50

30
10

50

60

30

Greedy Algorithms: Example


Consider items in decreasing order of the cost per unit

volume value.
Let (1, , ) be the volume of items in the sack chosen
by GreedySteal.
Let (1, , ) be some optimal volume of items that
maximizes the profit.
Claim: For all ,
1 1 + + 1 1 + +

End
Problems to think about:
1. Consider the fractional-knapsack problem. Think of an exchange
argument to prove that the greedy algorithm gives the optimal
solution.

You might also like