0% found this document useful (0 votes)
28 views8 pages

Prims and Kruskal

Prim's Algorithm is a greedy method used to find the minimum spanning tree (MST) of a connected graph by starting from a vertex and adding the smallest weight edges without forming cycles. Kruskal's Algorithm, another greedy approach, constructs the MST by sorting edges by weight and adding them while avoiding cycles. Both algorithms have specific applications in network design and have different time complexities based on the data structures used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Prims and Kruskal

Prim's Algorithm is a greedy method used to find the minimum spanning tree (MST) of a connected graph by starting from a vertex and adding the smallest weight edges without forming cycles. Kruskal's Algorithm, another greedy approach, constructs the MST by sorting edges by weight and adding them while avoiding cycles. Both algorithms have specific applications in network design and have different time complexities based on the data structures used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Prim's Algorithm

Spanning tree - A spanning tree is the subgraph of an undirected connected


graph.

Minimum Spanning tree - Minimum spanning tree can be defined as the


spanning tree in which the sum of the weights of the edge is minimum. The weight
of the spanning tree is the sum of the weights given to the edges of the spanning
tree.

Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning
tree from a graph. Prim's algorithm finds the subset of edges that includes every
vertex of the graph such that the sum of the weights of the edges can be
minimized.

Prim's algorithm starts with the single node and explores all the adjacent nodes with
all the connecting edges at every step. The edges with the minimal weights causing
no cycles in the graph got selected.

How does the prim's algorithm work?


Prim's algorithm is a greedy algorithm that starts from one vertex and continue to
add the edges with the smallest weight until the goal is reached. The steps to
implement the prim's algorithm are given as follows -

o First, we have to initialize an MST with the randomly chosen vertex.


o Now, we have to find all the edges that connect the tree in the above step
with the new vertices. From the edges found, select the minimum edge and
add it to the tree.
o Repeat step 2 until the minimum spanning tree is formed.

The applications of prim's algorithm are -

o Prim's algorithm can be used in network designing.


o It can be used to make network cycles.
o It can also be used to lay down electrical wiring cables.

Example of prim's algorithm


Now, let's see the working of prim's algorithm using an example. It will be easier to
understand the prim's algorithm using an example.

Suppose, a weighted graph is -


Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.

Step 2 - Now, we have to choose and add the shortest edge from vertex B. There
are two edges from vertex B that are B to C with weight 10 and edge B to D with
weight 4. Among the edges, the edge BD has the minimum weight. So, add it to the
MST.

Step 3 - Now, again, choose the edge with the minimum weight among all the
other edges. 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. So, select the edge DE and add it to the
MST.

Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.

So, the graph produced in step 5 is the minimum spanning tree of the given graph.
The cost of the MST is given below -

Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has mini
mum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT

Complexity of Prim's algorithm


Now, let's see the time complexity of Prim's algorithm. The running time of the
prim's algorithm depends upon using the data structure for the graph and the
ordering of edges. Below table shows some choices –

o Time Complexity
Data structure used for the minimum edge Time Complexity
weight

Adjacency matrix, linear searching O(|V|2)

Adjacency list and binary heap O(|E| log |V|)

Adjacency list and Fibonacci heap O(|E|+ |V| log |


V|)

Prim's algorithm can be simply implemented by using the adjacency matrix or


adjacency list graph representation, and to add the edge with the minimum weight
requires the linearly searching of an array of weights. It requires O(|V| 2) running
time. It can be improved further by using the implementation of heap to find the
minimum weight edges in the inner loop of the algorithm.

The time complexity of the prim's algorithm is O(E logV) or O(V logV), where E is the
no. of edges, and V is the no. of vertices.

Kruskal's Algorithm
Spanning tree - A spanning tree is the subgraph of an undirected
connected graph.

Minimum Spanning tree - Minimum spanning tree can be defined


as the spanning tree in which the sum of the weights of the edge is
minimum. The weight of the spanning tree is the sum of the weights
given to the edges of the spanning tree.

Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of the algorithm is to find the subset of edges by using which we can
traverse every vertex of the graph. It follows the greedy approach that finds an optimum solution
at every stage instead of focusing on a global optimum.
How does Kruskal's algorithm work?
In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges
until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows -

o First, sort all the edges from low weight to high.


o Now, take the edge with the lowest weight and add it to the
spanning tree. If the edge to be added creates a cycle, then
reject the edge.
o Continue to add the edges until we reach all vertices, and a
minimum spanning tree is created.

The applications of Kruskal's algorithm are -

o Kruskal's algorithm can be used to layout electrical wiring


among cities.
o It can be used to lay down LAN connections.

Example of Kruskal's algorithm


Now, let's see the working of Kruskal's algorithm using an example. It will be easier to
understand Kruskal's algorithm using an example.

Suppose a weighted graph is -

The weight of the edges of the above graph is given in the below table -

Edge A A A A B C D
B C D E C D E

Weig 1 7 10 5 3 4 2
ht

Now, sort the edges given above in the ascending order of their weights.

Edge A D B C A A A
B E C D E C D

Weig 1 2 3 4 5 7 10
ht

Now, let's start constructing the minimum spanning tree.

Step 1 - First, add the edge AB with weight 1 to the MST.

Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.

Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is
not forming the cycle.

Step 5 - After that, pick the edge AE with weight 5. Including this
edge will create the cycle, so discard it.

Step 6 - Pick the edge AC with weight 7. Including this edge will
create the cycle, so discard it.

Step 7 - Pick the edge AD with weight 10. Including this edge will
also create the cycle, so discard it.

So, the final minimum spanning tree obtained from the given
weighted graph by using Kruskal's algorithm is -

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.


Now, the number of edges in the above tree equals the number of
vertices minus 1. So, the algorithm stops here.

Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the grap
h is a separate tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spa
nning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, t
hen add it to the forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END

Complexity of Kruskal's algorithm


Now, let's see the time complexity of Kruskal's algorithm.

o Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V
logV), where E is the no. of edges, and V is the no. of vertices.

You might also like