Difference between prim and Kruskal algorithm

By Priyanshu Vaish|Updated : July 28th, 2022

Difference between prim and Kruskal algorithm: Prim and Kruskal algorithms follow the greedy technique. When there is an undirected and connected graph(G), a spanning tree is a tree that spans and is a subgraph of G. A minimal spanning tree is a spanning tree with the lowest cost among all spanning trees. It is mainly used in network design.

The primary difference between Prim and Kruskal algorithm is that the Prims method creates the minimal spanning tree from the root vertex. In contrast, the Kruskal algorithm generates the lowest spanning tree from the least weighted edge.

Table of Content

What is the Kruskal algorithm?

Before Knowing the difference between prim and Kruskal algorithm, let us know about the Kruskal algorithm. The Kruskal algorithm is a technique introduced in discrete mathematics graph theory. A connected weighted graph is used to find the shortest path between two points. This algorithm turns a given graph into a forest, with each node treated as a distinct tree. These trees can only be connected if the edge connecting them has a low value and does not produce a cycle in the MST structure.

Steps for Kruskal algorithm

  1. Choose the network's shortest edge.
  2. Choose the next shortest edge that does not form a cycle.
  3. Repeat step 2 until all vertices are connected.
  4. Kruskal starts with a forest and proceeds to a tree.

What is the prim algorithm?

Before Knowing the difference between prim and Kruskal algorithm, let us know about the prim algorithm. The Prim algorithm is a greedy method that seeks the shortest path across a weighted undirected network. This implies that it identifies a subset of the edges that form a tree that contains every vertex and where the total weight of all the edges in the tree is minimized.

Steps for prim algorithm

Step 1: Choose any vertex

Step 2: Choose the smallest weight edge that connects to that vertex. 

Step 3: Choose the smallest weight edge that connects to the selected vertex. 

Step 4: Repeat step 3 until all vertices are connected.

Step 5: Prim always remains as a tree.  

Difference Between Prim And Kruskal Algorithm

The table shows the difference between prim and Kruskal algorithms. The difference between prim and Kruskal algorithm is based on the selection of the edge or node, performance, etc.

Prim algorithm

Kruskal algorithm

The Prim algorithm builds the shortest spanning tree starting with the root node.

The Kruskal method constructs the smallest spanning tree starting with the least weighted edge.

The Prim algorithm chooses the shortest edge connecting the root node.

The Kruskal algorithm chooses the next shortest edge.

Prim's algorithm begins with a node.

The Kruskal algorithm starts with an edge.

The prim algorithm can function on connected graphs. 

Kruskal algorithm can function on connected or disconnected graphs. 

The prim algorithm explores one node many times to determine the shortest distance.

The Kruskal algorithm explores one node only once to determine the shortest distance.

In dense graphs, the Prim algorithm performs better.

In sparse graphs, the Kruskal method performs better.

Comments

write a comment

FAQs

  • The main difference between the Prim and Kruskal algorithms is that the Kruskal approach generates the shortest spanning tree from the least weighted edge. In contrast, the Prims approach generates the shortest spanning tree from the root vertex.

  • The difference between prim and Kruskal algorithm regarding the performance is the time complexity of prims algorithm is better than the Kruskal algorithm. The time complexity of the Kruskal algorithm is O(E log E) or O(E log V), whereas the time complexity of the Prim Algorithm is O ( ( V + E ) l o g V ), where E is a number of edges and V is a number of vertices.

  • The difference between prim and Kruskal algorithm regarding the graph is that prim algorithm is helpful when dealing with dense graphs with lots of edges, whereas the Kruskal algorithm is helpful with a sparse graph.

  • The difference between prim and Kruskal algorithm regarding the answer is the same as both the algorithms have the same goal: to construct the graph's shortest spanning tree.

  • The Prims and Kruskal Algorithms are used to determine the minimum spanning trees. Now, the applications of the Kruskal and Prims Algorithms are essentially MST applications. Hence, both techniques are referred to as 'greedy' algorithms.

Follow us for latest updates