Algo Chap4
Algo Chap4
CHAPTER 4
Foundations of Algorithms
2
Greedy Algorithm
Greedy Algorithm
arrives at a solution by making a sequence of choices,
each of which simply looks the best at the moment
- Once each choice has been made and added to a
partial solution, it will always be in the solution set.
- Each choice is locally optimal, but not necessarily
globally optimal.
3
Greedy Algorithm
Example: Giving Change for a Purchase
Solution: a smallest number of bills/coins totaling the
amount of change
\7,870 : one \5,000 bill,
two \1,000 bills,
one \500 coin,
three \100 coins,
one \50 coin,
two \10 coins.
4
Greedy Algorithm
Example: Giving Change for a Purchase
while (there are more bills/coins and the instance is not solved)
{
grab the largest remaining bill; //selection procedure
if (adding the bill/coin makes the change exceed the amount owed)
// feasibility check
reject the bill/coin ;
else
add the bill/coin to the change ;
if (the total value of the change equals the amount owed)
// solution check
the instance is solved ;
}
5
Greedy Algorithm
General Greedy Approach
- starts with an empty set, and adds items to the set in sequence
until the set represents a solution to an instance of a problem
- each iteration consists of the following components:
1. Selection Procedure
chooses the next item to add to the set according to a greedy criteria
2. Feasibility Check
checks whether it is possible to complete the set in such a way
as to give a solution to the instance.
3. Solution Check
determines whether the new set constitutes a solution to the instance.
6
4.1 Minimum Spanning Trees
Definitions
Undirected Graph
- a graph where its edges do not have directions
Connected Graph
- an undirected graph where there is a path between
every pair of vertices
Acyclic Graph
- a graph with no cycles
Tree
- an acyclic, connected, undirected graph
7
4.1 Minimum Spanning Trees
Definitions
Spanning Tree for undirected graph
- a connected subgraph that contains all the vertices in G
and is a tree
Minimum Spanning Tree for undirected graph
- a spanning tree of G with minimum weight
8
4.1 Minimum Spanning Trees
Examples of Graph
1 1 G4,G5:
V1 V2 V1 V2
G1: 3 G2: minimum
3 6 6
4 4 spanning trees
V3 V4 V3 V4
2 5 2 5
of G1
V5 V5
1 1 1
V1 V2 V1 V2 V1 V2
G3: 3 G4: G5: 3
6 3
4 4
V3 V4 V3 V4 V3 V4
5 2 2
V5 V5 V5
9
4.1 Minimum Spanning Trees
4.1.1 Prim’s Algorithm
F=Ø;
Y = {v1}
while (the instance is not solved)
{
select a vertex in V-Y that is nearest to Y ;
if (Y==V)
the instance is solved ;
}
10
4.1 Minimum Spanning Trees
4.1.1 Prim’s Algorithm
1 Y V1 1 Y V1 1
V1 V2 V2 V2
3 3 6 3 3 6 3 3 6
4 4 4
V3 V4 V3 V4 V3 V4
2 5 2 5 2 5
V5 V5 V5
Y V1 1 Y V1 1 Y V1 1
V2 V2 V2
3 3 6 3 3 6 3 3 6
4 4 4
V3 V4 V3 V4 V3 V4
2 5 2 5 2 5
V5 V5 V5
11
4.1 Minimum Spanning Trees
4.1.1 Prim’s Algorithm
- To find the nearest vertex from Y, we use the following
arrays:
W[i][j] : Weight table (symmetric)
nearest[i] = index of the vertex in Y nearest to Vi
distance[i] = weight on edge between Vi and the
vertex indexed by nearest[i]
- nearest[i] is initialized to 1
- distance[i] is initialized to W[1][i]
- as vertices are added to Y, two arrays are updated to
reference the new vertex in Y nearest to each vertex outside Y
12
4.1 Minimum Spanning Trees
4.1.1 Prim’s Algorithm
public static set_of_edges prim(
int n, const number W[][]) min = ∞ ;
{ for (i=2; i<=n; i++)
index i, vnear; if (0<= distance[i] < min) {
index [ ] nearest = new index[2..n] ; min = distance[i] ; vnear = i ;
number min; }
number[ ] distance = new number[2..n] ; e = edge connecting vertices indexed
edge e;
by vnear and nearest[vnear] ;
set_of_edges F = Ø;
add e to F ;
for (i=2; i<=n; i++) { distance[vnear] = -1 ;
nearest[i]= 1; for (i=2; i<=n; i++)
distance[i] = W[1][i]; if (W[i][vnear] < distance[i]) {
} distance[i] = W[i][vnear] ;
repeat( n-1 times ) { … } nearest[i] = vnear ;
return F ;
}
}
13
4.1 Minimum Spanning Trees
Time Complexity of Prim’s Algorithm
Basic Operation:
if-statement inside two for-i loops
Input Size:
n, the number of vertices
e e
F F ∪{e} F′
19
4.1 Minimum Spanning Trees
e e e
e′
F F ∪{e} F′ F′∪{e} F′∪{e} – {e′}
21
4.1 Minimum Spanning Trees
4.1.2 Kruskal’s Algorithm
1. F = Ø ;
2. create disjoint subsets of V, one for each vertex and containing only
that vertex;
3. sort the edges in E in non-decreasing order ;
4. while (the instance is not solved) {
select next edge from the sorted list
if (the edge connects two vertices in disjoint subsets) {
merge the subsets ;
add the edge to F ;
}
if (all the subsets are merged)
the instance is solved ;
}
22
4.1 Minimum Spanning Trees
4.1.2 Kruskal’s Algorithm
Example:
1. Sorted Edges:
1 (v1,v2) 1
V1 V2
3 3 6 (v3,v5) 2
4 (v1,v3) 3
V3 V4
2 5 (v2,v3) 3
V5
(v3,v4) 4
(v4,v5) 5
(v2,v4) 6
23
4.1 Minimum Spanning Trees
4.1.2 Kruskal’s Algorithm
1 1 1
Sorted Edges V1 V2 V1 V2 V1 V2
3 3 3 3 6
(v1,v2) 1 3 6 3 6
4 4 4
(v3,v5) 2 V3 V4 V3 V4 V3 V4
2 5 2 5 2 5
(v1,v3) 3 V5 V5 V5
(v2,v3) 3 1 1 V1
1
V2
V1 V2 V1 V2
(v3,v4) 4 3 3 6 3 3 6 3 3 6
(v4,v5) 5 V3 4
V4 V3 4
V4 V3 4
V4
(v2,v4) 6 2 5 2 5 2 5
V5 V5 V5
24
4.1 Minimum Spanning Trees
4.1.2 Kruskal’s Algorithm
- To write a formal version of Kruskal’s Algorithm, we use
the following:
- Initial(n) initializes n disjoint subsets, each of which
contains exactly one of the indices between 1 and n
- p= find(i) makes p point to the set containing index i
- merge(p,q) merges the two sets, to which p and q
point, into a set.
- equal(p,q) returns true if both p and q point to the
same set.
25
4.1 Minimum Spanning Trees
4.1.2 Kruskal’s Algorithm
public static set_of_edges kruskal(
int n, int m, set_of_edges E) {
index i, j;
e = edge with the least weight
set_pointer p,q ;
not yet considered ;
edge e;
i,j = indices of vertices connected by e;
set_of_edges F = Ø ;
p = find(i) ;
Sort the m edges in E by weight
q = find(j) ;
in nondecreasing order;
if (! Equal(p,q)) {
initial(n) ;
merge(p,q) ;
while( |F| < n-1)
add e to F ;
{ }
…
return F ;
}
}
26
4.1 Minimum Spanning Trees
e e
F F ∪{e} F′
31
4.1 Minimum Spanning Trees
e e e
e′ e′
Huffman Code
- an efficient encoding method for data compression
- uses a variable-length binary code to represent a text file
Note:
Variable-Length Binary Code
a: 00, b: 01, c: 11
ababcbbbc: 000100011101010111 - 18 bits
Variable-Length Binary Code
a: 10, b: 0, c: 11
ababcbbbc: 1001001100011 - 13 bits
36
4.4 Huffman Code
p Example
b:5 e:10 c:12 a:16 d:17 f:25 c:12 15 a:16 d:17 f:25
0 1
b:5 e:10
p Example (cont’d) 0 1
0 0 1
33 52
1
a:16 f:25
0 1 0 1 d:17 0 1
a:16 d:17 f:25 27
c:12
0 1 0 1
c:12 15 b:5 e:10
0 1
b:5 e:10
44
4.4 Huffman Code
p Proof of the Optimality of Huffman’s Algorithm
p Induction Basis
pThe set of single nodes in the 0th step
branches in an optimal prefix binary tree
p Induction Hypothesis
pThe set of trees in the ith step
branches in an optimal prefix binary tree T
p Induction Step
p u & v: roots of trees combined in the (i+1)th step
p NEXT PAGE..
45
4.4 Huffman Code
w u u w
46
4.4 Huffman Code