SlideShare a Scribd company logo
1 of 55
1
Minimum Spanning TREE
Kruskal’s Algorithm
Prim’s Algorithm
What is a Spanning Tree
 In order to find a spanning tree, we must make sure that every node (vertex) is
some how connected to the rest of the nodes by arcs (edges)
 Use the fewest number of edges possible.
3
4
Spanning tree but not
MST
Spanning trees
What is a Spanning Tree
 In order to find a spanning tree, we must make sure that every node (vertex) is
some how connected to the rest of the nodes by arcs (edge)
 Use the fewest number of edges possible.
5
What is a Spanning Tree
 In order to find a spanning tree, we must make sure that every node (vertex) is
some how connected to the rest of the nodes by arcs (edge)
 Use the fewest number of edges possible.
 If a loop can be found,
then one of the edges is not necessary
6
What is a Spanning Tree
 In order to find a spanning tree, we must make sure that every node (vertex) is
some how connected to the rest of the nodes by an arc (edge)
 Use the fewest number of edges possible.
 You may think now, How many edges you
have in a spanning tree given N vertices
7
Minimum Spanning Tree
 The subset of edges E that connects all vertices V in the graph, and has
minimum total weight.
a
b c
d
1
6
54
2
8
Minimum Spanning Tree
 The subset of edges E that connects all vertices V in the graph, and has
minimum total weight.
a
b c
d
1
6
54
2
MST = { (a,b),(a,d),(d,c) }
9
10
APPLICATIONS OF MST
Communication problem.
Circuit diagram.
T.V. cables.
Kruskal’s Algorithm
THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS
MINIMUM SPANNING TREE
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
 Kruskal's algorithm utilizes Disjoint Sets (DS).
12
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
 Kruskal's algorithm utilizes Disjoint Sets (DS).
 DS has two Operations Find and Union.
13
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
 Find: takes an item and pretend that this is a
DS that item belongs to
 Union: will merge two DS of its input items in to
one DS
14
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 A: is used to store all edges of the minimum
spanning tree
Initially A is empty
Eventually return A
15
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 for each vertex v of the graph, make a DS with v
 Each vertex will be used to create a single vertex
Disjoint Set
16
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 Sort all edges by weight increasingly
 the edge of smallest weight will be in front and the one
with largest weight will be at the end
17
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 1st we will take the edge with the smallest weight
 C and f don’t belong to the same DS
18
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),}
 c and f will be merged in a one DS
 (c,f) is pushed to A
19
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),}
20
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),}
21
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),}
 c and d will be merged in a one DS
22
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),}
 c and d will be merged in a one DS
23
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),}
24
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),}
 e and f belong to the same DS
 DO NOT MERGE !!
25
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),(a,b)}
26
Kruskal’s Algorithm
Kruskal(V,E)
A = ø
foreach v V:
Make-disjoint-set(v)
Sort E by weight increasingly
foreach (v1,v2) E:
if Find(v1) ≠ Find(v2):
A = A U {(v1,v2)}
Union (v1,v2)
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { (c,f),(a,f),(d,e),(c,d),(a,b)}
 Now we have a Spanning Tree that connects all
vertices which have the minimum total weight
27
Kruskal’s Algorithm 28
Kruskal’s Algorithm 29
Prim’s Algorithm
THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS
MINIMUM SPANNING TREE
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 A: is used to store all edges of the minimum
spanning tree
Initially A is empty
Eventually return A
31
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 for each vertex v of the graph, Each vertex will have
two attributes KEY and PARENT
 KEY and PARENT initialized to ∞ and null Respectively
32
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
a
2
c
b d
f
4
6
3
4
15
e
2
A = { }
 for each vertex v of the graph, Each vertex will have
two attributes KEY and PARENT
 KEY and PARENT initialized to ∞ and null Respectively
∞,null
∞,null
∞,null
∞,null
∞,null
∞,null
33
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Randomly choose a vertex as a ROOT r and set its
KEY value to Zero
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
34
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Create Q list which contains all vertices in the graph
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { a,b,c,d,e,f }
35
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Find Q with the smallest (Minimum) KEY
 In this case, KEY[a] = 0 (Minimum KEY)
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { a,b,c,d,e,f }
36
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Remove a from Q
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
37
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 PARENT [a] = null !!
 Skip the if statement
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
38
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Update attributes for all vertices Adjacent to a which
are b and f
 Starting with f , 2 < ∞
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
39
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { }
 Update attributes for all vertices Adjacent to a which are b
and f
 Starting with f , 2 < ∞
 PARENT [ f ] = a and KEY [ f ] = 2
