Greedy Technique
Greedy Technique
Greedy Technique
Approximations/heuristics:
• traveling salesman problem (TSP)
• knapsack problem
• other combinatorial optimization problems
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-3
Change-Making Problem
Greedy solution is
optimal for any amount and “normal’’ set of denominations
Ex: Prove the greedy algorithm is optimal for the above denominations.
may not be optimal for arbitrary coin denominations
For example, d1 = 25c, d2 = 10c, d3 = 1c, and n = 30c
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-4
Minimum Spanning Tree (MST)
Spanning tree of a connected graph G: a connected acyclic
subgraph of G that includes all of G’s vertices
Example:
6 c 6 c c
a a a
4 1 4 1 1
2 2
d d d
b 3 b b 3
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-5
Prim’s MST algorithm
Start with tree T1 consisting of one (any) vertex and “grow”
tree one vertex at a time to produce MST through a series of
expanding subtrees T1, T2, …, Tn
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-6
Example
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d b d d
b 3 3 b 3
4 c
4 c
a
a 1
1 6
6
2
2
b d
b d 3
3
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-7
Notes about Prim’s algorithm
Proof by induction that this construction actually yields an
MST (CLRS, Ch. 23.1). Main property is given in the next
page.
Efficiency
• O(n2) for weight matrix representation of graph and array
implementation of priority queue
• O(m log n) for adjacency lists representation of graph with
n vertices and m edges and min-heap implementation of the
priority queue
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-8
The Crucial Property behind Prim’s Algorithm
Y
y
x
X
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-9
Another greedy algorithm for MST: Kruskal’s
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-10
Example
4 c 4 c 4 c
a a a
1 1 1
6 6 6
2 2 2
d d d
b 3 b 3 b 3
4 c c c
a a a
1 1 1
6 6
2 2 2
d d d
b 3 b 3 b 3
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-11
Notes about Kruskal’s algorithm
Algorithm looks easier than Prim’s but is harder to
implement (checking for cycles!)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-12
Minimum spanning tree vs. Steiner tree
1 a c
a c
1 1 vs s
b d b d
1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-14
4
b c
Example a
3
7 2
d
d
5 4
6
4
b c
b(a,3) c(b,3+4) d(b,3+2) e(-,∞) 3 6
2 5
a d e
7 4
4
d(b,5) c(b,7) e(d,5+4) 3
b c
6
2 5
a d e
7 4
4
c(b,7) e(d,9) 3
b c
6
2 5
a d e
7 4
e(d,9)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-15
Notes on Dijkstra’s algorithm
Correctness can be proven by induction on the number of vertices.
We prove the invariants: (i) when a vertex is added to the tree, its correct distance
is calculated and (ii) the distance is at least those of the previously added vertices.
Doesn’t work for graphs with negative weights (whereas Floyd’s
algorithm does, as long as there is no negative cycle). Can you find a
counterexample for Dijkstra’s algorithm?
Efficiency
• O(|V|2) for graphs represented by weight matrix and array
implementation of priority queue
• O(|E|log|V|) for graphs represented by adj. lists and min-heap
implementation of priority queue
0 1
Optimal binary tree minimizing the average
1
length of a codeword can be constructed
as follows:
represents {00, 011, 1}
Huffman’s algorithm
Initialize n one-node trees with alphabet characters and the tree weights
with their frequencies.
Repeat the following step n-1 times: join two binary trees with smallest
weights into one (as left and right subtrees) and make its weight equal the
sum of the weights of the two trees.
Mark edges leading to left and right subtrees with 0’s and 1’s, respectively.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-18
0.1 0.15 0.2 0.2 0.35
Example B _ C D A
_
0.1 0.15
character A B C D B _
0.4 0.6
1.0
0 1
0.4 0.6
0 1 0 1
0.1 0.15
B _
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 9 9-19