Pattern Recognition - seidenberg school of csis

Greedy Algorithms
Chapter 16 Highlights
Charles Tappert
Seidenberg School of CSIS, Pace University
Greedy Algorithms



Make the choice that looks best at the moment
We arrive at the greedy algorithm by first
considering a dynamic-programming approach
An important application of the greedy technique
is data-compression Huffman codes
Activity-selection Problem

Problem: select a maximum-size subset of mutually
compatible activities


Compatible activities have non-overlapping time intervals
Assume the activities are sorted in monotonically
increasing order of finish times
Activity-selection Problem
A Recursive Greedy Algorithm
Activity-selection Problem
A Recursive Greedy Algorithm
Activity-selection Problem
An Iterative Greedy Algorithm

Both the recursive and iterative versions are O(n)
Elements of Greedy Strategy
Greedy algorithm design steps
1.
2.
3.
Cast the problem so that each choice leaves
one subproblem
Prove that the greedy choice is always safe
(that is, on the optimal path to a solution)
Demonstrate optimal substructure – show that
an optimal solution to the remaining
subproblem after a greedy choice arrives at an
optimal solution to the original problem
Elements of Greedy Strategy
Two Key Ingredients
1.
Greedy-choice property


2.
Making locally optimal (greedy) choices leads to an
optimal solution
Greedy choices are made top down, without solving
subproblems
Optimal substructure


The optimal solution to a problem contains within it
optimal solutions to subproblems
Directly argue that the optimal solution to the
subproblem, combined with the greedy choice already
made, yields an optimal solution to the original problem
Elements of Greedy Strategy
Knapsack Problems

0-1 Knapsack problem: thief robs store of n items

i-th item worth vi dollars, weighs wi pounds


The thief wants as valuable a load as possible


integer dollars and pounds
But can carry at most W pounds in his knapsack
Which items should he take?
--------------------------------------------------------------------------------
Fractional knapsack problem


Same setup but the thief can take fractions of items
Which knapsack problem has optimal greedy solution?
Elements of Greedy Strategy
Knapsack Problems
Problem
0-1 Solutions
Fractional Solution
Huffman Codes

Huffman codes compress data efficiently



Data = a sequence of characters, e.g. text files
Problem: design binary char codes (codewords)
Algorithm uses a table of char frequency counts
Huffman Codes

Variable-length prefix codes

no codeword is also a prefix of another codeword
Huffman Codes
Constructing a Huffman Code
Huffman Codes
Steps of Huffman Algorithm