∞,null
∞,null
∞,null
2, a
0,null
4, a
Q = { b,c,d,e,f }
40
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f) }
 Now, Enter the Second Loop
 a ! Q
 u = min(Q) = f , Remove f from Q
 PARENT [ f ] ! = null, push (a,f) to A
∞,null
∞,null
∞,null
2, a
0,null
4, a
Q = { b,c,d,e }
41
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f) }
 Update attributes for all vertices Adjacent to f which
are c and d where weight < KEY
1, f
∞,null
4, f
2, a
0,null
4, a
Q = { b,c,d,e }
42
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f) }
 Now, Enter the NEXT Loop
 u = min(Q) = c , Remove c from Q
 PARENT [ c ] ! = null, push (c,f) to A
1, f
∞,null
4, f
2, a
0,null
4, a
Q = { b,d,e }
43
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f) }
 Update attributes for all vertices Adjacent to c where
weight < KEY
1, f
3, c
4, f
2, a
0,null
4, a
Q = { b,d,e }
44
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d) }
 Enter the NEXT Loop
 u = min(Q) = d , Remove d from Q
 PARENT [ d ] ! = null, push (c,d) to A
1, f
3, c
4, f
2, a
0,null
4, a
Q = { b,e }
45
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d) }
 Update attributes for all vertices Adjacent to d where
weight < KEY
1, f
3, c
2, d
2, a
0,null
4, a
Q = { b,e }
46
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e) }
 Enter the NEXT Loop
 u = min(Q) = e , Remove e from Q
 PARENT [ e] ! = null, push (d,e) to A
1, f
3, c
2, d
2, a
0,null
4, a
Q = { b }
47
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e) }
 No Updates for attributes because d and f ! Q
1, f
3, c
2, d
2, a
0,null
4, a
Q = { b }
48
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
 Enter the NEXT Loop
 u = b , Remove b from Q
 PARENT [ b] ! = null, push (a,b) to A
1, f
3, c
2, d
2, a
0,null
4, a
Q = { }
49
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
 No Updates for attributes because all neighbors of b
doesn’t exist in Q
1, f
3, c
2, d
2, a
0,null
4, a
Q = { }
50
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
 Enter the NEXT Loop, Q = ø
 Exit loop
 Return A
Q = { }
51
Prim’s Algorithm
A = ø
foreach v V:
KEY[v] = ∞
PARENT[v] = null
KEY[r] = 0
Q = V
While Q != ø:
u = min (Q) by KEY value
Q = Q – u
if PARENT(u) != null:
A = A U (u, PARENT(u))
foreach v Adj(u):
if v Q and w(u,v) < KEY[v]:
PARENT[v] = u
KEY[v] = w
Return A
3
a
2
c
b d
f
4
6
4
15
e
2
A = { (a,f), (c,f), (c,d), (d,e), (a,b) }
 Now we have a Spanning Tree that connects all
vertices which have the minimum total weight
Q = { }
52
Prim’s Algorithm 53
Prim’s Algorithm
 The time complexity of the algorithm is computed as follows:
 Step 1 costs q(n) time.
 The for loop in Step 2 requires Ø(n) time.
 The time taken by steps 3 - 6 is O(n).
 The time taken by Step 10 to search for a vertex y closest to X is Ø(n) per iteration. This
is because the algorithm inspects each entry in the vector representing the set Y. Since
it is executed n-1 times, the overall time required by Step 10 is Ø(n2).
 Steps 11,12 and 13 cost Ø(1) time per iteration for a total of q(n) time.
 The for loop in Step 14 is executed 2m times, where m=|E|. This is because each edge
(y, w) is inspected twice: once when y is moved to X and the other when w is moved to
X.
 Hence, the overall time required by step 14 is Ø(m).
 The test in Step 15 is executed exactly m times, and Steps 16 and 17 are executed at
most m times.
 Thus, Steps 14 to 19 cost q(m) time.
 It follows that the time complexity of the algorithm is
 Ø(m + n2) = Ø (n2)
54
THANKS

More Related Content

What's hot

Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
zhaokatherine
 

What's hot (20)

Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Graph theory
Graph theory Graph theory
Graph theory
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHM
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 

Viewers also liked

Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
Kumar
 
Project PPT slides_student_id#104519347
Project PPT slides_student_id#104519347Project PPT slides_student_id#104519347
Project PPT slides_student_id#104519347
savan Darji
 
Norton antivirus support phone number
Norton antivirus support phone numberNorton antivirus support phone number
Norton antivirus support phone number
PC Tech Support
 
