To connect a set of islands with bridges, or offices with network cables, at the lowest total cost, we need a set of edges that connects every vertex, has no unnecessary cycles, and has minimum total weight. That set is a minimum spanning tree.
Spanning trees
A graph can have many spanning trees; an MST need not be unique when some weights are equal, but its total weight is.
Kruskal’s algorithm
Start with a forest of n single-vertex trees.
Sort the edges by weight (or put them in a priority queue).
Take edges in increasing order; add an edge if its ends are in different trees (joining the trees); reject it if both ends are already in the same tree (it would form a cycle).
Stop after n−1 edges have been added.
Whether two vertices are in the same tree is tested with a union–find (disjoint-set) structure. Running time: O(eloge), dominated by sorting the edges.
Prim’s algorithm
Start with T = one vertex.
While T has fewer than n vertices: find the cheapest edge with one end in T and the other outside, and add it (and its outside vertex) to T.
With an array of “cheapest edge to the tree” for each outside vertex, Prim runs in O(n2), which is best for dense graphs; with a binary heap it runs in O(elogn).
Comparison
Kruskal
Prim
Grows
A forest of trees that merge
One tree, one vertex at a time
Chooses
Cheapest edge overall that joins two trees
Cheapest edge leaving the current tree
Needs
Sorted edges + union–find
Priority queue or array of best edges
Time
O(eloge)
O(n2) (array) or O(elogn) (heap)
Better for
Sparse graphs
Dense graphs (with the O(n2) array version)
Applications
Designing networks at minimum cost: cables, pipelines, roads, electrical grids.
Connecting n pins on a circuit board with n−1 wires of least total length.
Finding an independent set of circuit equations in an electrical network.
Clustering: deleting the most expensive MST edges splits the points into groups.
A lower bound and approximation for the travelling salesman problem.
Key takeaways
Spanning tree: all n vertices, n−1 edges, no cycle; MST = minimum total weight.
Kruskal: sort edges, add if it joins two different trees (union–find): O(eloge).
Prim: grow one tree with the cheapest crossing edge: O(n2) or O(elogn).
An MST is a lower bound for TSP, not a TSP tour.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Compare Kruskal’s and Prim’s algorithms (principle, data structures, complexity, suitable graphs).
Show worked answer
Principle: both are greedy MST algorithms. Kruskal considers edges in increasing order of weight over the whole graph and adds an edge if it joins two different trees of the growing forest; Prim grows a single tree from a start vertex, each time adding the cheapest edge with exactly one end in the tree. Data structures: Kruskal needs the edges sorted (or a priority queue) and a union–find structure; Prim needs a priority queue or an array of the cheapest edge to the tree for each vertex. Complexity: Kruskal O(eloge); Prim O(n2) with an array or O(elogn) with a binary heap. Suitability: Kruskal is good for sparse graphs (few edges to sort); Prim’s array version is better for dense graphs.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/graphs/minimum-cost-spanning-trees-kruskal-and-prim/ to read this page.