0% found this document useful (0 votes)
9 views

10a MST Prim

Uploaded by

ssmukherjee2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

10a MST Prim

Uploaded by

ssmukherjee2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MST - Prim

Minimum Spanning Tree


• A Tree is a graph with no cycles
• In a tree, there is only one path from one
node to any other
• Spanning Tree for a graph is a tree that
connects all vertices
• A graph can have many Spanning Trees
• Minimum Spanning Tree (MST) is the
spanning tree with minimum edge weight
MST Prim - Approach
• Start at any node of Graph, G=(V, E), called
Source Node
• Check all adjacent nodes of Source Node
• Select edge with minimum weight, add to MST
• Check others nodes adjacent to nodes in MST
• Select edge with minimum weight and add to
MST
• Continue till all nodes in G are covered
Prim MST Algorithm
Input: Graph G, (AdjMat[x, y]=INFINITY, if edge(x,y) not exist); Source S
Associated Structure: Visited [1..V], Distance [1..V]
Output: MST [1..V] for G
Procedure MST-Prim (G, S)
Begin
Repeat for all vertices v in V
Set Distance[v] := INFINITY
Set Visited [v] := FALSE
Set MST [v] := NULL
End Repeat
Set Distance[S] := 0
Repeat while all vertices not Visited
Set v := get_minimum()//returns vertex with min distance
Set Visited[v] := TRUE
Repeat for all adjacent vertices t of v
If Visited[t] is FALSE and Distance[t] > wt[v,t]
Set Distance[t] := wt[v,t]
Set MST[t] := (v,t)
End If
End Repeat
End Repeat
End
4
Example

4 A 7

3
B 5 6 C
D
2
9
8 12

E F
14
Example - 1
A
4 7
3
B 5 6
D C

2 8 9
12
E 14 F

A B C D E F
999 999 999 999 999 999
Distance Array
-1 -1 -1 -1 -1 -1 Visited Array
MST Array
Example - 2
A Source Vertex for MST, A
4 7
3 Initialize Distance of A to 0
B 5 6
D C

2 8 9
12
E 14 F

A B C D E F
0 999 999 999 999 999
Distance Array
-1 -1 -1 -1 -1 -1 Visited Array
MST Array
Example - 3
A Source Vertex for MST, A
4 7
3 Mark A as Visited
B 5 6 Adjacent Nodes of A: B, C, D
D C

2 Prev. Distances of B, C & D : INF


8 9
12
Update Distances of B, C, D
E 14 F
Update MST for B, C, D

A B C D E F
0 4 7 3 999 999
Distance Array
1 -1 -1 -1 -1 -1 Visited Array
AB AC AD MST Array
Example - 4
A Source Vertex for MST, A
4 7
3 Node with min. distance: D
B 5 6 Mark D as Visited
D C

2 Check Distances of adjacent


8 9
12 nodes via D
E 14 F

A B C D E F
0 4 7 3 999 999
Distance Array
1 -1 -1 1 -1 -1 Visited Array
AB AC AD MST Array
Example - 5
A Current Vertex: D
4 7
3 Distance of B from D: 5
B 5 6 Previous Distance of B: 4
D C

2 No change in Distance of B, no
8 9
12 change in MST
E 14 F

A B C D E F
0 4 7 3 999 999
Distance Array
1 -1 -1 1 -1 -1 Visited Array
AB AC AD MST Array
Example - 6
A Current Vertex: D
4 7
3 Distance of C from D: 6
B 5 6 Previous Distance of C: 7
D C

2 Update Distance & MST for C


8 9
12
E 14 F

A B C D E F
0 4 6 3 999 999
Distance Array
1 -1 -1 1 -1 -1 Visited Array
AB DC AD MST Array
Example - 7
A Current Vertex: D
4 7
3 Prev. Distance of E & F = INF
B 5 6 Update Distance of E & F from D
D C

2 Update MST
8 9
12
E 14 F

A B C D E F
0 4 6 3 8 12
Distance Array
1 -1 -1 1 -1 -1 Visited Array
AB DC AD DE DF MST Array
Example - 8
A Current Vertex: B
4 7
3 Node with min. distance : B
B 5 6 Mark B as Visited
D C

2 Check distances of adjacent


8 9
12 nodes from B
E 14 F

A B C D E F
0 4 6 3 8 12
Distance Array
1 1 -1 1 -1 -1 Visited Array
AB DC AD DE DF MST Array
Example - 9
A Current Vertex: B
4 7
3 Prev. Distance of E : 8
B 5 6 Distance of E from B : 2
D C

2 Update Distance & MST for E


8 9
12
E 14 F

A B C D E F
0 4 6 3 2 12
Distance Array
1 1 -1 1 -1 -1 Visited Array
AB DC AD BE DF MST Array
Example - 10
A Current Vertex: E
4 7
3 Node with min. Distance : E
B 5 6 Mark E as Visited
D C

2 Check distances of adjacent


8 9
12 nodes from E
E 14 F Distance of F not updated

A B C D E F
0 4 6 3 2 12
Distance Array
1 1 -1 1 1 -1 Visited Array
AB DC AD BE DF MST Array
Example - 11
A Current Vertex: C
4 7
3 Node with min. Distance : C
B 5 6 Mark C as Visited
D C

2 Check distances of adjacent


8 9
12 nodes from C
E 14 F Distance of F updated

A B C D E F
0 4 6 3 2 9
Distance Array
1 1 1 1 1 -1 Visited Array
AB DC AD BE CF MST Array
Example - 12
A Current vertex: F
4 7
3 F last node to be processed
B 5 6 All adjacent nodes already done
D C

2 No updates
8 9
12
E 14 F

A B C D E F
0 4 6 3 2 9
Distance Array
1 1 1 1 1 1 Visited Array
AB DC AD BE CF MST Array
Example - 13
A Processing Completed
4 7
3 MST stored in MST array
B 5 6
D C

2 8 9
12
E 14 F

A B C D E F
0 4 6 3 2 9
Distance Array
1 1 1 1 1 1 Visited Array
AB DC AD BE CF MST Array
Analysis of MST Algorithms
• Kruskal Algorithm:
– Sorting of Edges, O(E log E)
– Processing of Edges, O(E)
– Complete procedure: O(E log E)
• Prim Algorithm:
– Depends on Procedure get_minimum
– If implemented using arrays, O(V2)
– Using Priority Queues, O(E log V)
The End

You might also like