Акция благотворительность вместо сувениров
Акция благотворительность вместо сувенировАкция благотворительность вместо сувениров
Акция благотворительность вместо сувениров
Фонд Вера
 
University Transcript
University TranscriptUniversity Transcript
University Transcript
Sophie Winton
 

Viewers also liked (19)

Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Project PPT slides_student_id#104519347
Project PPT slides_student_id#104519347Project PPT slides_student_id#104519347
Project PPT slides_student_id#104519347
 
Kruskal’s Algorithm
Kruskal’s AlgorithmKruskal’s Algorithm
Kruskal’s Algorithm
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Business Law PPT
Business Law PPTBusiness Law PPT
Business Law PPT
 
Skuteczne strategie windykowania należności
Skuteczne strategie windykowania należnościSkuteczne strategie windykowania należności
Skuteczne strategie windykowania należności
 
Norton antivirus support phone number
Norton antivirus support phone numberNorton antivirus support phone number
Norton antivirus support phone number
 
Life matters consultancy
Life matters consultancyLife matters consultancy
Life matters consultancy
 
Patricia Augusta Resume 2015
Patricia Augusta Resume 2015Patricia Augusta Resume 2015
Patricia Augusta Resume 2015
 
Desenvolvimento de um REA - Diário de Produção
Desenvolvimento de um REA -  Diário de Produção Desenvolvimento de um REA -  Diário de Produção
Desenvolvimento de um REA - Diário de Produção
 
The deadliest lung cancer on women and preventive methods
The deadliest lung cancer on women and preventive methodsThe deadliest lung cancer on women and preventive methods
The deadliest lung cancer on women and preventive methods
 
Java8
Java8Java8
Java8
 
Акция благотворительность вместо сувениров
Акция благотворительность вместо сувенировАкция благотворительность вместо сувениров
Акция благотворительность вместо сувениров
 
University Transcript
University TranscriptUniversity Transcript
University Transcript
 
نتيجه السادس الإبتدائي دمياط 2016
نتيجه السادس الإبتدائي دمياط 2016نتيجه السادس الإبتدائي دمياط 2016
نتيجه السادس الإبتدائي دمياط 2016
 

Similar to Prims & kruskal algorithms

lecture 22
lecture 22lecture 22
lecture 22
sajinsc
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
Naor Ami
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
ASPAK2014
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
Kittel c. introduction to solid state physics 8 th edition - solution manual
Kittel c.  introduction to solid state physics 8 th edition - solution manualKittel c.  introduction to solid state physics 8 th edition - solution manual
Kittel c. introduction to solid state physics 8 th edition - solution manual
amnahnura
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
Traian Rebedea
 

Similar to Prims & kruskal algorithms (20)

lecture 22
lecture 22lecture 22
lecture 22
 
lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Capitulo 4, 7ma edición
Capitulo 4, 7ma ediciónCapitulo 4, 7ma edición
Capitulo 4, 7ma edición
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
 
Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...
 
lecture 21
lecture 21lecture 21
lecture 21
 
On Uq(sl2)-actions on the quantum plane
On Uq(sl2)-actions on the quantum planeOn Uq(sl2)-actions on the quantum plane
On Uq(sl2)-actions on the quantum plane
 
Linear Algebra.pptx
Linear Algebra.pptxLinear Algebra.pptx
Linear Algebra.pptx
 
Kittel c. introduction to solid state physics 8 th edition - solution manual
Kittel c.  introduction to solid state physics 8 th edition - solution manualKittel c.  introduction to solid state physics 8 th edition - solution manual
Kittel c. introduction to solid state physics 8 th edition - solution manual
 
Aed.pptx
Aed.pptxAed.pptx
Aed.pptx
 
Least Squares
Least SquaresLeast Squares
Least Squares
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
k - Symmetric Doubly Stochastic, s - Symmetric Doubly Stochastic and s - k - ...
k - Symmetric Doubly Stochastic, s - Symmetric Doubly Stochastic and s - k - ...k - Symmetric Doubly Stochastic, s - Symmetric Doubly Stochastic and s - k - ...
k - Symmetric Doubly Stochastic, s - Symmetric Doubly Stochastic and s - k - ...
 
Chpt 2-sets v.3
Chpt 2-sets v.3Chpt 2-sets v.3
Chpt 2-sets v.3
 

More from Ayesha Tahir (6)

STORAGE DEVICES & OPERATING SYSTEM SERVICES
STORAGE DEVICES & OPERATING SYSTEM SERVICESSTORAGE DEVICES & OPERATING SYSTEM SERVICES
STORAGE DEVICES & OPERATING SYSTEM SERVICES
 
OS , Its History and Types
OS , Its History and TypesOS , Its History and Types
OS , Its History and Types
 
