Docsity

Greedy Algorithms-Algorithms and Complexity Theory-Lecture Slides, Slides of Advanced Algorithms

Partial preview of the text

Download Greedy Algorithms-Algorithms and Complexity Theory-Lecture Slides and more Slides Advanced Algorithms in PDF only on Docsity!

COMP

Complexity of Algorithms

Fundamental Solution Techniques

Greedy Algorithms

[See Chapter 5 in Goodrich and Tamassia.]

Learning Outcomes

At the conclusion of this set of lecture notes, you should:

  1. Understand the idea of greedy algorithms.
  2. Be familiar with some of the classical problems for which a greedy solution exists.
  3. Realize that greedy algorithms are not always appropriate, and know some examples where some seemingly natural greedy approaches fail to find the best solution.

Greedy Method

An optimization problem is a problem that involves searching for a configuration that minimizes or maximizes some objective function.

Greedy Method

An optimization problem is a problem that involves searching for a configuration that minimizes or maximizes some objective function.

The greedy method solves (or tries to solve) a given optimization problem by going through a sequence of (feasible) choices.

Greedy Method

An optimization problem is a problem that involves searching for a configuration that minimizes or maximizes some objective function.

The greedy method solves (or tries to solve) a given optimization problem by going through a sequence of (feasible) choices. The sequence starts from a well-understood starting configuration, and then iteratively makes the decision that seems best from all those that are currently possible. Problems that have a greedy solution are said to possess the greedy-choice property.

Greed Works!

There are many problems that can be solved using a greedy method, such as I (^) Fractional Knapsack Problem I (^) Interval Scheduling I (^) Task Scheduling I (^) Minimum Spanning Trees (e.g. Kruskal’s Algorithm, Prim’s Algorithm) I (^) Shortest Paths (e.g. Dijkstra’s Algorithm, Bellman-Ford Algorithm) I (^) Change Making (for some (but not all) sets of coins) I (^) Maximum Spacing k-clustering I (^)...

Fractional Knapsack Problem (FKP)

Let S be a set of n items, where each item i has a positive benefit bi and a positive weight wi.

Fractional Knapsack Problem (FKP)

Let S be a set of n items, where each item i has a positive benefit bi and a positive weight wi. Goal : Find the maximum benefit subset that does not exceed total weight W.

Fractional Knapsack Problem (FKP)

Let S be a set of n items, where each item i has a positive benefit bi and a positive weight wi. Goal : Find the maximum benefit subset that does not exceed total weight W. In a FKP we are allowed to take arbitrary fractions, xi , of each item, i.e. the solution is a set of values xi such that

0 ≤ xi ≤ wi for all i, and ∑

i∈S

xi ≤ W.

The total benefit of the items taken is determined by the sum ∑

i∈S

bi (xi /wi ).

Fractional Knapsack Problem (cont.)

The general method for the FKP is to compute the value index for each item i, where this is defined as

vi =

bi wi

Fractional Knapsack Problem (cont.)

The general method for the FKP is to compute the value index for each item i, where this is defined as

vi =

bi wi

Then we select items to include in the knapsack, starting with the highest value index.

Using a heap (storing the maximum at the root), we can compute the value indices and build the heap in O(n log n) time. Then, each greedy choice (which removes an item with the greatest remaining value index) requires O(log n) time.

FRACTIONALKNAPSACK(S, W )

 Input: Set S of items, item i has weight wi and benefit bi , maximum total weight W.  Output: Amount xi of each item to maximize the total benefit. 1 for i ← 1 to |S| do 2 xi ← 0 3 vi ← bi /wi 4 Insert (vi , i) into a heap H (max value index at root). 5 w ← 0 6 while w < W do 7 Remove the max value from H. 8 a ← min{wi , W − w} 9 xi ← a 10 w ← w + a

The { 0 , 1 }-Knapsack Problem

The { 0 , 1 }-Knapsack Problem is very similar to the Fractional Knapsack Problem.

The significant difference is that in the { 0 , 1 } version, we are not allowed to take fractional amounts of the items, either we take the entire item, or leave it behind.

The { 0 , 1 }-Knapsack Problem

The { 0 , 1 }-Knapsack Problem is very similar to the Fractional Knapsack Problem.

The significant difference is that in the { 0 , 1 } version, we are not allowed to take fractional amounts of the items, either we take the entire item, or leave it behind.

A Greedy Method does not (in general) find an optimal solution of the { 0 , 1 }-Knapsack Problem.