The short answer

Both Prim’s and Kruskal’s are greedy algorithms that find the Minimum Spanning Tree of a weighted graph. Prim’s grows one tree from a start vertex, always adding the cheapest edge to a new vertex. Kruskal’s sorts all edges and adds the cheapest one that does not form a cycle. So Prim’s is vertex-based and Kruskal’s is edge-based, and both give an MST of the same total weight.

Prim’s and Kruskal’s are the two classic algorithms for building a Minimum Spanning Tree. They are a staple of DSA courses and GATE exams, and students often mix up how each one picks its edges.

Both reach the same goal by a greedy path, but they get there differently. This guide defines a spanning tree, explains each algorithm with examples, compares them in detail, and shows when to use which.

They build on graph basics, so it helps to know the difference between a graph and a tree first.

Two-panel weighted graph diagram showing Prim's algorithm growing a minimum spanning tree from start vertex A versus Kruskal's algorithm picking the cheapest edges across the whole graph
Prim’s grows the tree from one vertex; Kruskal’s picks the cheapest edges across the whole graph.

What is a Minimum Spanning Tree?

A spanning tree of a connected, weighted graph connects all the vertices with the fewest edges and no cycles. For a graph with V vertices, a spanning tree always has exactly V minus 1 edges. A graph can have many spanning trees.

The Minimum Spanning Tree (MST) is the spanning tree with the lowest total edge weight. Both Prim’s and Kruskal’s algorithms find this MST. They are used in network design, such as laying cable, pipelines, or roads at the least total cost.

What is Prim’s Algorithm?

Prim’s algorithm is a greedy, vertex-based method. It starts at any one vertex and grows a single tree outward. At each step it adds the cheapest edge that connects a vertex already in the tree to a vertex outside it.

It keeps going until every vertex is included. To find the cheapest connecting edge quickly, it uses a priority queue or min-heap. By construction, it never creates a cycle.

Advantages of Prim’s:

  • Efficient on dense graphs, where edges are plentiful.
  • Simple to implement with a priority queue or an adjacency matrix.
  • Builds one connected tree throughout, which is easy to reason about.

Disadvantages of Prim’s:

  • Less efficient on sparse graphs than Kruskal’s.
  • Needs a connected graph to span every vertex.
  • Harder to parallelise because the tree grows in sequence.

Complexity: time is O(E log V) with a binary heap, or O(V²) with an adjacency matrix. Space is O(V) for the priority queue and vertex tracking.

What is Kruskal’s Algorithm?

Kruskal’s algorithm is a greedy, edge-based method. It first sorts all the edges by weight. Then it adds the next cheapest edge, as long as that edge does not form a cycle.

It starts with every vertex as its own piece and merges them into one tree. To check whether an edge would form a cycle, it uses a Union-Find (disjoint set) structure.

Advantages of Kruskal’s:

  • Efficient on sparse graphs, where edges are few.
  • Works on a disconnected graph too, returning a minimum spanning forest.
  • Easier to parallelise, since independent edges can be considered together.

Disadvantages of Kruskal’s:

  • Slower on dense graphs because sorting many edges is costly.
  • Needs a Union-Find structure, which adds implementation effort.
  • Sorting the full edge list dominates the running time.

Complexity: time is O(E log E), which is the same as O(E log V), dominated by sorting. Space is O(V + E) for the edge list and the disjoint set.

Prim’s vs Kruskal’s Algorithm: Comparison Table

Comparison infographic listing approach, data structure, time complexity and best graph type for Prim's versus Kruskal's algorithm
Prim’s vs Kruskal’s at a glance.
Weighted graph with nodes A B C D where the minimum spanning tree edges AC, AB and BD with total weight 5 are highlighted
Both algorithms build the same MST: AC(1) + AB(2) + BD(2), total weight 5.
AspectPrim’s AlgorithmKruskal’s Algorithm
ApproachVertex-based (grows from a vertex)Edge-based (sorts all edges)
BuildsOne growing treeA forest that merges into a tree
Edge selectionCheapest edge to a new vertexGlobally cheapest edge with no cycle
Data structurePriority queue / min-heapSorting + Union-Find (DSU)
Cycle handlingNever forms a cycle by designExplicit cycle check (Union-Find)
Starting pointA chosen start vertexNo start; global edges
Time complexityO(E log V) heap, O(V²) matrixO(E log E) = O(E log V)
Space complexityO(V)O(V + E)
Best forDense graphsSparse graphs
Disconnected graphsNeeds a connected graphWorks (gives a spanning forest)
Negative weightsHandled fineHandled fine
ParallelisationHard (sequential growth)Easier (independent edges)
Memory useMore (queue + vertex tracking)Less (mainly the disjoint set)
StrategyGreedyGreedy
ResultAn MSTAn MST of the same total weight

Worked Example

Take a graph with four vertices, A, B, C, and D, and these weighted edges: AB(2), AC(1), AD(4), BC(3), BD(2), CD(5). Both algorithms find the same minimum spanning tree.

