CS With NehalCS With Nehal

Prim's Simulator

Build interactive graphs and watch Prim's algorithm prioritize the lowest-cost edges to connect all nodes into a Minimum Spanning Tree using a real-time priority queue.

Nodes (V): 5
Edges (E): 7
Classic Prim's
Watch Prim's expand from the start node, always picking the cheapest edge connecting the growing tree to an unvisited node.
Available Edges (Sorted)
Empty
12245810
A
Unvisited
B
Unvisited
C
Unvisited
D
Unvisited
E
Unvisited
Animation Speed — 1000ms

About This Simulation

This Prim's algorithm simulator lets you build a weighted undirected graph and watch a minimum spanning tree grow from one starting node, always adding the cheapest edge that connects a new node to the tree.

Features

  • Draw weighted undirected graphs or load examples
  • Choose the starting node
  • Live priority queue of candidate edges
  • Tree edges and rejected edges highlighted
  • Running total weight of the minimum spanning tree

Explanation

Prim's algorithm builds a minimum spanning tree (MST): a set of edges that connects every node with the smallest possible total weight and no cycles. It grows a single tree from a start node, one cheapest edge at a time.

How it works

  1. Start the tree with any node and add all of its edges to a priority queue.
  2. Remove the cheapest edge from the queue.
  3. If it leads to a node already in the tree, discard it (it would create a cycle).
  4. Otherwise add the edge and its new node to the tree, and push the new node's edges to the queue.
  5. Stop when the tree contains all V nodes, which means it has V − 1 edges.

Why it matters

Minimum spanning trees are used to design cheap networks (cables, roads, pipelines), in clustering and in approximation algorithms such as the travelling salesman 2-approximation. Prim and Kruskal are standard CSE221 topics.

Frequently Asked Questions

What is the time complexity of Prim's algorithm?

O(E log V) with a binary heap and an adjacency list, or O(V²) with an adjacency matrix and a simple array, which is better for dense graphs.

What is the difference between Prim's and Kruskal's algorithm?

Prim grows one connected tree from a start node, choosing the cheapest edge leaving the tree. Kruskal sorts all edges and adds the cheapest edge that does not form a cycle, so it may build several trees that later join. Both give a minimum spanning tree.

Does Prim's algorithm work with negative edge weights?

Yes. Minimum spanning tree algorithms only compare edge weights with each other, so negative weights are fine.

Is the minimum spanning tree unique?

If all edge weights are different, the MST is unique. If some weights are equal, there can be several MSTs with the same total weight.

Topics covered

  • Prim's Algorithm
  • Minimum Spanning Tree (MST)
  • Greedy Algorithms
  • Priority Queue
  • Cut Property
  • Weighted Undirected Graphs

Tags

  • prim
  • minimum spanning tree
  • mst
  • 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