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.
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
- Start the tree with any node and add all of its edges to a priority queue.
- Remove the cheapest edge from the queue.
- If it leads to a node already in the tree, discard it (it would create a cycle).
- Otherwise add the edge and its new node to the tree, and push the new node's edges to the queue.
- 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
Algorithms
Searching Algorithms
Run linear, binary, ternary and quaternary search on your own array and step through the code one line at a time.
Algorithms
Sorting Algorithm Visualiser
Visualise sorting algorithms
Algorithms
A* Algorithm
Show pathfinding using cost, heuristic, and total estimated distance.
Algorithms
BFS (Breadth First Search) Simulator
Build interactive graphs and watch BFS explore networks level-by-level using a visual queue.

