Download presentation
1
Greedy Algorithms Chapter 16 Highlights
Charles Tappert Seidenberg School of CSIS, Pace University
2
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
3
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
4
Activity-selection Problem A Recursive Greedy Algorithm
5
Activity-selection Problem A Recursive Greedy Algorithm
6
Activity-selection Problem An Iterative Greedy Algorithm
Both the recursive and iterative versions are O(n)
7
Elements of Greedy Strategy Greedy algorithm design steps
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
8
Elements of Greedy Strategy Two Key Ingredients
Greedy-choice property 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
9
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 integer dollars and pounds The thief wants as valuable a load as possible 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?
10
Elements of Greedy Strategy Knapsack Problems
0-1 Solutions Fractional Solution
11
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
12
Huffman Codes Variable-length prefix codes
no codeword is also a prefix of another codeword
13
Huffman Codes Constructing a Huffman Code
14
Huffman Codes Steps of Huffman Algorithm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.