Prim's Algorithm For MST - 0000
Prim's Algorithm For MST - 0000
CSE 2264
Introduction to Prim's Algorithm
Prim's algorithm is a minimum spanning
tree algorithm that takes a graph as input
and finds the subset of the edges of that
graph which
1. form a tree that includes every vertex
2. has the minimum sum of weights among all
the trees that can be formed from the
graph
How Prim's algorithm works
It falls under a class of algorithms called greedy
algorithms that find the local optimum in the hopes of
finding a global optimum.
We start from one vertex and keep adding edges with
the lowest weight until we reach our goal.
The steps for implementing Prim's algorithm are as
follows:
1. Initialize the minimum spanning tree with a vertex
chosen at random.
2. Find all the edges that connect the tree to new
vertices, find the minimum and add it to the tree
3. Keep repeating step 2 until we get a minimum
spanning tree
Example of Prim's algorithm
Consider the following graph and draw a
MST using prim’s algorithm.
Example of Prim's algorithm
As there is no parallel edge and loop, you
can start the procedure from choosing a
starting vertex.
Choose a vertex which is C.
Example of Prim's algorithm
Choose the shortest edge from this vertex
and add it.
All the edges connecting the incomplete
MST and other vertices are the edges {C,
A}, {C, B}, {C, D}, {C, F} and {C, E}. Among
these four the edge with minimum weight is
{C, B}. So include the edge and vertex B in
the MST.
Example of Prim's algorithm
Choose the nearest vertex not yet in the
solution.
The edges connecting the incomplete MST to
other vertices are {C, A}, {C, D},{C, F}, {C, E} and
B, A}. Among these edges the minimum weight
is 2 which is of the edges {C, E}. Let us here
include the edge {C, E} and the vertex E in the
MST.
Example of Prim's algorithm
Choose the nearest edge not yet in the
solution, if there are multiple choices,
choose one at random
Example of Prim's algorithm
Repeat until you have a spanning tree
Prim's vs Kruskal's Algorithm
Kruskal's algorithm is another popular
minimum spanning tree algorithm that uses
a different logic to find the MST of a graph.
Instead of starting from a vertex, Kruskal's
algorithm sorts all the edges from low
weight to high and keeps adding the
lowest edges, ignoring those edges that
create a cycle.
Prim's Algorithm Application
Laying cables of electrical wiring
In network designed
To make protocols in network cycles