Kruskal Simulator
Build interactive graphs and watch Kruskal's algorithm construct a Minimum Spanning Tree by sorting edges from lowest to highest cost and connecting the network while avoiding cycles.
About This Simulation
This Kruskal's algorithm simulator sorts the edges of your graph by weight and adds them one by one, skipping any edge that would form a cycle, until a minimum spanning tree connects every node.
Features
- Draw weighted undirected graphs or load examples
- Sorted edge list with the current edge highlighted
- Accepted and rejected (cycle-forming) edges shown clearly
- Running total weight of the minimum spanning tree
- Step-by-step playback with speed control
Explanation
Kruskal's algorithm builds a minimum spanning tree by considering edges from cheapest to most expensive and keeping an edge only if it joins two different components. It uses a disjoint set (union-find) structure to detect cycles quickly.
How it works
- Sort all edges by weight in increasing order.
- Start with every node in its own set.
- Take the next cheapest edge (u, v). If find(u) and find(v) are different, add the edge to the MST and union the two sets.
- If u and v are already in the same set, skip the edge because it would create a cycle.
- Stop after V − 1 edges have been added.
Why it matters
Kruskal's algorithm is simple to implement, works well on sparse graphs and is the standard example of the union-find data structure. It is used in network design and in single-linkage clustering.
Frequently Asked Questions
What is the time complexity of Kruskal's algorithm?
O(E log E), dominated by sorting the edges. The union-find operations are almost constant time with path compression and union by rank.
How does Kruskal's algorithm detect cycles?
With a disjoint set (union-find). If both ends of an edge already belong to the same set, they are already connected, so adding the edge would form a cycle.
When is Kruskal better than Prim?
Kruskal is usually better for sparse graphs or when the edges are already sorted. Prim with an adjacency matrix is usually better for dense graphs.
How many edges does a minimum spanning tree have?
Exactly V − 1 edges for a connected graph with V vertices.
Topics covered
- Kruskal's Algorithm
- Minimum Spanning Tree (MST)
- Union-Find / Disjoint Set
- Greedy Algorithms
- Cycle Detection
- Edge Sorting
Tags
- kruskal
- minimum spanning tree
- mst
- union find
- disjoint set
- greedy
Related Simulations
Algorithms
Searching Algorithms
Run linear, binary, ternary and quaternary search on your own array and step through the code one line at a time.
Algorithms
Sorting Algorithm Visualiser
Visualise sorting algorithms
Algorithms
A* Algorithm
Show pathfinding using cost, heuristic, and total estimated distance.
Algorithms
BFS (Breadth First Search) Simulator
Build interactive graphs and watch BFS explore networks level-by-level using a visual queue.

