Professional Documents
Culture Documents
Greedy Algorithms: CSE373: Design and Analysis of Algorithms
Greedy Algorithms: CSE373: Design and Analysis of Algorithms
Greedy Algorithms
CSE373: Design and Analysis of Algorithms
Types of Computational Problems
• Decision Problems: given an input x ∈ {0, 1}∗, verify if x
satisfies a certain property i.e., give YES/NO answer
•3-coloring decision problem: given an undirected graph, G=
(V,E), determine if there is a way to assign a “color” chosen
from {1, 2, 3} to each vertex in such a way that no two
adjacent vertices have the same color
d a →b →d
a →c →d
c a →b →c →d
Types of Computational Problems
• Counting Problem: Compute the number of solutions to a
given search problem
• 3-coloring counting problem: given an undirected graph G = (V, E)
compute the number of ways we can color the graph using only 3 colors:
{1,2,3}. While coloring we have to ensure that no two adjacent vertices
have the same color.
• Path counting Problem: Compute the number of possible paths from u
to v in a given graph G.
d
c
Types of Computational Problems
•Optimization Problems: Find the best possible solution among
the set of all possible solutions to a search problem
•Graph coloring optimization problem: given an undirected
graph, G= (V,E), what is the minimum number of colors
required to assign “colors” to each vertex in such a way that
no two adjacent vertices have the same color
•Path optimization problem: Find the shortest path(s) from u
to v in a given graph G.
a b Find all the shortest
paths from a to d:
d a →b →d
a →c →d
c
Greedy Algorithm
• Solves an optimization problem
• Example:
• Activity Selection Problem
• Dijkstra’s Shortest Path Problem
• Minimum Spanning Tree Problem
• For many optimization problems, greedy algorithm can be used. (not
always)
• Greedy algorithm for optimization problems typically go through a
sequence of steps, with a set of choices at each step. Current choice does
not depend on evaluating potential future choices or pre-solving
repeatedly occurring subproblems (a.k.a., overlapping subproblems). With
each step, the original problem is reduced to a smaller problem.
• Greedy algorithm always makes the choice that looks best at the moment.
• It makes a locally optimal choice in the hope that this choice will lead to a
globally optimal solution.
Finding the global minima of a function
• start from an arbitrary value of x
• If f(x+1) < f(x) then set x = x+1
•Otherwise if f(x-1) < f(x) then set x = x-1
• Continue until changing x doesn’t decrease f(x)
A
f(x) A f(x) B
B
C
RECURSIVE-ACTIVITY-SELECTOR(s,
RECURSIVE-ACTIVITY-SELECTOR(s,f,f,i,i,j)j)
//Solves
//Solvesthe
thesubproblem
subproblemSSi,ji,j=={a{ai,i,aai+1i+1, ,…,
…,aaj}j}ofofthe
theproblem
problemS={a
S={a1,a2,…,an}, i.e.,
1,a2,…,an}, i.e.,
//computes
//computesmutually
mutuallycompatible
compatibleactivities activitiesininthe theset
setSSi,j
i,j
1.1. mm==i i++11
//skip
//skipall allactivities
activitieswhich
whichare
areincompatible
incompatiblewith withi i
2.2. while
whilemm<<j jandands[m]s[m]<<f[i]
f[i]
3.3. mm==mm++11
4.4. ifif mm<<j j //if
//ifan
anactivity
activityisisfound
foundininSSi,j which
whichisiscompatible
compatiblewith
withi i
i,j
5.5. return
return{a
{amm}}UURECURSIVE-ACTIVITY-SELECTOR(s,
RECURSIVE-ACTIVITY-SELECTOR(s,f,f,m,
m,j)j)//add
//additittotosolution
solution
set
set
6.6. else returnØØ
elsereturn
• Running time is (n)
• Assuming that the activities have already been sorted by finish times,
• the running time of the call RECURSIVE-ACTIVITY-SELECTOR is (n)
• Because, over all recursive calls, each activity is examined exactly once in
the while loop test of line 2.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Elements of the Greedy Strategy
How can one tell if a greedy algorithm will solve a particular
optimization problem??
An optimal solution which does not begin with activity 1 is: A={2,4,9,11}
Let B = A – {2} U {1} = {1,4,9,11}; B is an optimal solution, too. Why?
Activity 2 doesn’t clash with 4,9, or 11. Activity 1 finishes before 2 finishes
and as such 1 cannot clash with 4,9, or 11, either. Also, activities 4,9, and 11
do not clash with each other (otherwise they wouldn’t be members of A).
Hence B = {1,4,9,11} is a valid solution. Since |B| = |A|, B is an optimal solution.
Optimal Substructure
Once the greedy choice of activity 1 is made, the problem
reduces to finding an optimal solution for the activity-selection
problem over those activities in S that are compatible with
activity 1
Optimal Substructure property
If A is an optimal solution of input S which contains the first
greedy choice (activity 1), then A’ = A – {1} is an optimal
solution of input S’={i S: si f1}
Proof:
If we could find an optimal solution, B’ of S’ where |B’| > |
A’|, then B = B’U{1} would be an optimal solution of S which
contains more activities than A
(since |B| = |B’|+1 > |A’|+1 =|A|),
which contradicts the optimality of A
Optimal Substructure Example
i 1 2 3 4 5 6 7 8 9 10 11
Sj 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
“knapsack”
Solution: P
• 1 ml of 5 50$
Items:
1 2 3 4 5 • 2 ml of 3 40$
• 6 ml of 4 30$
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2 4$
Benefit: $12 $32 $40 $30 $50 10 ml •Total Profit:124$
Value: 3 4 20 5 50
($ per ml)
The Fractional Knapsack Problem
Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with weight at most W.
If we are allowed to take fractional amounts, then this is the fractional knapsack
problem.
In this case, we let xi denote the amount we take of item i
Objective: maximize
Constraint: b (x / w )
iS
i i i
x
iS
i W ,0 xi wi
The Fractional Knapsack Algorithm
Greedy choice: Keep taking item with highest value (benefit to weight ratio)
Since
b ( x / w ) (b / w ) x
iS
i i i
iS
i i i
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
3 bits needed
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
Variable-Length Codes
E.g.: Data file containing 100,000 characters
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
Assign short codewords to frequent characters and long
codewords to infrequent characters
a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100
(45 1 + 13 3 + 12 3 + 16 3 + 9 4 + 5 4) 1,000
= 224,000 bits
Prefix Codes
Prefix codes:
Codes for which no codeword is also a prefix of some other
codeword
E.g.:
a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100
Idea:
f: 5 e: 9 c: 12 b: 13 d: 16 a: 45
Start with a set of |C| leaves
At each step, merge the two least frequent objects: the frequency of the new
node = sum of two frequencies
Use a min-priority queue Q, keyed on f to identify the two least frequent objects
Constructing a Huffman tree
Alg.: HUFFMAN(C)
Running time: O(nlgn)
1. n C
2. Q C
3. for i 1 to n – 1 O(n)
4. do allocate a new node z
5. left[z] x EXTRACT-MIN(Q)
6. right[z] y EXTRACT-MIN(Q) O(lgn)
7. f[z] f[x] + f[y]
8. INSERT (Q, z)
9. return EXTRACT-MIN(Q)
Example
f: 5 e: 9 c: 12 b: 13 d: 16 a: 45 c: 12 b: 13 14 d: 16 a: 45
0 1
f: 5 e: 9
14 d: 16 25 a: 45 25 30 a: 45
0 1 0 1 0 1 0 1
f: 5 e: 9 c: 12 b: 13 c: 12 b: 13 14 d: 16
0 1
f: 5 e: 9
a: 45 55 0 100 1
0 1
a: 45 55
25 30 0 1
0 1 0 1
25 30
c: 12 b: 13 14 d: 16 0 1 0 1
0 1 c: 12 b: 13 d: 16
14
f: 5 e: 9 0 1
f: 5 e: 9
Huffman encoding/decoding using Huffman tree
Encoding:
• Construct Huffman tree using the previous algorithm
•Traverse the Huffman tree to assign a code to each leaf
(representing an input character )
• Use these codes to encode the file
Decoding:
• Construct Huffman tree using the previous algorithm
• Traverse the Huffman tree according to the bits you encounter
until you reach a leaf node at which point you output the
character represented by that leaf node.
• Continue in this fashion until all the bits in the file are read.