MethodMath
Ask
Join
HomeTopicsAskAlerts
Profile

© 2026 MethodMath. Built for mathematical knowledge sharing.

AboutContactGuidelinesPrivacyTermsCookies
Chloe Villeneuve
Chloe Villeneuve
Apr 26, 2026

How to find a minimum spanning tree using Kruskal's and Prim's algorithms?

I am studying graph theory and I need to understand minimum spanning trees (MST). A spanning tree of a connected, undirected graph is a subgraph that is a tree and includes all vertices.

I know two algorithms:

  1. Kruskal's algorithm: Sort edges by weight, add edges one by one if they don't form a cycle
  2. Prim's algorithm: Start from a vertex, repeatedly add the smallest edge connecting the tree to a vertex outside

My questions:

  1. Why do these algorithms work (correctness proof)?
  2. What are the time complexities of each?
  3. When should I use Kruskal vs Prim?
  4. How do I detect cycles efficiently in Kruskal's algorithm?
  5. Can I apply MST to a real-world problem like designing a fiber optic network connecting cities?

For example, find the MST of the graph with vertices A-F and edges:
AB:4, AC:2, BC:1, BD:5, CD:8, CE:10, DE:2, DZ:6, EZ:3.

1 answers499 views

1 Answer

1
Priya Sharma
Apr 27, 2026
Accepted
**Kruskal's Algorithm (edge-centric):** 1. Sort edges by weight: BC(1), AC(2), DE(2), AB(4), BD(5), DZ(6), CD(8), CE(10), EZ(3) — wait EZ(3) should be before AB. 2. Add BC(1), AC(2), DE(2), EZ(3), AB(4) — but AB would create cycle A-B-C-A, so skip. Add DZ(6). 3. MST edges: BC(1), AC(2), DE(2), EZ(3), DZ(6). Total weight: 14. **Prim's Algorithm (vertex-centric, starting from A):** - A: pick AC(2) → {A, C} - {A, C}: pick CB(1) → {A, B, C} - {A, B, C}: pick BD(5) → {A, B, C, D} - {A, B, C, D}: pick DE(2) → {A, B, C, D, E} - {A, B, C, D, E}: pick EZ(3) → {A, B, C, D, E, Z} Total: 2 + 1 + 5 + 2 + 3 = 13. (Different from Kruskal because Prim picked BD(5) instead of DZ(6). Both are valid MSTs — there can be multiple.) **Why they work (Cut property):** For any cut partitioning vertices into two sets, the minimum-weight edge crossing the cut belongs to some MST. Both algorithms repeatedly apply this property. **Cycle detection in Kruskal:** Use Union-Find (Disjoint Set Union). Each vertex starts in its own set. When adding edge (u, v), check if find(u) = find(v). If so, adding the edge creates a cycle. Otherwise, union the sets. **Time Complexity:** - Kruskal: $O(E \log V)$ (sorting dominates) - Prim (binary heap): $O(E \log V)$ - Prim (Fibonacci heap): $O(V \log V + E)$ **When to use:** - Kruskal: sparse graphs, easy to implement - Prim: dense graphs, especially when adjacency matrix is available
1 comment
Lisa WangMay 15, 2026

The cut property explanation for MST algorithms is elegant. Union-Find is such a clever data structure.

Still unsure? Join to comment or ask a follow-up.
Login or Register to post your answer

Suggested questions

Why does 0 factorial equal 1 instead of 0? Simple proof

31.4k

Can someone explain Hilbert's hotel paradox simply?

23.9k

How many squares are actually in an 8 by 8 chessboard?

22.2k

Does the Banach-Steinhaus theorem hold for non-complete normed spaces?

31k

Do I need Calculus 3 before learning Fourier series?

3478