Вы находитесь на странице: 1из 9

The Greedy Method

The Greedy Method Technique


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

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.

Greedy Algorithms
A greedy algorithm always makes the choice that looks best at the moment
My everyday examples:
Taking Bus/Auto/Taxi to station

The hope: a locally optimal choice will lead to a globally optimal solution

Example: Making Change


Problem: Accept n dollars, to return a collection of coins with a total value of n dollars. Configuration: A collection of coins with a total value of n Objective function: Minimize number of coins returned. Greedy solution: Always return the largest coin you can

The Knapsack Problem


The famous knapsack problem:
A thief breaks into a museum. Fabulous paintings, Antiques, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collectors market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?

The Knapsack Problem: Greedy Vs. Dynamic


The fractional problem can be solved greedily

The 0-1 problem cannot be solved with a greedy approach

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 benefit but with weight at most W. If we are allowed to take fractional amounts, then this is the fractional knapsack problem.
In this case, we let xi denote the amount we take of item i Objective: maximize

b (x / w )
iS i i i i

Constraint:

x
iS

The Fractional Knapsack Algorithm


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} w0 {current total weight} while w < W remove item i with highest vi xi min{wi , W - w} w w + min{wi , W - w}

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 benefit but with weight at most W. knapsack Solution: Items: Weight: Benefit: Value:
($ per ml)
1 2 3 4 5

4 ml $12 3

8 ml $32 4

2 ml $40 20

6 ml $30 5

1 ml $50 50 10 ml

1 2 6 1

ml ml ml ml

of of of of

5 3 4 2

Вам также может понравиться