0% found this document useful (0 votes)
19 views

CSC2105: Algorithms Greedy Method

The document discusses the greedy method algorithm design paradigm. It can be used to find optimal solutions to optimization problems by making locally optimal choices at each step. Three examples of greedy algorithms are presented: (1) the coin changing problem, (2) the fractional knapsack problem, and (3) task scheduling. The greedy choice and running time are discussed for each algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CSC2105: Algorithms Greedy Method

The document discusses the greedy method algorithm design paradigm. It can be used to find optimal solutions to optimization problems by making locally optimal choices at each step. Three examples of greedy algorithms are presented: (1) the coin changing problem, (2) the fractional knapsack problem, and (3) task scheduling. The greedy choice and running time are discussed for each algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

CSC2105: Algorithms Greedy Method

Mashiour Rahman
[email protected]
American International University Bangladesh

The Greedy Method


The greedy method is a general algorithm design paradigm, built on the following elements:
configurations: different choices, collections, or values to find an objective function: a score assigned to configurations, which we want to either maximize or minimize

It is usually used for optimization problem. That is, problem that required to find a best solution. In making a decision, a greedy algorithm always chooses the option that looks best at the moment. It works best when applied to problems with the greedychoice property: A globally-optimal solution can always be found by a series of local improvements from a starting configuration.
Mashiour AIUB::CSC2105::Algorithms Greedy 2

Greedy Steps
On each step in the algorithm, the choice must be: Feasible - i.e. it satisfies the problems constraints. Locally optimal i.e. it has to be the best local choice among all feasible choices available at the step. Irrevocable i.e. once made, it cannot be changed on subsequent steps of the algorithm.

Greed, for lack of a better word, is good! Greed is right! Greed works!

Mashiour

AIUB::CSC2105::Algorithms

Greedy 3

General form of Greedy Method form: In general, a greedy algorithm is in the


Let A Repeat be the empty set;

Choose the best c such that A +{c} is a feasible solution; Insert c into A; Until A is a solution.
We call c a candidate. If a set of candidate A could be extended to a solution, then we Mashiour say that A is feasible. AIUB::CSC2105::Algorithms Greedy 4

Making Change
Problem: A dollar amount to reach and a collection of coin amounts to use to get there. Configuration: Input: Given an integer S. A solution is a set of coins such that the total is equal S. The coins must be drawn from { 1, 5, 10, 25} cents. Output: The solution which has minimum number of coins. Objective function: Minimize number of coins returned. Greedy solution: Let A Repeat let c be the highest valued coin such that total_amount (A) <= S. into A. S. c + Insert c Until S=36 S=24
Mashiour

be the empty set.

total_amount ( A ) = {25,10,1} {10,10,1,1,1,1}

AIUB::CSC2105::Algorithms

Greedy 5

Claim: in the greedy algorithm, the set A is always promising. Proof by induction.
First note that A is always feasible. This is because we can always use the 1-cent coin to top-up. basis: hypo: step: Suppose A is promising. Let c be the highest candidate. We want to show by contradiction that A+{c} is promising. If A +{c} is not promising, since A is promising, then there exist a set B of coins, such that (a) A+B is promising; (b) all coins in B is smaller than c, and (c) total value of B is larger or equal to c. Suppose c=5, then from (b) & (c), the only possible B is {1,1,1,1,1}, {1,1,1,1,1,1},....... In each case, we can replace a number of coins by a 5-cents coin. This contradict the fact that A+B is promising.
Mashiour AIUB::CSC2105::Algorithms Greedy 6

A= is promising. A is promising.

Claim: in the greedy algorithm, the set A is always promising. (a) A+B is promising;
(b) all coins in B is smaller than c, and (c) total value of B is larger or equal to c. Suppose c=10, then from (b) & (c) the only possible B is {5,5}, {5,1,1,1,1,1}, {5,5,5,...} In each case, we can replace a number of coins by a 10-cents coin. This contradict the fact that A+B is promising. Suppose c=25, then from (b) & (c) the only possible B is {10,10,5}, {10,10,10}, {10,5,5,5},.... If B contains at most two 10-cents, then by (c), there must be sufficient coins in B to sum more than or equal to 25 cents. In fact, there are coins to make up exactly 25 cents. These coins can be replaced by a single 25-cents coin. This contradicts the fact that A+B is promising. If B contains three or more 10-cents coins, we can replace 3 of them with 2 coins: 25-cents and 5-cents. Thus contradicting the fact that A+B is promising.
Mashiour AIUB::CSC2105::Algorithms Greedy 7

The Fractional Knapsack Problem Given: A set S of n items, with each item i having
bi - a positive benefit wi - a positive weight

Goal: Choose items with maximum total value but with weight at most W.
The value of an item is its benefit/weight ratio.

If we are allowed to take fractional amounts, then this is the fractional knapsack problem. let xi denote the amount we take of item i, 0 xi wi

x (b / w ) Objective: maximize x W Constraint:


iS i i i iS i
Mashiour

AIUB::CSC2105::Algorithms

Greedy 8

Example
Given: A set S of n items, with each item i having
bi - a positive benefit wi - a positive weight

Goal: Choose items with maximum total value but with weight at most W. knapsack Solution: Items: Weight: Benefit: Value:
($ per ml)
Mashiour 1 2 3 4 5

4 ml $12 3

8 ml 2 ml 6 ml 1 ml $32 4 $40 20 $30 5 $50 50 10 ml

1 2 6 1

ml ml ml ml

of of of of

5 3 4 2

AIUB::CSC2105::Algorithms

Greedy 9

The Fractional Knapsack Algorithm Algorithm fractionalKnapsack(S, W)


Input: set S of items with benefit bi and weight wi; maximum weight W Output: amount xi of each item i to maximize benefit with weight at most W for each item i in S xi 0 vi bi / wi {value} w 0 {total weight} while w < W remove item i with highest vi xi min{wi , W - w} w w + min{wi , W - w} Greedy choice: Keep taking item with highest value (benefit / weight ratio bi / wi ) Run time: O(n log n). Why? Use a max-heap priority queue
Mashiour AIUB::CSC2105::Algorithms Greedy 10

Task Scheduling
Given: a set T of n tasks, each having:
A start time, si A finish time, fi (where si < fi)

Goal: Perform all the tasks using a minimum number of machines.


Machine 3 Machine 2 Machine 1

Mashiour

AIUB::CSC2105::Algorithms

Greedy 11

Example
Given: a set T of n tasks, each having:
A start time, si A finish time, fi (where si < fi) [1,4], [1,3], [2,5], [3,7], [4,7], [6,9], [7,8] (ordered by start)

Goal: Perform all tasks on minimum number of machines

Machine 3 Machine 2 Machine 1

Mashiour

AIUB::CSC2105::Algorithms

Greedy 12

Task Scheduling Algorithm taskSchedule(T) Algorithm


m 0 {no. of machines} while T is not empty do remove task i with smallest si if theres a machine j for i then schedule i on machine j else m m + 1 schedule i on machine m

Input: set T of tasks with start time si and finish time fi Output: non-conflicting schedule with minimum number of machines

Greedy choice: consider tasks by their start time and use as few machines as possible with this order. Run time: O(n lg n). Why? Correctness: Suppose there is a better schedule. We can use k-1 machines The algorithm uses k Let i be first task scheduled on machine k Machine i must conflict with k-1 other tasks But that means there is no non-conflicting schedule using k-1 machines
Mashiour AIUB::CSC2105::Algorithms Greedy 13

You might also like