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Greedy Choice:
Start by grabbing quarters until exceeds amount,
then dimes, then nickels, then pennies.
Consequences:
4+1+1 = 6 mistake
3+3=6 better
79 km
to school
Exit
Exit
Exit
Make Progress
Exit
Exit Condition
Exit All objects have been considered
Designing a Greedy Algorithm
pre-condition
CodeA
loop
loop-invariant>
while exit condition
CodeB
end loop
CodeC
post-condition
Loop Invariant
We have not gone wrong.
There is at least one optimal solution
consistent with the choices made so far.
Establishing the Loop Invariant
Proof must massage optSLI into optSours and prove that optSours:
• is a valid solution
• is consistent both with previous and new choices.
• is optimal
Three Players
optSLI
22
Optimization problems
23
Example: Counting money
◼ Suppose you want to count out a certain amount of
money, using the fewest possible bills and coins
◼ A greedy algorithm would do this would be:
At each step, take the largest possible bill or coin
that does not overshoot
◼ Example: To make $6.39, you can choose:
◼ a $5 bill
◼ a $1 bill, to make $6
◼ a 25¢ coin, to make $6.25
◼ A 10¢ coin, to make $6.35
◼ four 1¢ coins, to make $6.39
◼ For US money, the greedy algorithm always gives
the optimum solution
24
A failure of the greedy algorithm
◼ In some (fictional) monetary system, “krons” come
in 1 kron, 7 kron, and 10 kron coins
◼ Using a greedy algorithm to count out 15 krons,
you would get
◼ A 10 kron piece
◼ Five 1 kron pieces, for a total of 15 krons
◼ This requires six coins
◼ A better solution would be to use two 7 kron pieces
and one 1 kron piece
◼ This only requires three coins
◼ The greedy algorithm results in a solution, but not
in an optimal solution
25
A scheduling problem
◼ You have to run nine jobs, with running times of 3, 5, 6, 10, 11,
14, 15, 18, and 20 minutes
◼ You have three processors on which you can run these jobs
◼ You decide to do the longest-running jobs first, on whatever
processor is available
P1 20 10 3
P2 18 11 6
P3 15 14 5
P1 3 10 15
P2 5 11 18
P3 6 14 20
◼ That wasn’t such a good idea; time to completion is now
6 + 14 + 20 = 40 minutes
◼ Note, however, that the greedy algorithm itself is fast
◼ All we had to do at each stage was pick the minimum or maximum
27
An optimum solution
◼ Better solutions do exist:
P1 20 14
P2 18 11 5
P3 15 10 6 3
◼ This solution is clearly optimal (why?)
◼ Clearly, there are other optimal solutions (why?)
◼ How do we find such a solution?
◼ One way: Try all possible assignments of jobs to processors
◼ Unfortunately, this approach can take exponential time
28
Huffman encoding
◼ The Huffman encoding algorithm is a greedy algorithm
◼ You always pick the two smallest numbers to combine
◼ Average bits/char:
100 0.22*2 + 0.12*3 +
54 0.24*2 + 0.06*4 +
0.27*2 + 0.09*4
27 A=00
= 2.42
B=100
C=01 ◼ The Huffman
46 15
D=1010 algorithm finds an
E=11 optimal solution
22 12 24 6 27 9
F=1011
A B C D E F
29
Minimum spanning tree
◼ A minimum spanning tree is a least-cost subset of the edges of a
graph that connects all the nodes
◼ Start by picking any node and adding it to the tree
◼ Repeatedly: Pick any least-cost edge from a node in the tree to a
node not in the tree, and add the edge and new node to the tree
◼ Stop when all nodes have been added to the tree
4
6 ◼ The result is a least-cost
2 (3+3+2+2+2=12) spanning tree
4 If you think some other edge should be
1 5 ◼
◼ Minimum spanning tree: At each new node, must include new edges and
keep them sorted, which is O(n log n) overall
◼ Therefore, MST is O(n log n) + O(n) = O(n log n)
32
Other greedy algorithms
◼ Dijkstra’s algorithm for finding the shortest path in a
graph
◼ Always takes the shortest edge connecting a known node to an
unknown node
◼ Kruskal’s algorithm for finding a minimum-cost
spanning tree
◼ Always tries the lowest-cost remaining edge
◼ Prim’s algorithm for finding a minimum-cost spanning
tree
◼ Always takes the lowest-cost edge between nodes in the
spanning tree and nodes not yet in the spanning tree
33
Dijkstra’s shortest-path algorithm
◼ Dijkstra’s algorithm finds the shortest paths from a given node to
all other nodes in a graph
◼ Initially,
◼ Mark the given node as known (path length is zero)
◼ For each out-edge, set the distance in each neighboring node equal to the cost
(length) of the out-edge, and set its predecessor to the initially given node
◼ Repeatedly (until all nodes are known),
◼ Find an unknown node containing the smallest distance
◼ Mark the new node as known
◼ For each node adjacent to the new node, examine its neighbors to see whether
their estimated distance can be reduced (distance to known node plus cost of
out-edge)
◼ If so, also reset the predecessor of the new node
34
Analysis of Dijkstra’s algorithm I
◼ Assume that the average out-degree of a node is some
constant k
◼ Initially,
◼ Mark the given node as known (path length is zero)
◼ This takes O(1) (constant) time
◼ For each out-edge, set the distance in each neighboring node equal to
the cost (length) of the out-edge, and set its predecessor to the initially
given node
◼ If each node refers to a list of k adjacent node/edge pairs, this
35
Analysis of Dijkstra’s algorithm II
◼ Repeatedly (until all nodes are known), (n times)
◼ Find an unknown node containing the smallest distance
◼ Probably the best way to do this is to put the unknown nodes into a
priority queue; this takes k * O(log n) time each time a new node is
marked “known” (and this happens n times)
◼ Mark the new node as known -- O(1) time
◼ For each node adjacent to the new node, examine its neighbors to
see whether their estimated distance can be reduced (distance to
known node plus cost of out-edge)
◼ If so, also reset the predecessor of the new node
◼ There are k adjacent nodes (on average), operation requires constant
time at each, therefore O(k) (constant) time
◼ Combining all the parts, we get:
O(1) + n*(k*O(log n)+O(k)), that is, O(nk log n) time
36
Connecting wires
◼ There are n white dots and n black dots, equally spaced, in a line
◼ You want to connect each white dot with some one black dot,
with a minimum total length of “wire”
◼ Example:
39