*This preview shows
pages
1–4. Sign up
to
view the full content.*

Greedy Template
1.
First Application: Selection Sort
2.
Second Application: Optimal Merge Patterns
3.
Third Application: The Knapsack Problem
4.
Fourth Application: Minimum Spanning Trees
5.
Fifth Application: Single-Source Shortest Paths Problem
6.
I. Greedy Template:
Greedy(
input
I)
begin
while
(solution is not complete)
do
Select the best element x in the
remaining input I;
Put x next in the output;
Remove x from the remaining input;
endwhile
end
The notion of "best" has to be defined in each problem separately.
Therefore, the essense of each greedy algorithm is the selection policy
Back to Top
II. First Application: Selection Sort
The Greedy Method
http://www.seas.gwu.edu/~ayoussef/cs212/greedy.html
1 of 8
9/3/2010 4:51 AM

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up* To sort using the greedy method, have the selection policy select the minimum of the remaining
input. That is, best=minimum.
The resulting algorithm is a well-known sorting algorithm, called Selection Sort. It takes O(n^2)
time, so it is not the best sorting algorithm.
Back to Top
III. Second Application: Optimal Merge Patterns
Input: ' sorted arrays of length L[1], L[2],.
..,L[n]
Problem: Ultimateley, to merge the arrays pairwise as fast as possible. The problem is to
determine which pair to merge everytime.
Method (the Greedy method): The selection policy (of which best pair of arrays to merge next) is
to choose the two shortest remaining arrays.
Implementation:
'eed a data structure to store the lengths of the arrays, to find the shortest 2 arrays at any
time, to delete those lengths, and insert in a new length (for the newly merged array).
In essence, the data structure has to support delete-min and insert. Clearly, a min-heap is
ideal.
Time complexity of the algorithm: The algorithm iterates (n-1) times. At every iteration two
delete-mins and one insert is performed. The 3 operations take O(log n) in each iteration.
Thus the total time is O(nlog n) for the
while
loop + O(n) for initial heap construction.
That is, the total time is O(nlog n).
Back to Top
IV. Third Application: The Knapsack Problem
Input: A weight capacity C, and n items of weights W[1:n] and monetary value P[1:n].
Problem: Determine which items to take and how much of each item so that
the total weight is <= C, and
the total value (profit) is maximized.
Formulation of the problem: Let x[i] be the fraction taken from item i. 0 <= x[i] <= 1.
The weight of the part taken from item i is x[i]*W[i]
The Corresponding profit is x[i]*P[i]
The problem is then to find the values of the array x[1:n] so that x[1]P[1]+x[2]P[2]+.
..+x[n]P[n] is
The Greedy Method
http://www.seas.gwu.edu/~ayoussef/cs212/greedy.html
2 of 8
9/3/2010 4:51 AM

maximized subject to the constraint that x[1]W[1]+x[2]W[2]+.
..+x[n]W[n] <= C

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
This is the end of the preview. Sign up
to
access the rest of the document.