Anda di halaman 1dari 14

Greedy Algorithms

Contents

Introduction
Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These locally optimal solutions will finally add up to a globally optimal solution. Only a few optimization problems can be solved by the greedy method.

Greedy-choice property: A globally-optimal solution can always be found by a series of local improvements from a starting configuration.
A Greedy algorithm works 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 local optimum at each step, you will end up at a global optimum

Some Terms
Optimization Problem Constraints Objective Function Feasible solution Optimal Solution

A simple example
Problem: Pick k numbers out of n numbers such that the sum of these k numbers is the largest.

Algorithm: FOR i = 1 to k pick out the largest number and delete this number from the input. ENDFOR

Knapsack Problem
Given: A set S of n items, with each item i having bi - a positive benefit(profit pi ) wi - a positive weight Size of Knapsack(capacity)=M Goal: Choose items with maximum total benefit but with weight at most W. 0-1 Knapsack problem: In this problem each item must either be taken or left behind, in this we can not take fractional amount of an item. Fractional Knapsack Problem: In this problem we can take fractional amount of items.

Fractional Knapsack Problem


In this case, we let xi denote the amount we take of item i.
Objective: Maximize

b x
iS i

Constraint:

w x
iS

i i

And 0

xi

Algorithm fractionalKnapsack(S, W) Input: Set S of items w/ benefit bi and weight wi; Max. 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 {current total weight} while w < W remove item i with highest vi xi min {wi / wi , W - w /wi } w w + min {wi , W - w}

Numerical
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.6 p3/w3 = 15/10 = 1.5

total profit = 24 + 7.5 = 31.5

Optimal solution: x1 = 0, x2 = 1, x3 = 1/2

Example
Given: A set S of n items, with each item i having

Goal: Choose items with maximum total benefit but with weight at most W.
knapsack Items:
1 2 3 4 5

bi - a positive benefit wi - a positive weight

Solution:

Weight: Benefit: Value: ($ per ml)

4 ml $12 3

8 ml $32 4

2 ml $40 20

6 ml $30 5

1 ml $50 50 10 ml

1 ml of 5 2 ml of 3 6 ml of 4 1 ml of 2

Job Sequencing With Deadlines


Problem: n jobs, S={1, 2, , n}, each job i has a deadline di 0 and a profit pi 0. We need one unit of time to process each job and we can do at most one job each time. We can earn the profit pi if job i is completed by its deadline.

i
pi di

1
20 2

2
15 2

3
10 1

4
5 3

5
1 3

The optimal solution = {1, 2, 4}.

The total profit = 20 + 15 + 5 = 40.

Algorithm: Step 1: Sort pi into nonincreasing order. After sorting 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 time slot [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, go to step 2. Time complexity: O(n2)

e.g.
i 1 2 3 4 5 pi 20 15 10 5 1 di 2 2 1 3 3

assign to [1, 2] assign to [0, 1] reject assign to [2, 3] reject

solution = {1, 2, 4} total profit = 20 + 15 + 5 = 40

Anda mungkin juga menyukai