#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
⚡ FREE · KRUSKAL AND PRIM

MST Visualizer: Kruskal and Prim, One Edge at a Time

Enter a weighted graph and watch the minimum spanning tree grow edge by edge.

  • 🟢 Taken edges in green
  • 🔴 Cycle edges skipped
  • ⚖️ Total weight

How to use the Minimum Spanning Tree Visualizer

  1. 1. Enter weighted edgesOne per line, like A-B 4.
  2. 2. Pick an algorithmKruskal, or Prim with a start node.
  3. 3. Step throughSee why each edge is taken or skipped.

Two greedy algorithms

A minimum spanning tree connects every node of a weighted, undirected graph using the least total edge weight, with no cycles. It always has V − 1 edges. It's used to design networks such as cables, pipelines and roads, and in clustering.

  • Kruskal's algorithm sorts all edges by weight and takes each one unless it would create a cycle. A union-find structure tracks which nodes are already connected. Time: O(E log E).
  • Prim's algorithm grows a single tree from a start node, always adding the cheapest edge that reaches a new node. With a binary heap: O(E log V).

Both are greedy and both give the same total weight. If several edges have equal weights, the exact edges chosen can differ. If the graph isn't connected, Kruskal gives a minimum spanning forest, and the tool tells you.

Worked example

The sample is the 9-node graph from the standard algorithms textbook (CLRS), with 14 edges. Running Kruskal:

  1. Take G-H (1), C-I (2) and F-G (2), then A-B (4) and C-F (4).
  2. Skip G-I (6): G and I are already connected through F and C.
  3. Take C-D (7); skip H-I (7); take A-H (8); skip B-C (8); take D-E (9).

That's 8 edges with a total weight of 37. Run Prim from A and the edges are added in a different order, A-B first, but the total is again 37. Taken edges turn green and skipped ones are dashed red, so you can see exactly which edges would have made a cycle.

Frequently asked questions

Do negative weights work?

Yes. Both algorithms only compare weights, so negative values are fine for an MST.

Does it work on directed graphs?

No. MSTs are defined for undirected graphs. The directed version is called a minimum arborescence.

When is Prim better than Kruskal?

On dense graphs with many edges, Prim with an adjacency matrix or heap is usually faster; Kruskal suits sparse graphs.

Is the minimum spanning tree visualizer free to use?

Yes, the minimum spanning tree visualizer is free and costs nothing. Your first MST shows a short form for your name and WhatsApp number one time; once that's done, this tool and every other Uncodemy tool open with no form.

More free tools

See all free tools