SlideShare a Scribd company logo
Minimum Spanning TREE
Kruskal’s Algorithm
Prim’s Algorithm
BY: IBRAHIM S. ALFAYOUMI
WWW.IBRAHIM.PS
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.
2
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.
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 (edge)
 Use the fewest number of edges possible.
 If a loop can be found,
then one of the edges is not necessary
4
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
5
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
6
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) }
7
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).
9
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.
10
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
11
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
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
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
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
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
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 = { }
 1st we will take the edge with the smallest weight
 C and f don’t belong to the same DS
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 = { (c,f),}
 c and f will be merged in a one DS
 (c,f) is pushed to A
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 = { (c,f),(a,f),}
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 = { (c,f),(a,f),(d,e),}
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),(a,f),(d,e),}
 c and d will be merged in a one DS
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),(d,e),}
 c and d will be merged in a one DS
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),(c,d),}
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,d),}
 e and f belong to the same DS
 DO NOT MERGE !!
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,d),(a,b)}
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),(a,b)}
 Now we have a Spanning Tree that connects all
vertices which have the minimum total weight
24
Kruskal’s Algorithm 25
Kruskal’s Algorithm 26
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
28
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
29
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
30
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
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
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 }
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
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 }
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 = { }
 Remove a from Q
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { b,c,d,e,f }
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 = { }
 PARENT [a] = null !!
 Skip the if statement
∞,null
∞,null
∞,null
∞,null
0,null
∞,null
Q = { 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 = { }
 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 }
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 = { }
 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 }
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 = { (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 }
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 = { (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 }
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 = { (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 }
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), (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 }
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), (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 }
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), (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 }
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), (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 }
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), (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 }
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), (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 = { }
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), (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 = { }
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), (a,b) }
 Enter the NEXT Loop, Q = ø
 Exit loop
 Return A
Q = { }
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) }
 Now we have a Spanning Tree that connects all
vertices which have the minimum total weight
Q = { }
49
Prim’s Algorithm 50
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)
51
THANKS

More Related Content

What's hot

minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
sachin varun
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and PolynomialsASPAK2014
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theorem
JamesMa54
 
Solving the energy problem of helium final report
Solving the energy problem of helium final reportSolving the energy problem of helium final report
Solving the energy problem of helium final report
JamesMa54
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringASPAK2014
 
Iterative Compression
Iterative CompressionIterative Compression
Iterative CompressionASPAK2014
 
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...
政謙 陳
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
Narendra Singh Patel
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
Kiran K
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
JamesMa54
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
Dhaval Shukla
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Traian Rebedea
 
Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
Bidimensionality
BidimensionalityBidimensionality
BidimensionalityASPAK2014
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
Kiran K
 
Prim algorithm
Prim algorithmPrim algorithm
Prim algorithm
University of Potsdam
 
Treewidth and Applications
Treewidth and ApplicationsTreewidth and Applications
Treewidth and ApplicationsASPAK2014
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth process
gidc engineering college
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
Kiran K
 

What's hot (20)

minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and Polynomials
 
Solovay Kitaev theorem
Solovay Kitaev theoremSolovay Kitaev theorem
Solovay Kitaev theorem
 
Solving the energy problem of helium final report
Solving the energy problem of helium final reportSolving the energy problem of helium final report
Solving the energy problem of helium final report
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
 
Iterative Compression
Iterative CompressionIterative Compression
Iterative Compression
 
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...
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
 
Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Bidimensionality
BidimensionalityBidimensionality
Bidimensionality
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Prim algorithm
Prim algorithmPrim algorithm
Prim algorithm
 
Treewidth and Applications
Treewidth and ApplicationsTreewidth and Applications
Treewidth and Applications
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth process
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 

Viewers also liked

Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Treezhaokatherine
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
Alona Salva
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
Gaurav Kolekar
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
Ifad Rahman
 
Kruskal’s Algorithm
Kruskal’s AlgorithmKruskal’s Algorithm
Kruskal’s Algorithm
Syed Maniruzzaman Pabel
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraguest1f4fb3
 
Normalization
NormalizationNormalization
Normalization
murdhani heena
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 
Kruskal’s algorithm
Kruskal’s algorithmKruskal’s algorithm
Kruskal’s algorithm
Abdul Moiz Lakhani
 
Pda
PdaPda
Push down automata
Push down automataPush down automata
Push down automataSomya Bagai
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Tanmay Baranwal
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 

Viewers also liked (20)

Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Kruskal’s Algorithm
Kruskal’s AlgorithmKruskal’s Algorithm
Kruskal’s Algorithm
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitra
 
Normalization
NormalizationNormalization
Normalization
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
Kruskal’s algorithm
Kruskal’s algorithmKruskal’s algorithm
Kruskal’s algorithm
 
