Greedy Algorithms and Activity Selection: Optimal Solutions through Local Choices - Prof. , Study notes of Computer Science

The concept of greedy algorithms and their application to the activity selection problem. Greedy algorithms make the choice that looks best at each moment, and while they don't always lead to optimal solutions, they do for many problems, including activity selection and the fractional knapsack problem. An example of the greedy activity selector algorithm and analyzes its optimality.

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-uj1
koofers-user-uj1 🇺🇸

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 561, Lecture 15
Jared Saia
University of New Mexico
Today’s Outline
Greedy Algorithm Intro
Activity Selection
Knapsack
1
Greedy Algorithms
“Greed is Good” - Michael Douglas in Wall Street
A greedy algorithm always makes the choice that looks best
at the moment
Greedy algorithms do not always lead to optimal solutions,
but for many problems they do
In the next week, we will see several problems for which
greedy algorithms produce optimal solutions including: ac-
tivity selection, fractional knapsack.
When we study graph theory, we will also see that greedy
algorithms can work well for computing shortest paths and
finding minimum spanning trees.
2
Activity Selection
You are given a list of programs to run on a single processor
Each program has a start time and a finish time
However the processor can only run one program at any given
time, and there is no preemption (i.e. once a program is
running, it must be completed)
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Greedy Algorithms and Activity Selection: Optimal Solutions through Local Choices - Prof. and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 15

Jared Saia University of New Mexico

Today’s Outline

  • Greedy Algorithm Intro
  • Activity Selection
  • Knapsack

Greedy Algorithms

“Greed is Good” - Michael Douglas in Wall Street

  • A greedy algorithm always makes the choice that looks best at the moment
  • Greedy algorithms do not always lead to optimal solutions, but for many problems they do
  • In the next week, we will see several problems for which greedy algorithms produce optimal solutions including: ac- tivity selection, fractional knapsack.
  • When we study graph theory, we will also see that greedy algorithms can work well for computing shortest paths and finding minimum spanning trees.

Activity Selection

  • You are given a list of programs to run on a single processor
  • Each program has a start time and a finish time
  • However the processor can only run one program at any given time, and there is no preemption (i.e. once a program is running, it must be completed)

Another Motivating Problem

  • Suppose you are at a film fest, all movies look equally good, and you want to see as many complete movies as possible
  • This problem is also exactly the same as the activity selection problem.

Example

Imagine you are given the following set of start and stop times for activities

  • time

Ideas

  • There are many ways to optimally schedule these activities
  • Brute Force: examine every possible subset of the activites and find the largest subset of non-overlapping activities
  • Q: If there are n activities, how many subsets are there?
  • The book also gives a DP solution to the problem

Greedy Activity Selector

  1. Sort the activities by their finish times
  2. Schedule the first activity in this list
  3. Now go through the rest of the sorted list in order, scheduling activities whose start time is after (or the same as) the last scheduled activity

(note: code for this algorithm is in section 16.1)

Proof of Optimality

  • Let A be the set of activities selected by the greedy algorithm
  • Consider any non-overlapping set of activities B
  • We will show that |A| ≥ |B| by showing that we can replace each activity in B with an activity in A
  • This will show that A has at least as many activities as any other non-overlapping schedule and thus that A is optimal.

Proof of Optimality

  • Let ax be the first activity in A that is different than an activity in B
  • Then A = a 1 , a 2 ,... , ax, ax+1,... and B = a 1 , a 2 ,... , bx, bx+1,...
  • But since A was chosen by the greedy algorithm, ax must have a finish time which is earlier than the finish time of bx
  • Thus B′^ = a 1 , a 2 ,... , ax, bx+1,... is also a valid schedule (B′^ = B − {bx} ∪ {ax} )
  • Continuing this process, we see that we can replace each activity in B with an activity in A. QED

What?

  • We wanted to show that the schedule, A, chosen by greedy was optimal
  • To do this, we showed that the number of activities in A was at least as large as the number of activities in any other non-overlapping set of activities
  • To show this, we considered any arbitrary, non-overlapping set of activities, B. We showed that we could replace each activity in B with an activity in A

Greedy pattern

  • The problem has a solution that can be given some numerical value. The “best” (optimal) solution has the highest/lowest value.
  • The solutions can be broken down into steps. The steps have some order and at each step there is a choice that makes up the solution.
  • The choice is based on what’s best at a given moment. Need a criterion that will distinguish one choice from another.
  • Finally, need to prove that the solution that you get by making these local choices is indeed optimal

Activity Selection Pattern

  • The value of the solution is the number of non-overlapping activities. The best solution has the highest number.
  • The sorting gives the order to the activities. Each step is examining the next activity in order and decide whether to include it.
  • In each step, the greedy algorithm chooses the activity which extends the length of the schedule as little as possible

Knapsack Problem

  • Those problems for which greedy algorithms can be used are a subset of those problems for which dynamic programming can be used
  • So, it’s easy to mistakenly generate a dynamic program for a problem for which a greedy algorithm suffices
  • Or to try to use a greedy algorithm when, in fact, dynamic programming is required
  • The knapsack problem illustrates this difference
  • The 0-1 knapsack problem requires dynamic programming, whereas for the fractional knapsack problem, a greedy algo- rithm suffices

0-1 Knapsack

The problem:

  • A thief robbing a store finds n items, the i-th item is worth vi dollars and weighs wi pounds, where wi and vi are integers
  • The thief has a knapsack which can only hold W pounds for some integer W
  • The thief’s goal is to take as valuable a load as possible
  • Which values should the thief take?

(This is called the 0-1 knapsack problem because each item is either taken or not taken, the thief can not take a fractional amount)

Fractional Knapsack

  • In this variant of the problem, the thief can take fractions of items rather than the whole item
  • An item in the 0-1 knapsack is like a gold ingot whereas an item in the fractional knapsack is like gold dust

Proof

  • Assume the objects are sorted in order of cost per pound. Let vi be the value for item i and let wi be its weight.
  • Let xi be the fraction of object i selected by greedy and let V be the total value obtained by greedy
  • Consider some arbitrary solution, B, and let x′ i be the fraction of object i taken in B and let V ′^ be the total value obtained by B
  • We want to show that V ′^ ≤ V or that V − V ′^ ≥ 0

Proof

  • Let k be the smallest index with xk < 1
  • Note that for i < k, xi = 1 and for i > k, xi = 0
  • You will show that for all i, (xi − x′ i) vi wi

≥ (xi − x′ i) vk wk

Proof

V − V ′^ =

∑^ n i=

xivi −

∑^ n i=

x′ ivi (1)

∑^ n i=

(xi − x′ i) ∗ vi (2)

∑^ n i=

(xi − x′ i) ∗ wi

( vi wi

) (3)

∑^ n i=

(xi − x′ i) ∗ wi

( vk wk

) (4)

( vk wk

) ∗

∑^ n i=

(xi − x′ i) ∗ wi (5)

≥ 0 (6)

Proof

  • Note that the last step follows because (^) wvkk is positive and because: ∑^ n i=

(xi − x′ i) ∗ wi =

∑^ n i=

xiwi −

∑^ n i=

x′ iwi (7)

= W − W ′^ (8) ≥ 0. (9)

  • Where W is the total weight taken by greedy and W ′^ is the total weight for the strategy B
  • We know that W ≥ W ′

In-Class Exercise

Consider the inequality:

(xi − x′ i) vi wi

≥ (xi − x′ i) vk wk

  • Q1: Show this inequality is true for i < k
  • Q2: Show it’s true for i = k
  • Q3: Show it’s true for i > k