CSC2105: Algorithms Greedy Method
CSC2105: Algorithms Greedy Method
Mashiour Rahman
[email protected]
American International University Bangladesh
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
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
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
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
1 2 6 1
ml ml ml ml
of of of of
5 3 4 2
AIUB::CSC2105::Algorithms
Greedy 9
Task Scheduling
Given: a set T of n tasks, each having:
A start time, si A finish time, fi (where si < fi)
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)
Mashiour
AIUB::CSC2105::Algorithms
Greedy 12
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