Pda
PdaPda
Pda
 
Normalization
NormalizationNormalization
Normalization
 
Kruskal algorithms
Kruskal algorithmsKruskal algorithms
Kruskal algorithms
 
Push down automata
Push down automataPush down automata
Push down automata
 
0 1 knapsack problem
0 1 knapsack problem0 1 knapsack problem
0 1 knapsack problem
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 

Similar to Minimum spanning tree algorithms by ibrahim_alfayoumi

lecture 22
lecture 22lecture 22
lecture 22sajinsc
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Capitulo 4, 7ma edición
Capitulo 4, 7ma ediciónCapitulo 4, 7ma edición
Capitulo 4, 7ma edición
Sohar Carr
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
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
Steven Duplij (Stepan Douplii)
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Vcla - Inner Products
Vcla - Inner ProductsVcla - Inner Products
Vcla - Inner Products
Preetshah1212
 
Ch4
Ch4Ch4
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Vivekananda Samiti
 
Chpt 2-sets v.3
Chpt 2-sets v.3Chpt 2-sets v.3
Chpt 2-sets v.3
ShahidAkbar22
 
ANALISIS VECTORIAL.pdf
ANALISIS VECTORIAL.pdfANALISIS VECTORIAL.pdf
ANALISIS VECTORIAL.pdf
JHONFREDYGONZALEZ4
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
Sania Nisar
 

Similar to Minimum spanning tree algorithms by ibrahim_alfayoumi (20)

lecture 22
lecture 22lecture 22
lecture 22
 
lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Least Squares
Least SquaresLeast Squares
Least Squares
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Capitulo 4, 7ma edición
Capitulo 4, 7ma ediciónCapitulo 4, 7ma edición
Capitulo 4, 7ma edición
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Least Squares
Least SquaresLeast Squares
Least Squares
 
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
 
lecture 21
lecture 21lecture 21
lecture 21
 
Vcla - Inner Products
Vcla - Inner ProductsVcla - Inner Products
Vcla - Inner Products
 
Ch4
Ch4Ch4
Ch4
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
 
Chpt 2-sets v.3
Chpt 2-sets v.3Chpt 2-sets v.3
Chpt 2-sets v.3
 
ANALISIS VECTORIAL.pdf
ANALISIS VECTORIAL.pdfANALISIS VECTORIAL.pdf
ANALISIS VECTORIAL.pdf
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

Minimum spanning tree algorithms by ibrahim_alfayoumi

  • 1. Minimum Spanning TREE Kruskal’s Algorithm Prim’s Algorithm BY: IBRAHIM S. ALFAYOUMI WWW.IBRAHIM.PS
  • 2. 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. 2
  • 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 (edge)  Use the fewest number of edges possible. 3
  • 4. 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 4
  • 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 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 5
  • 6. 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 6
  • 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 MST = { (a,b),(a,d),(d,c) } 7
  • 8. Kruskal’s Algorithm THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
  • 9. 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). 9
  • 10. 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. 10
  • 11. 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 11
  • 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 A = { }  A: is used to store all edges of the minimum spanning tree Initially A is empty Eventually return A 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 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 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 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 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 = { }  1st we will take the edge with the smallest weight  C and f don’t belong to the same DS 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 = { (c,f),}  c and f will be merged in a one DS  (c,f) is pushed to A 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 = { (c,f),(a,f),} 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 = { (c,f),(a,f),(d,e),} 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),(a,f),(d,e),}  c and d will be merged in a one DS 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),(d,e),}  c and d will be merged in a one DS 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),(c,d),} 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,d),}  e and f belong to the same DS  DO NOT MERGE !! 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,d),(a,b)} 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),(a,b)}  Now we have a Spanning Tree that connects all vertices which have the minimum total weight 24
  • 27. Prim’s Algorithm THE PROBLEM: GIVING A GRAPH WITH WEIGHTED EDGES, FIND ITS MINIMUM SPANNING TREE
  • 28. 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 28
  • 29. 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 29
  • 30. 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 30
  • 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 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 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 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 } 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 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 } 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 = { }  Remove a from Q ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { b,c,d,e,f } 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 = { }  PARENT [a] = null !!  Skip the if statement ∞,null ∞,null ∞,null ∞,null 0,null ∞,null Q = { 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 = { }  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 } 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 = { }  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 } 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 = { (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 } 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 = { (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 } 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 = { (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 } 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), (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 } 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), (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 } 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), (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 } 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), (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 } 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), (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 } 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), (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 = { } 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), (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 = { } 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), (a,b) }  Enter the NEXT Loop, Q = ø  Exit loop  Return A Q = { } 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) }  Now we have a Spanning Tree that connects all vertices which have the minimum total weight Q = { } 49
  • 51. 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) 51