CS With NehalCS With Nehal

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.

Nodes (V): 5
Edges (E): 7
Classic MST
Watch Kruskal's evaluate edges from lowest to highest weight, merging disjoint sets to build a spanning tree.
Sorted Edge Queue (Lowest Weight)
Empty
12245810
A
set:A
B
set:B
C
set:C
D
set:D
E
set:E
Animation Speed — 1000ms

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

  1. Sort all edges by weight in increasing order.
  2. Start with every node in its own set.
  3. 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.
  4. If u and v are already in the same set, skip the edge because it would create a cycle.
  5. 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

Acknowledgements

The Person Behind This Simulation

This simulation would not have been possible without his valuable contributions and support. We sincerely appreciate his time, effort, and dedication to the project.

Tahsin Uddin Labib

Tahsin Uddin Labib

  • 4th Year CS Undergraduate Student | BRAC University