0% found this document useful (0 votes)
4 views14 pages

Greedy ActivitySelection

The document outlines the greedy algorithm for activity selection, which involves making locally optimal choices to construct a solution set of mutually compatible activities. It explains the process of sorting activities by finish times and determining the optimal number of activities through dynamic programming. Additionally, it discusses the correctness of the greedy approach using an exchange argument to demonstrate that a greedy choice can lead to an optimal solution.

Uploaded by

ma23btech11005
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)
4 views14 pages

Greedy ActivitySelection

The document outlines the greedy algorithm for activity selection, which involves making locally optimal choices to construct a solution set of mutually compatible activities. It explains the process of sorting activities by finish times and determining the optimal number of activities through dynamic programming. Additionally, it discusses the correctness of the greedy approach using an exchange argument to demonstrate that a greedy choice can lead to an optimal solution.

Uploaded by

ma23btech11005
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/ 14

Greedy Algorithm

1. Successively make “greedy choices”: Start


with an empty solution set and add an element x
that is “locally optimal”.

2. Argue that if S is the current solution and x is


the greedy choice, then there is some optimal
solution containing 𝑆 ∪ 𝑥 .
Activity Selection

i 1 2 3 4 5 6 7 8 9 10 11

s(i) 1 3 0 5 3 5 6 8 8 2 12

f(i) 4 5 6 7 8 9 10 11 12 13 14
Activity Selection

i 1 2 3 4 5 6 7 8 9 10 11

s(i) 1 3 0 5 3 5 6 8 8 2 12

f(i) 4 5 6 7 8 9 10 11 12 13 14
Activity Selection

i 1 2 3 4 5 6 7 8 9 10 11

s(i) 1 3 0 5 3 5 6 8 8 2 12

f(i) 4 5 6 7 8 9 10 11 12 13 14
Activity Selection

Input: A set 𝑆 = {𝑎1 , 𝑎2 , … , 𝑎𝑛 } of activities


(𝑠𝑖 , 𝑓𝑖 ): start and finish time of 𝑎𝑖

Output: A largest set of mutually compatible


(i.e. non-overlapping) activities
Activity Selection: Dynamic programming

1. Sort the activities wrt finish times.


2. Let OPT(i) = Max # of mutually compatible
activities from {𝑎1 , 𝑎2 , … , 𝑎𝑖 }.
3. OPT(i) = Max {OPT(i-1), 1+OPT(j)}, where
j=largest index for which 𝑓𝑖 ≤ 𝑠𝑖 .
4. For i=2 to n compute OPT(i).
Activity Selection: Dynamic Programming

i 1 2 3 4 5 6 7 8 9 10 11

s(i) 1 3 0 5 3 5 6 8 8 2 12

f(i) 4 5 6 7 8 9 10 11 12 13 14

OPT(8) = Max{OPT(7), 1+OPT(5)}


Activity Selection: Greedy Solution

i 1 2 3 4 5 6 7 8 9 10 11

s(i) 1 3 0 5 3 5 6 8 8 2 12

f(i) 4 5 6 7 8 9 10 11 12 13 14
Activity Selection: Greedy Algorithm

1. Sort the activities wrt finish times.


2. S= {𝑎1 }.
3. k=1.
4. For i=2 to n
If 𝑠𝑖 > 𝑓𝑘 , add 𝑎𝑖 to S.
k=i
5. Return S.
Activity Selection: Greedy Algorithm

1. Running time for sorting : O(n log n)


2. Running time for greedy phase: O(n)
Greedy Algorithm: Correctness

The most common idea to show that the greedy


choice is optimal is via an exchange argument.
Greedy Algorithm: Exchange Argument

To prove: Our greedy choice x can be extended


to some optimal solution.

Proof Sketch: Consider an optimal solution S.


If x is in S, we are done. Otherwise, there exists
y in S such that we can replace y with S and
𝑆 ∪ 𝑥 ∖ 𝑦 is an optimal solution.

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