Prim’s, starting at A:

  • From A, the cheapest edge is AC(1). Add C.
  • From the tree {A, C}, the cheapest new edge is AB(2). Add B.
  • From {A, B, C}, the cheapest new edge is BD(2). Add D.

Kruskal’s, sorting edges:

  • Sorted order: AC(1), AB(2), BD(2), BC(3), AD(4), CD(5).
  • Add AC(1), then AB(2), then BD(2). Each joins two separate pieces, so none forms a cycle.

Both produce the MST {AC, AB, BD} with a total weight of 5. The tree has 3 edges for 4 vertices, as expected.

How They Work, Step by Step

Prim’s follows these steps:

  • Pick any start vertex and add it to the tree.
  • Find the cheapest edge from the tree to a vertex not yet in it.
  • Add that edge and vertex, then repeat until all vertices are included.

Kruskal’s follows these steps:

  • Sort every edge by weight, from smallest to largest.
  • Take the next cheapest edge and add it if it joins two separate pieces.
  • Skip any edge that would form a cycle, and repeat until the tree is complete.

Applications of MST Algorithms

Minimum spanning trees solve real “least total cost to connect everything” problems, so both algorithms show up widely.

  • Network and circuit design: laying cables, pipelines, roads, or wiring a circuit board at the lowest total cost.
  • Cluster analysis: grouping similar data points, where Kruskal’s edge order is handy.
  • Image segmentation: splitting an image into regions based on pixel similarity.
  • Approximation algorithms: building blocks for problems like the travelling salesman.

Prim’s is common in dense network problems, while Kruskal’s suits sparse graphs and clustering.

When to Use Prim’s or Kruskal’s

Use Prim’s when the graph is dense, with many edges. Growing from a vertex with a priority queue is efficient there, and it avoids sorting a huge edge list.

Use Kruskal’s when the graph is sparse, with few edges. Sorting a short edge list is cheap, and Union-Find makes the cycle checks fast. Kruskal’s also handles a disconnected graph by returning a spanning forest.

Either way, both return a minimum spanning tree of the same total weight, so the choice is about graph shape, not the result.

Interview Questions

Prim’s is vertex-based: it grows one tree from a start vertex, always adding the cheapest edge to a new vertex, using a priority queue. Kruskal’s is edge-based: it sorts all edges and adds the cheapest one that does not form a cycle, using Union-Find. Both are greedy and both produce a minimum spanning tree of the same total weight.

Prim’s is usually better for dense graphs, because growing from vertices with a heap avoids sorting a large edge list. Kruskal’s is usually better for sparse graphs, because sorting a short edge list is cheap and Union-Find handles the cycle checks quickly.

Yes, both work with negative edge weights. A minimum spanning tree only depends on the relative order of edge weights, not their sign, so negative weights cause no problem for either algorithm. This is unlike shortest-path algorithms such as Dijkstra, which can fail with negative weights.

Prim’s runs in O(E log V) with a binary heap, or O(V²) with an adjacency matrix, using O(V) space. Kruskal’s runs in O(E log E), which equals O(E log V), dominated by sorting the edges, using O(V + E) space.

Frequently Asked Questions

Prim’s algorithm grows a single tree from a start vertex by adding the cheapest edge to a new vertex, using a priority queue. Kruskal’s algorithm sorts all edges and adds the cheapest one that does not form a cycle, using Union-Find. Prim’s is vertex-based and Kruskal’s is edge-based, but both find a minimum spanning tree.

They always give a minimum spanning tree of the same total weight. The exact tree can differ when the graph has several edges of equal weight, since the two algorithms may pick different ones. The minimum total cost, however, is always the same.

Prim’s algorithm uses a priority queue or min-heap to find the cheapest edge to a new vertex quickly. Kruskal’s algorithm uses sorting to order the edges, plus a Union-Find (disjoint set) structure to detect cycles when adding each edge.

Choose Prim’s for a dense graph with many edges, or when you want to grow the tree from a specific start vertex, especially with an adjacency matrix. Choose Kruskal’s for a sparse graph with few edges, or when the graph may be disconnected, since it returns a spanning forest.

Wrapping Up

Prim’s and Kruskal’s both build a minimum spanning tree, but from different angles. Prim’s grows one tree from a vertex with a priority queue, while Kruskal’s sorts edges and joins pieces with Union-Find.

Remember the simple rule: Prim’s suits dense graphs and Kruskal’s suits sparse graphs. Both are greedy, both handle negative weights, and both return an MST of the same total cost.

Related reading on DiffStudy:


Whatsapp-color Created with Sketch.

By Arun Kumar

Full Stack Developer with a BE in Computer Science, working with React, Next.js, Node.js, MongoDB, and AI/ML tools. Founder of DiffStudy — built to help CS students ace GATE and university exams, and keep developers up to date across AI, cloud, system design, web development, and every field of computer science. Every article is written from real hands-on experience, not just theory.

Leave a Reply

Your email address will not be published. Required fields are marked *


You cannot copy content of this page