Students often understand Prim's Algorithm and Kruskal's Algorithm separately, but they get confused when an exam asks them to compare both. The names are different, the edges are selected in a different order, and both still give a Minimum Spanning Tree. So what is the actual difference?
This chapter explains Prim's vs Kruskal's Algorithm in Data Structure using the same graph, simple diagrams, step-by-step tables, a working example and a C program. You can follow every step even if this is your first time learning graph algorithms.
Prim's Algorithm starts from one vertex and grows a single tree by choosing the cheapest edge that connects the tree to a new vertex. Kruskal's Algorithm sorts all edges by weight and chooses the cheapest edge that does not form a cycle. Both are greedy algorithms used to find a Minimum Spanning Tree (MST) of a connected, weighted, undirected graph.
What Is a Minimum Spanning Tree?

Imagine five school buildings that need to be connected using internet cables. There are many possible routes, but each cable has a different cost. We want every building connected without spending money on unnecessary connections.
A Minimum Spanning Tree (MST) connects all vertices of a weighted, undirected graph with the smallest total edge weight and without any cycle. A cycle is a closed route that comes back to its starting point. An MST with V vertices has V − 1 edges.
A point in a graph, such as a building marked A.
EdgeA connection between two vertices, such as A–B.
The cost or distance written on an edge.
Greedy algorithmAn algorithm that makes a useful local choice at each step.
What Is Prim's Algorithm?
Prim's Algorithm is a greedy MST algorithm that starts from a chosen vertex. It repeatedly adds the cheapest edge that goes from a vertex already in the tree to a vertex outside the tree.
- Choose any starting vertex, for example A.
- Look at edges going from the current tree to vertices not yet included.
- Select the edge with the lowest weight.
- Add the new vertex and repeat until every vertex is included.
Easy example: Think of extending a cable network from one existing office. At every step, connect the cheapest new office that can be reached from your current network. The growing network stays connected.
What Is Kruskal's Algorithm?
Kruskal's Algorithm is also greedy, but it does not start from a particular vertex. It checks all edges from smallest weight to largest. An edge is selected if it connects two parts of the graph that were not already connected.
- Sort all edges by weight, smallest first.
- Pick the next smallest edge.
- If this edge creates a cycle, skip it. Otherwise, add it.
- Stop once the MST has V − 1 edges in a connected graph.
Easy example: Imagine the contractor first picks the cheapest roads anywhere in a group of villages. At first, this may create separate groups of connected villages. Later, the groups are joined together without making any loop.
Prim's vs Kruskal's Algorithm: Example with Diagram
We will solve both algorithms on exactly the same graph. It has five vertices (A, B, C, D, E) and seven weighted edges. A spanning tree needs 5 − 1 = 4 edges.
Original Weighted Graph
Step-by-Step Prim's Algorithm (Starting from A)
For Prim, the tree begins at A. We may choose only an edge with one endpoint inside the growing tree and the other outside it.
| Step | Choice | Why? | Total |
|---|---|---|---|
| Start | A | Only A is in the tree. | 0 |
| 1 | A–B (2) | From A, this is cheaper than A–C (3). | 2 |
| 2 | B–C (1) | Cheapest edge going to a new vertex. | 3 |
| 3 | B–D (5) | Cheapest way to reach D or E from the current tree. | 8 |
| 4 | D–E (4) | Cheapest edge to the last vertex E. | 12 |
Notice Step 3: D–E has weight 4, but Prim cannot choose it yet because neither D nor E is inside the current tree. It chooses B–D (5) first. After D joins the tree, D–E (4) becomes available.
Step-by-Step Kruskal's Algorithm
For Kruskal, first sort all edges: B–C (1), A–B (2), A–C (3), D–E (4), B–D (5), C–E (7), C–D (8). Now process them one by one.
| Step | Edge | Decision | Total |
|---|---|---|---|
| 1 | B–C (1) | Accept: connects B and C. | 1 |
| 2 | A–B (2) | Accept: adds A to the B–C group. | 3 |
| 3 | A–C (3) | Skip: it makes the A–B–C–A cycle. | 3 |
| 4 | D–E (4) | Accept: makes a separate D–E group. | 7 |
| 5 | B–D (5) | Accept: joins the two groups. MST complete. | 12 |
Notice Step 4: Kruskal can choose D–E even when A, B and C are in a different group. It does not need to grow only one connected tree from the beginning. It just avoids cycles.
Final MST: Same Edges, Same Cost
Interactive Pictorial Comparison: Learn One Step at a Time
Choose Prim or Kruskal, then press Next Step. Green lines are selected edges. A red dashed line means Kruskal rejected an edge because it would form a cycle. The written tables above remain available even if JavaScript is disabled.
Step 0 · MST cost: 0
Choose an algorithm and press Next Step.
Selected: none
Difference Between Prim's and Kruskal's Algorithm
Here is the comparison to learn for 5-mark and 10-mark semester exam questions.
| Basis | Prim's Algorithm | Kruskal's Algorithm |
|---|---|---|
| Method | Grows one connected tree. | Joins separate components into a tree. |
| Start | Starts from a chosen vertex. | Does not need a starting vertex. |
| Edge choice | Cheapest edge from the tree to an outside vertex. | Cheapest edge overall that does not create a cycle. |
| Cycle control | Only connects visited to unvisited vertices. | Uses a component check, often Union-Find. |
| Typical data structure | Priority queue / min-heap or adjacency matrix. | Sorted edge list and Union-Find. |
| Typical time | O(V²) with matrix; O((V + E) log V) with binary heap. | O(E log E) with efficient sorting and Union-Find. |
| Disconnected graph | One run from a vertex covers its component; restart for a forest. | Can produce a minimum spanning forest. |
| Practical choice | Convenient for adjacency-based graphs, including dense ones with a matrix. | Convenient when all edges are provided as a list. |
Why Do the Algorithms Pick Edges in Different Orders?
Prim asks, "Which cheap connection can extend my current tree?" Kruskal asks, "Which cheapest safe connection is available anywhere in the graph?"
That is why Prim from A picks A–B (2) before B–C (1), while Kruskal starts with B–C (1). Prim also waits before choosing D–E (4), while Kruskal can pick it as a separate group. In this graph, the final MST still uses the same four edges.
Prim's and Kruskal's Algorithms: Simple Pseudocode
Start at vertex A
Mark A as included
While vertices remain:
Find cheapest edge
from tree to new vertex
Add edge and vertex
Return the tree
Sort all edges by weight
Make separate sets
For each sorted edge:
If endpoints in
different sets:
Add edge
Join their sets
Stop after V - 1 edges
C Program for Prim's and Kruskal's Algorithm
This single C program runs both algorithms on the same five-vertex graph. It uses an adjacency matrix for Prim and an edge list with Union-Find for Kruskal. Keeping both in one file makes the comparison easy to check.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 5
#define E 7
typedef struct {
int u, v, w;
} Edge;
int parent[V], rankVal[V];
int findRoot(int x) {
if (parent[x] != x)
parent[x] = findRoot(parent[x]);
return parent[x];
}
int unite(int a, int b) {
a = findRoot(a);
b = findRoot(b);
if (a == b) return 0;
if (rankVal[a] < rankVal[b]) parent[a] = b;
else if (rankVal[a] > rankVal[b]) parent[b] = a;
else { parent[b] = a; rankVal[a]++; }
return 1;
}
int compare(const void *a, const void *b) {
const Edge *x = (const Edge *)a;
const Edge *y = (const Edge *)b;
return (x->w > y->w) - (x->w < y->w);
}
void prim(int g[V][V]) {
int key[V], from[V], used[V] = {0};
int total = 0;
for (int i = 0; i < V; i++) {
key[i] = INT_MAX;
from[i] = -1;
}
key[0] = 0; /* Start from A */
printf("Prim's Algorithm (start A):\n");
for (int step = 0; step < V; step++) {
int u = -1;
for (int i = 0; i < V; i++) {
if (!used[i] &&
(u == -1 || key[i] < key[u]))
u = i;
}
if (u == -1 || key[u] == INT_MAX) {
printf("Graph is disconnected.\n");
return;
}
used[u] = 1;
if (from[u] != -1) {
printf("%c-%c: %d\n", 'A'+from[u],
'A'+u, key[u]);
total += key[u];
}
for (int v = 0; v < V; v++) {
if (g[u][v] > 0 && !used[v]
&& g[u][v] < key[v]) {
key[v] = g[u][v];
from[v] = u;
}
}
}
printf("Total cost: %d\n", total);
}
void kruskal(Edge e[E]) {
int chosen = 0, total = 0;
for (int i = 0; i < V; i++) {
parent[i] = i;
rankVal[i] = 0;
}
qsort(e, E, sizeof(Edge), compare);
printf("\nKruskal's Algorithm:\n");
for (int i = 0; i < E && chosen < V-1; i++) {
if (unite(e[i].u, e[i].v)) {
printf("%c-%c: %d\n", 'A'+e[i].u,
'A'+e[i].v, e[i].w);
total += e[i].w;
chosen++;
}
}
if (chosen == V-1)
printf("Total cost: %d\n", total);
else
printf("Graph is disconnected.\n");
}
int main(void) {
int graph[V][V] = {
{0,2,3,0,0},
{2,0,1,5,0},
{3,1,0,8,7},
{0,5,8,0,4},
{0,0,7,4,0}
};
Edge edges[E] = {
{1,2,1}, {0,1,2}, {0,2,3},
{3,4,4}, {1,3,5}, {2,4,7}, {2,3,8}
};
prim(graph);
kruskal(edges);
return 0;
}
Output
Prim's Algorithm (start A):
A-B: 2
B-C: 1
B-D: 5
D-E: 4
Total cost: 12
Kruskal's Algorithm:
B-C: 1
A-B: 2
D-E: 4
B-D: 5
Total cost: 12
How to run: Save the file as mst_compare.c. In a terminal with GCC, run gcc -std=c11 -Wall -Wextra mst_compare.c -o mst_compare. On Windows PowerShell, run .\mst_compare.exe.
Time Complexity Explained in Easy Language
V means the number of vertices, and E means the number of edges. Time complexity describes how the work grows as a graph becomes larger.
For Prim's Algorithm, the adjacency-matrix version repeatedly scans vertices, taking O(V²) time. A binary heap with an adjacency list commonly takes O((V + E) log V).
For Kruskal's Algorithm, sorting E edges usually takes O(E log E). Union-Find makes connection checks very fast, so sorting normally dominates. These bounds describe standard implementations; a particular library sorting function may not promise the same worst-case bound.
Space: Prim with an adjacency matrix stores O(V²) graph entries. Kruskal usually stores O(E) edges and O(V) Union-Find information, or O(E + V) including both.
When Should You Use Prim or Kruskal?
There is no rule that one algorithm is always faster. The best choice depends on the graph representation, number of vertices and edges, and available data structures.
- Choose Prim when you want to grow a network from one point, especially when an adjacency representation is already available.
- Choose Kruskal when the graph is given as a list of weighted edges and sorting them is straightforward.
- For a dense graph, matrix-based Prim can be a simple, effective choice.
- For a sparse graph, either algorithm can work efficiently with suitable data structures.
Common Mistakes to Avoid
- Do not say Prim always selects the smallest edge in the entire graph. It chooses the smallest edge leaving the current tree.
- Do not say Kruskal needs a starting vertex. It does not.
- Do not accept an edge in Kruskal when it creates a cycle, even if that edge is cheap.
- Do not confuse MST with the shortest route between two vertices.
- Do not claim both algorithms must produce the same exact tree when equal weights allow multiple MSTs. They must have the same minimum total cost, but their selected edges may differ.
Important Exam Answers
2 Marks: Write the Main Difference
Prim's Algorithm starts from one vertex and extends a single connected tree. Kruskal's Algorithm sorts edges and adds the lowest-weight edges that do not create cycles.
5 Marks: Compare Prim and Kruskal
Both are greedy MST algorithms. Prim grows one tree from a starting vertex and chooses the cheapest outgoing edge. Kruskal sorts every edge and joins different components while avoiding cycles, commonly using Union-Find. Their typical implementations and time complexities also differ.
10 Marks: How to Write a Complete Answer
Start with an MST definition and the V − 1 rule. Write the steps for both algorithms. Draw one weighted graph, show a separate dry-run table for Prim and Kruskal, then write the key differences, the final MST cost and time complexity. This chapter's example gives 12 for both methods.
Frequently Asked Questions
Do Prim and Kruskal always give the same MST?
They always give an MST with the minimum possible total weight on a connected, weighted, undirected graph. If there are ties, they can select different edges and still have the same minimum cost.
Which algorithm uses Union-Find?
Kruskal commonly uses Union-Find to check whether adding an edge would form a cycle. Prim normally tracks vertices already included in the growing tree.
Can they work on a disconnected graph?
A disconnected graph has no single spanning tree covering every vertex. Kruskal naturally gives a minimum spanning forest. Prim can also produce a forest if restarted for every unvisited component.
Quick Revision and Conclusion
Prim = Start from a vertex and grow one tree. Kruskal = Sort all edges and avoid cycles.
Both find a Minimum Spanning Tree. In our example, Prim chooses A–B, B–C, B–D and D–E. Kruskal chooses B–C, A–B, D–E and B–D, after rejecting A–C because it forms a cycle. Both finish with 4 edges and total cost 12.
For exam preparation, practise these two edge-selection orders on the same graph. That one exercise makes the difference much easier to remember.
