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
- Choose the network's shortest edge.
- Choose the next shortest edge that does not form a cycle.
- Repeat step 2 until all vertices are connected.
- 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