Bölüm 4.3 · Simülatör

Minimum Spanning Tree

← Bölüm 4.3
MST edgeexaminedcrossing the cut

Ready.

 

 

Minimum Spanning Tree

Generate a connected weighted graph and compare Prim's and Kruskal's algorithms step by step.

How to use

  • Set the number of nodes (2–15) and press Generate. The nodes form a ring (so the graph is always connected) with extra random chords; weights are 1–20.
  • Drag nodes to rearrange the drawing (not while an algorithm is running).
  • Run Prim's Algorithm (starts from node 0) or Kruskal's Algorithm. Cancel stops the animation; Speed changes its pace.
  • Thick edges belong to the tree. Prim shows the edges crossing the cut between the tree and the remaining nodes; Kruskal shows each examined edge and marks the edges skipped because they would close a cycle.

Prim's Algorithm

Starts from one vertex and repeatedly selects the cheapest edge that connects the current tree to a new vertex. With a binary heap it runs in O(E log V).

Kruskal's Algorithm

Processes edges from smallest weight to largest and adds an edge only if it does not create a cycle. Cycle checks use a union–find structure (path compression and union by rank); sorting dominates, giving O(E log E).

Both algorithms are correct because of the cut property: the lightest edge crossing any cut belongs to some minimum spanning tree. When several edges have equal weight, the two algorithms may pick different trees, but the total weight is always the same.