AiTechWorlds
AiTechWorlds
Visualize Kruskal's MST sorting edges and using union-find to avoid cycles, with synced code and live weight.
def kruskal(edges, n): # edges: (w, u, v)
p = list(range(n))
def find(x):
while p[x] != x: p[x] = p[p[x]]; x = p[x]
return x
total = 0
for w, u, v in sorted(edges):
if find(u) != find(v):
p[find(u)] = find(v); total += w
return totalKruskal's algorithm builds a minimum spanning tree by sorting all edges and adding the next cheapest edge that does not form a cycle, using a union-find structure.
| Time | O(E log E) |
| Space | O(V) |
| Graph | Undirected, weighted |
The Kruskal's Algorithm Visualizer animates how Kruskal's minimum spanning tree (MST) works: it sorts all edges by weight, then adds each next-cheapest edge if it does not create a cycle, using a union-find (disjoint set) structure. Kruskal's runs in O(E log E) time, uses O(V) space, and is ideal for sparse graphs.
Generate a graph
Click New Graph or set the node-count slider to create a random weighted graph.
Play the animation
Press Play to watch edges considered in weight order; cycle-forming edges are skipped.
Step and scrub
Use Step Forward/Back or the timeline to inspect each accept/skip decision.
Read the synced code
The highlighted pseudocode line tracks the union-find logic; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.