Professional Documents
Culture Documents
Introduction to Algorithms
Imagine: You wish to connect all the computers in an
office building using the least amount of cable
Output:
A Minimum
Mi i i ttree T : tree
spanning t that
th t connects
t allll
the vertices and minimizes w(T ) = ∑ w(u, v)
(u ,v )∈T
Greedy Algorithms
Kruskal’s algorithm
Prim’s algorithm
Kruskal'ss Algorithm
Kruskal Kruskal'ss Algorithm
Kruskal
INPUT:
Edge based algorithm
edge-weighted graph G = (V, E), with |V| = n
OUTPUT:
a spanning tree A of G
Greedy strategy: touches all vertices,
has n-1 edges
off minimum
i i costt ( = ttotal
t l edge
d weight)
i ht)
From the remaining edges, select a least-cost
Algorithm:
edge that does not result in a cycle when added
Start with A empty,
to the set of already selected edges Add the edges one at a time, in increasing weight order
MST Kruskal(G,w)
MST-Kruskal(
Does the addition of an edge (u, v) to T
1 A ← ∅ result in a cycle?
2 for each vertex v ∈ V[G] do
3 Set(v) //creates set containing v (for initialization)
Make-Set(
Make Each component
p of T is a tree.
4 sort the edges of E When u and v are in the
5 for each (u,v)∈E do
6 Fi d S t(u) ≠ Find-Set(
if Find-Set( Fi d S t(v) then th // diff
different
t component the addition of the edge (u, v)
same component,
component creates a cycle. 1 3
7 A ← A ∪ {(u,v)} 7 4
2
8 Union(Set(u),Set(v)) // merge
9 return A 2 4
different components, the addition of the edge (u, v)
does not create a cycle. 5 7
6 3
6 8
2 19 2 19
2 19 2 19
9 9
14 17 14 17 9 9
8 25 8 25 14 17 14 17
5 5 8 25 8? 25
5 5
21 13 1 21 13 1?
21 13 1 21 13 1
2 19 2? 19
2 19 2 19
9 9
14 17 14 17 9 9?
8 25 8 25 14 17 14 17
5 5 25 8 25
5 5
21 13 1 21 13 1
21 13 1 21 13 1
Kruskal’ss Algorithm
Kruskal
2 19 2 19
Kruskal’ss Algorithm
Kruskal
2 19? 2 19
9 9 9 9
14 17 14 17 14 17 14 17
8 25 8 25 8 25 8 25
5 5 5 5
21 13 1 21 13? 1 21 13 1 21? 13 1
2 19 2 19 2 19 2 19
9 9 9 9
14 17 14
14? 17 14 17 14 17
8 25 8 25 8 25? 8 25
5 5 5 5
21 13 1 21 13 1 21 13 1 21 13 1
2 19 2 19 2 19
9 9 9
14 17 14 17? 14 17
8 25 8 25 8 25
5 5 5
21 13 1 21 13 1 21 13 1
5 5
A B A B
4 6 2 4 6 2
2 D 3 2 D 3
C C
3 1 2 3 1 2
E F E F
4 4
5 5
A B A B
4 6 2 4 6 2
2 D 3 2 D 3
C C
3 1 2 3 1 2
E F E F
4 4
5 5
A B A B
4 6 2 4 6 2
2 D 3 2 D 3 cycle!!
C C
3 1 2 3 1 2
E F E F
4 4
5 5
A B A B
4 6 2 4 6 2
2 D 3 2 D 3
C C
3 1 2 3 1 2
E F E F
4 4
Kruskal'ss Algorithm
Kruskal
minimum-
u spspanning
g tree
ee MST-Kruskal(G,w)
1 A ← ∅
2 for each vertex v ∈ V[G] do // takes O(V)
3 (v)
Make-Set(
4 sort the edges of E // takes O(E lg E)
A B // takes O(E)
5 for each (u,v)∈E, in nondecreasing of weight do
2
6 if Find-Set(u) ≠ Find-Set(v) then
7 A ← A ∪ {(u,v)}
2 D 8 Union(Set(u),Set(
) Set(v))
C 9 return A
3 1 2
E F
So total running time becomes O(E lg V). Grows one tree T, one vertex at a time
Prim’ss Algorithm
Prim Prim’ss Algorithm
Prim
MST-Prim(G,w,r) //G: graph with weight w and a root vertex r
Lines 1-5 set the key of each vertex to ∞ (except root r,
1 for each u ∈ V[G]{
2 key[u] ← ∞ whose key is set to 0 first vertex processed).
processed) Also
Also, set
3 p[u] ← NULL // parent of u parent of each vertex to NULL, and initialize min-priority
} queue Q to contain all vertices.
4 key[r] ← 0
5 Q = BuildMinHeap(V,key); // Q – vertices out of T
6 while Q ≠ ∅ do Line 7 identifies a vertex u є Q
7 u ← ExtractMin(Q) // making u part of T
8 for each v ∈ Adj[u] do
9 if v ∈ Q and w(u,v)
( , ) < key[v]
y[ ] then updating Removing u from set Q adds it to set Q-V of vertices in tree,
10 p[v] ← u keys thus adding (u, p[ u]) to A.
11 key[v] ← w(u,v)
The for loop of lines 8-11
8 11 update key and p fields of every
• For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. vertex v adjacent to u but not in tree.
• key [v]= ∞ if there is no edge and p [v] names parent of v in tree.
• When
Wh algorithm
l i h terminatesi the
h min-
min
i -priority
i i queue Q is i empty.
14 2 14 2
10 10
15 15
∞ ∞ ∞
3 8 3 ∞ 8
key[u] = ∞
Run on example graph Run on example graph
6 ∞ 4 6 ∞ 4
5 9 5 9
∞ ∞ ∞ ∞ ∞ ∞
14 2 14 2
10 10
15 15
r 0 ∞ ∞ u 0 ∞ ∞
3 ∞ 8 3 ∞ 8
14 2 14 2
10 10
15 15
u 0 ∞ ∞ u 0 ∞ ∞
3 3 8 3 3 8
14 2 14 2
10 10
15 15
0 ∞ ∞ 0 8 ∞
3 3 8 3 3 8
u u
14 2 14 2
10 10
15 15
0 8 ∞ 0 8 ∞
3 3 8 3 3 8
u u
Run on example graph Run on example graph
6 ∞ 4 6 ∞ 4
5 9 5 9
10 2 ∞ 10 2 ∞
14 2 14 2
10 10
15 15
0 8 ∞ 0 8 15
3 3 8 3 3 8
u u
14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8
Run on example graph Run on example graph
4 u 4 u
6 4 6 4
5 9 5 9
10 2 9 5 2 9
14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8
14 2 14 2
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8
Run on example graph Run on example graph
u
6 4 4 6 4 4
5 9 5 9
5 2 9 5 2 9
14 2 14 2 u
10 10
15 15
0 8 15 0 8 15
3 3 8 3 3 8