MULTIPLEXERS
MULTIPLEXERSMULTIPLEXERS
MULTIPLEXERS
 
UDP and TCP Protocol & Encrytion and its algorithm
UDP and TCP Protocol & Encrytion and its algorithmUDP and TCP Protocol & Encrytion and its algorithm
UDP and TCP Protocol & Encrytion and its algorithm
 
Presentation tree traversal
Presentation tree traversalPresentation tree traversal
Presentation tree traversal
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Recently uploaded (20)

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 

Prims & kruskal algorithms

  • 1. 1
  • 2. Minimum Spanning TREE Kruskal’s Algorithm Prim’s Algorithm
  • 3. What is a Spanning Tree  In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edges)  Use the fewest number of edges possible. 3
  • 4. 4 Spanning tree but not MST Spanning trees
  • 5. What is a Spanning Tree  In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edge)  Use the fewest number of edges possible. 5
  • 6. What is a Spanning Tree  In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by arcs (edge)  Use the fewest number of edges possible.  If a loop can be found, then one of the edges is not necessary 6
  • 7. What is a Spanning Tree  In order to find a spanning tree, we must make sure that every node (vertex) is some how connected to the rest of the nodes by an arc (edge)  Use the fewest number of edges possible.  You may think now, How many edges you have in a spanning tree given N vertices 7
  • 8. Minimum Spanning Tree  The subset of edges E that connects all vertices V in the graph, and has minimum total weight. a b c d 1 6 54 2 8
  • 9. Minimum Spanning Tree  The subset of edges E that connects all vertices V in the graph, and has minimum total weight. a b c d 1 6 54 2 MST = { (a,b),(a,d),(d,c) } 9
  • 10. 10 APPLICATIONS OF MST Communication problem. Circuit diagram. T.V. cables.
  • 11. Kruskal’s Algorithm THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
  • 12. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2  Kruskal's algorithm utilizes Disjoint Sets (DS). 12
  • 13. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2  Kruskal's algorithm utilizes Disjoint Sets (DS).  DS has two Operations Find and Union. 13
  • 14. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2  Find: takes an item and pretend that this is a DS that item belongs to  Union: will merge two DS of its input items in to one DS 14
  • 15. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  A: is used to store all edges of the minimum spanning tree Initially A is empty Eventually return A 15
  • 16. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  for each vertex v of the graph, make a DS with v  Each vertex will be used to create a single vertex Disjoint Set 16
  • 17. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  Sort all edges by weight increasingly  the edge of smallest weight will be in front and the one with largest weight will be at the end 17
  • 18. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  1st we will take the edge with the smallest weight  C and f don’t belong to the same DS 18
  • 19. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),}  c and f will be merged in a one DS  (c,f) is pushed to A 19
  • 20. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),} 20
  • 21. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),} 21
  • 22. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),}  c and d will be merged in a one DS 22
  • 23. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),}  c and d will be merged in a one DS 23
  • 24. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),(c,d),} 24
  • 25. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),(c,d),}  e and f belong to the same DS  DO NOT MERGE !! 25
  • 26. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),(c,d),(a,b)} 26
  • 27. Kruskal’s Algorithm Kruskal(V,E) A = ø foreach v V: Make-disjoint-set(v) Sort E by weight increasingly foreach (v1,v2) E: if Find(v1) ≠ Find(v2): A = A U {(v1,v2)} Union (v1,v2) Return A a 2 c b d f 4 6 3 4 15 e 2 A = { (c,f),(a,f),(d,e),(c,d),(a,b)}  Now we have a Spanning Tree that connects all vertices which have the minimum total weight 27
  • 30. Prim’s Algorithm THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
  • 31. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  A: is used to store all edges of the minimum spanning tree Initially A is empty Eventually return A 31
  • 32. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  for each vertex v of the graph, Each vertex will have two attributes KEY and PARENT  KEY and PARENT initialized to ∞ and null Respectively 32
  • 33. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A a 2 c b d f 4 6 3 4 15 e 2 A = { }  for each vertex v of the graph, Each vertex will have two attributes KEY and PARENT  KEY and PARENT initialized to ∞ and null Respectively ∞,null ∞,null ∞,null ∞,null ∞,null ∞,null 33
  • 34. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Randomly choose a vertex as a ROOT r and set its KEY value to Zero ∞,null ∞,null ∞,null ∞,null 0,null ∞,null 34
  • 35. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Create Q list which contains all vertices in the graph ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { a,b,c,d,e,f } 35
  • 36. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Find Q with the smallest (Minimum) KEY  In this case, KEY[a] = 0 (Minimum KEY) ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { a,b,c,d,e,f } 36
  • 37. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Remove a from Q ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { b,c,d,e,f } 37
  • 38. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  PARENT [a] = null !!  Skip the if statement ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { b,c,d,e,f } 38
  • 39. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Update attributes for all vertices Adjacent to a which are b and f  Starting with f , 2 < ∞ ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { b,c,d,e,f } 39
  • 40. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { }  Update attributes for all vertices Adjacent to a which are b and f  Starting with f , 2 < ∞  PARENT [ f ] = a and KEY [ f ] = 2 ∞,null ∞,null ∞,null 2, a 0,null 4, a Q = { b,c,d,e,f } 40
  • 41. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f) }  Now, Enter the Second Loop  a ! Q  u = min(Q) = f , Remove f from Q  PARENT [ f ] ! = null, push (a,f) to A ∞,null ∞,null ∞,null 2, a 0,null 4, a Q = { b,c,d,e } 41
  • 42. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f) }  Update attributes for all vertices Adjacent to f which are c and d where weight < KEY 1, f ∞,null 4, f 2, a 0,null 4, a Q = { b,c,d,e } 42
  • 43. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f) }  Now, Enter the NEXT Loop  u = min(Q) = c , Remove c from Q  PARENT [ c ] ! = null, push (c,f) to A 1, f ∞,null 4, f 2, a 0,null 4, a Q = { b,d,e } 43
  • 44. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f) }  Update attributes for all vertices Adjacent to c where weight < KEY 1, f 3, c 4, f 2, a 0,null 4, a Q = { b,d,e } 44
  • 45. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d) }  Enter the NEXT Loop  u = min(Q) = d , Remove d from Q  PARENT [ d ] ! = null, push (c,d) to A 1, f 3, c 4, f 2, a 0,null 4, a Q = { b,e } 45
  • 46. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d) }  Update attributes for all vertices Adjacent to d where weight < KEY 1, f 3, c 2, d 2, a 0,null 4, a Q = { b,e } 46
  • 47. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e) }  Enter the NEXT Loop  u = min(Q) = e , Remove e from Q  PARENT [ e] ! = null, push (d,e) to A 1, f 3, c 2, d 2, a 0,null 4, a Q = { b } 47
  • 48. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e) }  No Updates for attributes because d and f ! Q 1, f 3, c 2, d 2, a 0,null 4, a Q = { b } 48
  • 49. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e), (a,b) }  Enter the NEXT Loop  u = b , Remove b from Q  PARENT [ b] ! = null, push (a,b) to A 1, f 3, c 2, d 2, a 0,null 4, a Q = { } 49
  • 50. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e), (a,b) }  No Updates for attributes because all neighbors of b doesn’t exist in Q 1, f 3, c 2, d 2, a 0,null 4, a Q = { } 50
  • 51. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e), (a,b) }  Enter the NEXT Loop, Q = ø  Exit loop  Return A Q = { } 51
  • 52. Prim’s Algorithm A = ø foreach v V: KEY[v] = ∞ PARENT[v] = null KEY[r] = 0 Q = V While Q != ø: u = min (Q) by KEY value Q = Q – u if PARENT(u) != null: A = A U (u, PARENT(u)) foreach v Adj(u): if v Q and w(u,v) < KEY[v]: PARENT[v] = u KEY[v] = w Return A 3 a 2 c b d f 4 6 4 15 e 2 A = { (a,f), (c,f), (c,d), (d,e), (a,b) }  Now we have a Spanning Tree that connects all vertices which have the minimum total weight Q = { } 52
  • 54. Prim’s Algorithm  The time complexity of the algorithm is computed as follows:  Step 1 costs q(n) time.  The for loop in Step 2 requires Ø(n) time.  The time taken by steps 3 - 6 is O(n).  The time taken by Step 10 to search for a vertex y closest to X is Ø(n) per iteration. This is because the algorithm inspects each entry in the vector representing the set Y. Since it is executed n-1 times, the overall time required by Step 10 is Ø(n2).  Steps 11,12 and 13 cost Ø(1) time per iteration for a total of q(n) time.  The for loop in Step 14 is executed 2m times, where m=|E|. This is because each edge (y, w) is inspected twice: once when y is moved to X and the other when w is moved to X.  Hence, the overall time required by step 14 is Ø(m).  The test in Step 15 is executed exactly m times, and Steps 16 and 17 are executed at most m times.  Thus, Steps 14 to 19 cost q(m) time.  It follows that the time complexity of the algorithm is  Ø(m + n2) = Ø (n2) 54