Greedy Methods
-
Upload
deepali-yadav -
Category
Documents
-
view
241 -
download
0
Transcript of Greedy Methods
-
7/31/2019 Greedy Methods
1/14
Greedy Algorithms
-
7/31/2019 Greedy Methods
2/14
Contents
-
7/31/2019 Greedy Methods
3/14
Introduction
Suppose that a problem can be solved by asequence of decisions. The greedy method hasthat each decision is locally optimal. Theselocally optimal solutions will finally add up to a
globally optimal solution.
Only a few optimization problems can besolved by the greedy method.
-
7/31/2019 Greedy Methods
4/14
Greedy-choice property:A globally-optimal solution can always be found
by a series of local improvements from astarting configuration.
AGreedy algorithmworks in phases. At each
phase:You take the best you can get right now,without regard for future consequences
You hope that by choosing a localoptimumat each step, you will end up at a globaloptimum
-
7/31/2019 Greedy Methods
5/14
Optimization Problem
Constraints
Objective Function
Feasible solution
Optimal Solution
Some Terms
-
7/31/2019 Greedy Methods
6/14
Problem: Pick k numbers out of n numberssuch that the sum of these k numbers is thelargest.
Algorithm:
FOR i = 1 to k
pick out the largest number anddelete this number from the input.
ENDFOR
A simple example
-
7/31/2019 Greedy Methods
7/14
Knapsack ProblemGiven:A set S ofnitems, with each item i having
bi - a positive benefit(profit pi )wi - a positive weightSize of Knapsack(capacity)=M
Goal: Choose items with maximum total benefit but withweight at most W.
0-1 Knapsack problem: In this problem each itemmust either be taken or left behind, in this we can not takefractional amount of an item.
Fractional Knapsack Problem: In this problem wecan take fractional amount of items.
-
7/31/2019 Greedy Methods
8/14
Fractional Knapsack Problem
In this case, we let xi denote the amount wetake of item i.
Objective: Maximize
Constraint:
And 0 1
Siiixb
Siii
Mxw
ix
-
7/31/2019 Greedy Methods
9/14
AlgorithmfractionalKnapsack(S,W)
Input: Set S of items w/ benefit bi and weight wi; Max.
weight W.Output: Amountxiof each item ito maximize benefit withweight at most W.
foreach item i in Sxi 0
vibi / wi {value}
w 0 {current total weight}
whilew < Wremove item iwith highest viximin {wi / wi , W - w /wi}
w w + min {wi , W - w}
-
7/31/2019 Greedy Methods
10/14
The greedy algorithm:
Step 1: Sort pi/wi into nonincreasing order.Step 2: Put the objects into the knapsack according
to the sorted sequence as possible as we can.e. g.
n = 3, M = 20,(p1, p2, p3) = (25, 24, 15)(w1, w2, w3) = (18, 15, 10)
Sol: p1/w1 = 25/18 = 1.32
p2/w2 = 24/15 = 1.6p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 = 1, x3 = 1/2
total profit = 24 + 7.5 = 31.5
Numerical
-
7/31/2019 Greedy Methods
11/14
ExampleGiven: A set S of n items, with each item i having
bi - a positive benefitwi - a positive weight
Goal: Choose items with maximum total benefit butwith weight at most W.
Weight:
Benefit:
1 2 3 4 5
4 ml 8 ml 2 ml 6 ml 1 ml
$12 $32 $40 $30 $50
Items:
Value: 3
($ per ml)
4 20 5 50
10 ml
Solution:
1 ml of 5
2 ml of 3 6 ml of 4 1 ml of 2
knapsack
-
7/31/2019 Greedy Methods
12/14
Job Sequencing With DeadlinesProblem: n jobs, S={1, 2, , n}, each job i has a
deadline di 0 and a profit pi 0. We need one unit oftime to process each job and we can do at most one jobeach time. We can earn the profit pi if job i is completedby its deadline.
i 1 2 3 4 5
pi 20 15 10 5 1
di 2 2 1 3 3
The optimal solution = {1, 2, 4}.
The total profit = 20 + 15 + 5 = 40.
-
7/31/2019 Greedy Methods
13/14
Algorithm:
Step 1: Sort pi into nonincreasing order. Aftersorting p1 p2 p3 pi.
Step 2:Add the next job i to the solution set if i
can be completed by its deadline. Assign i to timeslot [r-1, r], where r is the largest integer such that 1 r di and [r-1, r] is free.
Step 3: Stop if all jobs are examined. Otherwise, goto step 2.
Time complexity: O(n2)
-
7/31/2019 Greedy Methods
14/14
e.g.
i pi di1 20 2 assign to [1, 2]
2 15 2 assign to [0, 1]
3 10 1 reject
4 5 3 assign to [2, 3]
5 1 3 reject
solution = {1, 2, 4}total profit = 20 + 15 + 5 = 40