Greedy Algorithms
Greedy Algorithms
Greed is good.
(Some of the time)
Outline
Elements of greedy algorithm
Greedy choice property
Optimal substructures
Minimum spanning tree
Kruskal’s algorithm
Prim’s algorithm
Huffman code
Activity selection
A feasible solution
a solution that satisfies the constraints
An optimal solution
a feasible solution for which the optimization function has
the best possible value
In general, finding an optimal solution is computationally
hard
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4
3 (D,E) 1 (B,E) 4
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
}
2
3 (G,H) 3 (D,F) 6
G E (C,F) 3 (A,B) 8
not
consider
ed
(B,C) 4 (A,F) 10
Done
Total Cost = dv =
21
12 8
2 9 1 9
5 3 11
8 7 3 5
1 10
Find-Set(v)
Return member[v]
Scan through the member array and update old members to be the new set.
Running time O(n), length of member array.
Union(2,3)
member = [1,3,3] ; {1} {2 3}
14
2
10
e f g
15
3
h 8
• A={ }, Make each element its own set. {a} {b} {c} {d} {e} {f} {g}
{h}
• Sort edges.
• Look at smallest edge first: {c} and {f} not in same set, add it to A,
union together.
• Now get {a} {b} {c f} {d} {e} {g} {h}
Ravirajsinh Chauhan Greedy Algorithms 41
Kruskal Example
Keep going, checking next smallest edge.
Had: {a} {b} {c f} {d} {e} {g} {h}
{e} ≠ {h}, add edge.
a
6 4
5 9
b c d
14
2
10
e f g
15
3
h 8
14
2
10
e f g
15
3
h 8
14
2
10
e f g
15
3
h 8
14
2
10
e f g
15
3
h 8
14
2
10
e f g
15
3
h 8
a
6 4
5 9
b c d
14
2
10
e f g
15
3
h 8
a
6 4
5 9
b c d
14
2
10
e f g
15
3 8
h
a
6 4
5 9
b c d
14
2
10
e f g
15
3 8
h
A 7
4
3 A
8
18 B
4
9
B D C 3 D
10
H 25
D T 0
2
3 E 25 D
G 7
E F 18 D
G T 2 D
H
A 7
4
3 A
8
18 B
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
A 7
4
3 A
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
Table entries
unchanged
A 7
4
3 A 10 F
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7 E F T 3 C
G T 2 D
H T 3 G
A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
A 7
4
3 A T 4 H
8
18 B 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
Table entries
unchanged
A 7
4
3 A T 4 H
8
18 B T 4 C
4
9
B D C T 3 D
10
H 25
D T 0
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
A 4
3 A T 4 H
B T 4 C
4
B D C T 3 D
H D T 0
2
3 E T 2 F
G E F T 3 C
G T 2 D
H T 3 G
Done
12 8
2 9 1 9
5 3 11
8 7 3 5
1 10
12 8
0 12
9
3 8
2 9 1 9
5 2 3 3
1 11
8 7 3 5
5 1 1
7 10 10
5
inf
a
6 4
inf inf inf
5 9
b c d
14
2
10 inf
e f g
0/nil 15
3 inf
8
h
inf
14
2
10 inf
e f g
0/nil 15
3 inf
h 8
3/e
Q={ (a, ) (b,14) (c, ) (d, ) (f, ) (g, ) (h,3) }
14
2
10 inf
e f g
0/nil 15
3 8/h
h 8
3/e
14
2
10 15/f
e f g
0/nil 15
3 8/h
h 8
3/e
Q={ (a, ) (b,10) (c, 2) (d, ) (g,15) }
14
2
10 15/f
e f g
0/nil 15
3 8/h
h 8
3/e
Q={ (a,4) (b,5) (d,9) (g,15) }
Extract min, vertex a. No keys are smaller than edges from a (4>2 on edge ac, 6>5 on edge ab) so nothing
done.
4/c
a
6 4
5/c 2/f 9/c
5 9
b c d
14
2
10 15/f
e f g
0/nil 15
3 8/h
8
h
3/e
The inner loop takes O(E lg V) for the heap update inside the O(E) loop.
This is over all executions, so it is not multiplied by O(V) for the while loop
(this is included in the O(E) runtime through all edges.
64
0 1
0
34 30
1
0 1 0 15
0 1
ก: 36 ข: 17 ค: 17 ง: 15 จ: 10 ฉ: 5
0 100 101 110 1110 1111
B B
C A C A
4 3 1 2 D 3 1 2
5 4
C
D
4 B
5 B
3 A
C A
1 2
4 3 1 2
ขคงจฉ: 64
0 0 1
ขค: 34 งจฉ: 30
1
0 1 0 จฉ: 15
0 1
ก: 36 ข: 17 ค: 17 ง: 15 จ: 10 ฉ: 5
0 100 101 110 1110 1111
Ravirajsinh Chauhan Greedy Algorithms 86
Theorem
A full binary tree for an optimal prefix code for C letters
has exactly C leaves, and exactly C−1 internal nodes.
Proof by induction.
Basis:
C=1. If there is one letter, the binary tree requires only 1
leaf node, and 0 internal node.
Induction hypotheses:
For C< n, the full binary tree for an optimal prefix code
for C letters has exactly C leaves, and exactly C−1
internal nodes.
Since f [x] and f [y] are the two lowest leaf frequencies, in
order, and f [a] and f [b] are two arbitrary frequencies, in
order, we have f [x] ≤ f [a] and f [y] ≤ f [b].
= f [x] dT (x) + f [a] dT (a) − f [x] dT’ (x) − f [a] dT’ (a)
= f [x] dT (x) + f [a] dT (a) − f [x] dT (a) − f [a] dT (x)
= ( f [a] − f [x])( dT (a) − dT (x)) ≥ 0
T T’
x a
y y
a b x b
Ravirajsinh Chauhan Greedy Algorithms 93
Proof
Then, exchange the positions of b
and y in T’ to produce T’’. T’
a
Similarly, it does not increase the
cost, and so B(T’ ) − B(T’’ ) 0. y
Therefore, B(T’’ ) ≤ B(T).
x b
Since T is optimal, B(T) ≤ B(T’’ ).
Then, B(T’’ ) = B(T).
T’’
Thus, T’’ is an optimal tree in which a
x and y appear as sibling leaves of
maximum depth. b
x y
Ravirajsinh Chauhan Greedy Algorithms 94
Optimal-substructure Property
Let C be a given alphabet with frequency f [c] defined for
each character c C,
x and y be two characters in C with minimum frequency,
C’ be the alphabet C with characters x, y removed and
(new) character z added, so that C’ = C − {x, y} {z};
define f for C’ as for C, except that f [z] = f [x] + f [y], and
T be any tree representing an optimal prefix code for the
alphabet C ’.
Then the tree T , obtained from T ’ by replacing the leaf
node for z with an internal node having x and y as
children, represents an optimal prefix code for the
alphabet C.
Ravirajsinh Chauhan Greedy Algorithms 95
Proof
Show that the cost B(T) can be expressed in terms of the
cost B(T’) by considering the component costs.
For each c C − {x, y}, we have
dT (c) = dT ’ (c).
f [c] dT (c) = f [c] dT ’ (c).
Since dT (x) = dT (y) = dT ’(z) + 1, we have
f [x]dT (x) + f [y]dT (y) = ( f [x] + f [y])( dT’ (z) + 1)
= f [z] dT ’ (z) + ( f [x] + f [y])
Thus, B(T) = B(T ’) + f [x] + f [y].
That is, B(T ’) = B(T) − f [x] − f [y] .
Then there exists a tree T ’’ such that B(T ’’) < B(T).
A1
A2
A3
Aij ak
A’ij am
Aij ak
A’ij am
ai
am
S
ak
aj
Ai j = {ak} Akj