Unformatted text preview: ly possible.
This greedy approach does not always lead to an optimal solution. But there
are several problems that it does work optimally for, and such problems are said
to possess the greedychoice property. This is the property that a global optimal
conﬁguration can be reached by a series of locally optimal choices (that is, choices
that are the best from among the possibilities available at the time), starting from a
welldeﬁned conﬁguration. 5.1.1 The Fractional Knapsack Problem
Consider the fractional knapsack problem, where we are given a set S of n items,
such that each item i has a positive beneﬁt bi and a positive weight wi , and we wish
to ﬁnd the maximumbeneﬁt subset that does not exceed a given weight W . If we
are restricted to entirely accepting or rejecting each item, then we would have the
01 version of this problem (for which we give a dynamic programming solution
in Section 5.3.3). Let us now allow ourselves to take arbitrary fractions of some
elements, however. The motivation for this fractional knapsack problem is that we
are going on a trip and we have a single knapsack that can carry items that together
have weight at most W . In addition, we are allowed to break items into fractions
arbitrarily. That is, we can take an amount xi of each item i such that
0 ≤ xi ≤ wi for each i ∈ S and ∑ xi ≤ W. i∈S The total beneﬁt of the items taken is determined by the objective function ∑ bi (xi /wi ). i∈S Consider, for example, a student who is going to an outdoor sporting event and
must ﬁll a knapsack full of foodstuffs to take along. Each candidate foodstuff is
something that can be easily divided into fractions, such as soda pop, potato chips,
popcorn, and pizza. Chapter 5. Fundamental Techniques 260 Algorithm FractionalKnapsack(S, W ):
Input: Set S of items, such that each item i ∈ S has a positive beneﬁt bi and a
positive weight wi ; positive maximum total weight W
Output: Amount xi of each item i ∈ S that maximizes the total beneﬁt while not
exceeding the maximum total weight W
for each item i ∈ S do
xi ← 0
vi ← bi /wi
{value index of item i}
w←0
{total weight}
while w < W do
remove from S an item i with highest value index
{greedy choice}
a ← min{wi , W − w}
{more than W − w causes a weight overﬂow}
xi ← a
w ← w+a
Algorithm 5.1: A greedy algorithm for the fractional knapsack problem. This is one place where greed is good, for we can solve the fractional knapsack
problem using the greedy approach shown in Algorithm 5.1.
The FractionalKnapsack algorithm can be implemented in O(n log n) time,
where n is the number of items in S. Speciﬁcally, we use a heapbased priority
queue (Section 2.4.3) to store the items of S, where the key of each item is its value
index. With this data structure, each greedy choice, which removes the item with
greatest value index, takes O(log n) time.
To see that the fractional knapsack problem satisﬁes the greedychoice property,
suppose that there are two items i and j such that
xi < wi , x j > 0, and vi < v j . Let
y = min{wi − xi , x j }.
We could then replace an amount y of item j with an equal amount of item i, thus
increasing the total beneﬁt without changing the total weight. Therefore, we can
correctly compute optimal amounts for the items by greedily choosing items with
the largest value index. This leads to the following theorem.
Theorem 5.1: Given a collection S of n items, such that each item i has a beneﬁt
bi and weight wi , we can construct a maximumbeneﬁt subset of S, allowing for
fractional amounts, that has a total weight W in O(n log n) time.
This theorem shows how efﬁciently we can solve the fractional version of the
knapsack problem. The allornothing, or “01” version of the knapsack problem
does not satisfy the greedy choice property, however, and solving this version of
the problem is much harder, as we explore in Sections 5.3.3 and 13.3.4. 5.1. The Greedy Method 261 5.1.2 Task Scheduling
Let us consider another optimization problem. Suppose we are given a set T of n
tasks, such that each task i has a start time, si , and a ﬁnish time, fi (where si < fi ).
Task i must start at time si and it is guaranteed to be ﬁnished by time fi . Each task
has to be performed on a machine and each machine can execute only one task at
a time. Two tasks i and j are nonconﬂicting if fi ≤ s j or f j ≤ si . Two tasks can be
scheduled to be executed on the same machine only if they are nonconﬂicting.
The task scheduling problem we consider here is to schedule all the tasks in
T on the fewest machines possible in a nonconﬂicting way. Alternatively, we can
think of the tasks as meetings that we must schedule in as few conference rooms as
possible. (See Figure 5.2.) Machine 3
Machine 2
Machine 1 1 2 3 4 5 6 7 8 9 An illustration of a solution to the task scheduling problem, for tasks whose collection of pairs of start times and ﬁnish times is
{(1, 3), (1, 4), (2, 5), (3, 7), (4, 7), (6, 9), (7, 8)}. Figure 5.2: In Algorithm 5.3, we describe a simple greedy algorithm for this problem.
Algorithm TaskSchedule(T ):
Input: A set T of tasks, such that each task has a start time si and a ﬁnish time fi
Output: A nonconﬂicting schedule of the tasks in T using a minimum number
of machines
m←0
{optimal number of machines}
while T = ∅ do
remove from T the task i with smallest start time si
if there is a mach...
View
Full Document
 Spring '14
 Dynamic Programming, Fundamental Techniques

Click to edit the document details