0% found this document useful (0 votes)
12 views7 pages

Sept 29

Greedy algorithms make locally optimal choices at each step in the hope of finding a global optimum. The document discusses greedy algorithms for scheduling and set cover problems. It provides examples of greedy algorithms that pick the interval or set with the earliest end time or largest uncovered elements. While greedy choices may find an optimal solution, greedy algorithms are not always optimal and can have worse case performance.

Uploaded by

Jee Aspirant
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)
12 views7 pages

Sept 29

Greedy algorithms make locally optimal choices at each step in the hope of finding a global optimum. The document discusses greedy algorithms for scheduling and set cover problems. It provides examples of greedy algorithms that pick the interval or set with the earliest end time or largest uncovered elements. While greedy choices may find an optimal solution, greedy algorithms are not always optimal and can have worse case performance.

Uploaded by

Jee Aspirant
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/ 7

Greedy Algorithms

1
Example: A Scheduling Problem

Input: A set of intervals 𝐴𝐴 = { 𝑥𝑥1 , 𝑦𝑦1 , 𝑥𝑥2 , 𝑦𝑦2 , … , … }


𝑥𝑥𝑖𝑖 ≤ 𝑦𝑦𝑖𝑖 , 𝑥𝑥𝑖𝑖 , 𝑦𝑦𝑖𝑖 ∈ ℝ
Output: Schedule 𝑆𝑆 ⊆ 𝐴𝐴 Non-overlapping
(null intersection)
𝐼𝐼3
Goal: Maximize |𝑆𝑆| 𝐼𝐼1
Both optimal
𝐼𝐼2
Algo Skeleton:
1. Set 𝑆𝑆 = 𝜙𝜙
2. While ∃ 𝑥𝑥, 𝑦𝑦 ∈ 𝐴𝐴\S1 that does not
overlap with any 𝑥𝑥, 𝑦𝑦 ∈ S
• Choose an optimal 𝑥𝑥 ∗ , 𝑦𝑦 ∗ ∈ 𝐴𝐴\S1
and add to S
3. Return 𝑆𝑆 Strategy?!

Greedy because it does not revisit its


decisions [here, choosing optimal interval]
2
Strategy:
Pick the non-intersecting (with S) interval with the earliest end time!

How to prove the optimality?

- Let G = (g1 , g 2 , … ) be the greedy solution.


- Let S = (𝑠𝑠1 , 𝑠𝑠2 , … ) be any optimal solution.

Sorted by end time Complexity? 𝑛𝑛 = |𝐴𝐴|


Lets compare G and S: 𝑂𝑂 𝑛𝑛 log 𝑛𝑛 + 𝑂𝑂(𝑛𝑛)
Say, g1 = 𝑠𝑠1 but g 2 ≠ 𝑠𝑠2
Consider: S ′ = (𝑔𝑔1 , 𝑔𝑔2 , s3 , s4 , … )

Is this a valid solution?


𝑔𝑔2 does not intersect with the intervals before it
Does 𝑔𝑔2 go with intervals after it?
3
𝑔𝑔2 was chosen when 𝑠𝑠2 , 𝑠𝑠3 and all other options were available.

Now if 𝑠𝑠2 co-exist with all others that follow it, 𝑔𝑔2 with
earlier or same end time can also!
So S ′ = (𝑔𝑔1 , 𝑔𝑔2 , s3 , s4 , … ) is valid (optimal).

Like this we can obtain more optimal solutions and end with
the greedy solution without ever loosing optimality.
Formal proof by contradiction:
Suppose 𝐺𝐺 is NOT optimal.
Pick an optimal 𝑆𝑆 which is closest (max |𝑆𝑆 ∩ 𝐺𝐺|) to 𝐺𝐺.
𝑆𝑆 ≠ 𝐺𝐺
We can modify 𝑆𝑆 to 𝑆𝑆𝑆 so that:
𝑆𝑆𝑆 is also optimal & 𝑆𝑆 ′ ∩ 𝐺𝐺 ≥ 𝑆𝑆 ∩ 𝐺𝐺
4 Contradiction!
Set Cover
𝐼𝐼 = 𝑆𝑆𝑖𝑖 , 𝑖𝑖 = 1: 𝑚𝑚 & ∪𝑘𝑘=1:𝑚𝑚 𝑆𝑆𝑘𝑘 = 𝐵𝐵, 𝑆𝑆𝑘𝑘 ≠ {𝜙𝜙}

Minimize 𝐼𝐼𝑠𝑠 𝑜𝑜𝑜𝑜 |𝐽𝐽| ≤ 𝑚𝑚 , 𝐼𝐼𝑠𝑠 = 𝑆𝑆𝑖𝑖 ,∪𝑖𝑖 𝑆𝑆𝑖𝑖 = 𝐵𝐵 , 𝐽𝐽 = {𝑖𝑖,∪𝑖𝑖 𝑆𝑆𝑖𝑖 = 𝐵𝐵}
Example: 𝐵𝐵 is a universal set of topics covered by 𝑚𝑚 available ALGO
courses. 𝑆𝑆𝑗𝑗 is the set of topics covered by the 𝑗𝑗𝑡𝑡𝑡 ALGO course. Taking
minimum number of which ALGO courses will allow us to learn all
the topics being covered by all the 𝒎𝒎 of them?
Courses
More
than 1
optimal! Topics
5
A graph? (number of points (n) = number of sets (m))

Example: 𝐵𝐵 is a universal set of 𝑛𝑛 towns. 𝑆𝑆𝑗𝑗 is the set of towns (that will be)
covered by the school of 𝑗𝑗𝑡𝑡𝑡 town (𝑚𝑚 = 𝑛𝑛, symmetric, not transitive). Which
minimum number of schools can cover all the 𝑛𝑛 towns. [Vertex Cover]
A school in a town will cover another adjacent (edge) town

6
Algo Skeleton:
1. Set 𝐽𝐽 = 𝜙𝜙
2. While ∪𝑖𝑖∈𝐽𝐽 𝑆𝑆𝑖𝑖 ≠ 𝐵𝐵: Pick 𝑖𝑖 ∉ 𝐽𝐽 (𝑆𝑆𝑖𝑖 ∈ 𝐼𝐼\J) and add to 𝐽𝐽
3. Return 𝐽𝐽
Strategy?
Chose the largest uncovered set? max |𝑆𝑆𝑖𝑖 \∪𝑘𝑘∈𝐽𝐽 𝑆𝑆𝑘𝑘 |
𝑖𝑖∉𝐽𝐽

Valid as worst case will be all 𝑆𝑆𝑖𝑖 chosen.


Optimal? NO. Cubic time
First complexity!
𝑂𝑂(𝑚𝑚2 𝑛𝑛)
Second
Third

Greedy algos may not be optimal!

7 Notion of approximation algos.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy