1
prim's algorithm
Prims Algorithm
2
Members:
Angela Tare 20/03483.
Zachary Karanja 20/02830.
Nicasio Tabb 20/03233.
Brenda Wangeci 20/03198.
Fiona Njuguna 20/03593.
Benard Kibet 20/03021.
Tabitha Njeri 20/03062.
Ednah Mutiso 19/03866.
Esther Akinyi 20/03553.
Francis Otieno 20/03531
prim's algorithm
3
Introduction to Prim’s Algorithm
It is an algorithm for determining the minimal spanning tree in a connected graph.
It is a famous greedy algorithm.
Prim’s algorithm is used for finding the minimum Spanning Tree of a given graph.
To apply it the given graph must be weighted, connected and unidirected.
It finds the subnet of edges that includes every vertex of the graph such that the sum of
the weights of the edges can be minimized.
Only the edges with the minimal weights causing no cycle are selected
prim's algorithm
4
Minimum Spanning Tree
Also called minimum weight spanning tree.
It is a set of edges in an undirected weighted graph that connects all the vertices with no
cycles and minimum total edge weight.
It is a spanning tree which has minimal total weight.
prim's algorithm
5
Steps to be followed when selecting
1. Select any starting vertex.
2. Look at all edges connected to the vertex and choose the one with the lowest weight and
add it to the tree.
3. Add the selected edge and the vertex to the minimum spanning tree.
4. Repeat process 2&3 until there are fringe vertices.
prim's algorithm
6
Example one
Construct a minimum spanning tree of the graph given in the following figure by using
prim’s algorithm.
prim's algorithm
7
Solution
Step 1 : Choose a starting vertex B.
Step 2: Add the vertices that are adjacent to A. the edges that connecting the vertices are shown by
dotted lines.
Step 3: Choose the edge with the minimum weight among all. i.e. BD and add it to MST. Add the
adjacent vertices of D i.e. C and E.
Step 3: Choose the edge with the minimum weight among all. In this case, the edges DE and CD
are such edges. Add them to MST and explore the adjacent of C i.e. E and A.
Step 4: Choose the edge with the minimum weight i.e. CA. We can't choose CE as it would cause
cycle in the graph.
The graph produces in the step 4 is the minimum spanning tree of the graph shown in the above
figure. The cost of MST will be calculated as; cost(MST) = 4 + 2 + 1 + 3 = 10 units.
prim's algorithm
8
Solution.
prim's algorithm
9
Example two
Construct a minimum spanning tree of the graph given in the following figure by using
prim’s algorithm
prim's algorithm
10
Solution
Total Cost = 10 + 25 + 22 + 12 + 16 + 14 = 99
prim's algorithm
11
Step 1 : Choose a starting vertex 0.
Step 2: Add the vertices that are adjacent to 5.
Step 3: Choose the edge with the minimum weight among all. I.e 4.
Step 3: Choose the edge with the minimum weight among all. i.e 3.
Step 4: Choose the edge with the minimum weight. i.e 2
Step 5: Choose the edge with the minimum weight. i.e 1
Step 6: Choose the edge with the minimum weight. i.e 6
The graph produces in the step 4 is the minimum spanning tree of the graph shown in the above
figure. The cost of MST will be calculated as; cost(MST) = 10 + 25 + 22 + 12 + 16 + 14 = 99units.
prim's algorithm
12
Application of Prim’s Algorithm
Network for roads and rail tracks connecting all the cities.
Irrigation channels and placing microwave towers.
Designing a fibre optic grid or Ics.
Travelling salesman problem.
Cluster analysis.
Pathfinding algorithm used in AI.
Game development.
Cognitive science.
prim's algorithm