UPI Study › Courses › Discrete Mathematics › Free lessons › Lesson 11.6 Free with a free account
Prim's Algorithm for Minimum Spanning Trees
Discrete Mathematics · Chapter 11: Trees, Spanning Trees, and Minimum Cost NetworksSection 11.6 · lesson 76 of 878 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Discrete Mathematics.
Lesson complete
Earn the 4 college credits for Discrete Mathematics
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Introduction to Matrices →← Kruskal's Algorithm for Minimum Spanning TreesNext lesson: Introduction to Matrices →
In this lesson
Grow the network outward, one cheapest link at a time.
- State Prim's algorithm and its frontier edges
- Trace Prim's algorithm with a priority queue
- Contrast vertex-based Prim with edge-based Kruskal
- Choose between them by density and use
Transcript
Everything said in the video, section by section (1,078 words). Read along, or search it.
Trees, Spanning Trees, and Minimum Cost Networks
Prim's Algorithm for Minimum Spanning Trees
This section closes chapter eleven with the second classic minimum spanning tree algorithm. Prim's algorithm grows a network outward from wherever it already stands, one cheapest link at a time. It reaches the same kind of optimal answer as Kruskal's algorithm, by a genuinely different mechanism.
The Opening Problem
Building Out From a Data Center
A fiber-optic provider already has one data center, H, up and running, and needs to extend service to four nearby towns. The costs of every feasible link are surveyed in advance. Kruskal's approach looked at every possible connection across the whole map before choosing. But these crews work outward from the data center that already exists. At each stage, they ask a narrower question. Of all the links that would extend the current network to exactly one new site, which is cheapest? They build it, and ask again from the bigger network. That's Prim's algorithm. And like Kruskal's, it always produces a minimum spanning tree.
11.6.1
Prim's Algorithm
Here's the algorithm. Start with a tree, T, containing only a chosen start vertex. A frontier edge is an edge with exactly one endpoint in T, and the other outside. Repeatedly add the cheapest frontier edge to T, which brings its outside endpoint into the tree. Stop when T spans every vertex. Notice what's never considered. An edge between two vertices both outside T, or both already inside T, is never a candidate. So growth happens by admitting one new vertex at a time to a single tree, which is connected at every step. That's what makes Prim's algorithm vertex-based.
The Code
Prim's Algorithm With a Min-Heap
In Python, a min-heap holds the frontier edges, as weight, inside vertex, outside vertex. The heap always returns the cheapest one first. At each step, pop the cheapest edge. If its outside vertex is already in the tree, both ends are inside, so it's not really a frontier edge anymore. Skip it. Otherwise, add that vertex and edge to the tree, and push the new vertex's edges to vertices still outside. The loop ends once every vertex is visited, returning the tree edges and their total weight.
11.6.3
Running Prim From H
Here's a worked example. The vertices are the data center H and towns P, Q, R and S. The edge costs, in thousands of dollars, are H P one, H Q seven, P Q three, P R six, Q R two, Q S five, and R S four. Start from H. The frontier is H P at one, and H Q at seven. Add H P, bringing in P. That adds P Q at three and P R at six to the frontier. The cheapest of seven, three and six is P Q. Add it, bringing in Q. Now H Q joins two tree vertices, so it drops out. Q R at two and Q S at five join the frontier. The cheapest of six, two and five is Q R. Add it, bringing in R. P R drops out, and R S at four joins. Finally, R S at four beats Q S at five. Add it, and every vertex is in. The minimum spanning tree is H P, P Q, Q R and R S, with total weight one plus three plus two plus four, which is ten.
11.6.2
Same Graph, 2 Edges In
Here's the same graph, twice, after each algorithm has accepted two edges. Prim has taken H P, then P Q. Its accepted edges form one connected tree, H, P and Q. Kruskal sorts all edges first. Its two cheapest are H P at one, and Q R at two. So after two edges, it holds two separate fragments, H P and Q R, with S untouched. That's the structural difference. Kruskal can accept an edge anywhere in the graph, so its partial result can be in pieces. Prim's partial result is always one connected tree.
Kruskal on the Same Graph
Different Route, Identical Tree
Let's finish Kruskal on this graph. The sorted order is H P, Q R, P Q, R S, Q S, P R, H Q. It accepts H P, then Q R, giving two fragments. Then P Q merges them into one. Then R S brings in S. That's four edges, so it's done. Both algorithms end with exactly the same edges, H P, P Q, Q R and R S, and the same total, ten. They discovered those edges in a different order. And Kruskal passed through a temporarily disconnected stage that Prim never does.
Table 1
Kruskal Versus Prim
Here's the full comparison. Kruskal adds the cheapest remaining edge anywhere, if it doesn't close a cycle. Prim adds the cheapest edge leaving the current tree. Kruskal needs a sorted edge list and union-find. Prim needs a priority queue over the frontier. Along the way, Kruskal may hold several disconnected fragments. Prim always holds one connected tree. Kruskal runs in order E log E, dominated by sorting. Prim runs in order E log V with a heap, or order V squared with a plain array. And Kruskal fits sparse graphs stored as edge lists, while Prim fits dense graphs stored as adjacency matrices.
11.6.4
Choosing Between Them
Both always return a correct minimum spanning tree. So choosing between them is about efficiency and convenience, not correctness. Density is the main factor. On a sparse graph, Kruskal's sort stays cheap. On a dense graph, where the number of edges approaches V squared, Prim's array version, at order V squared, beats sorting all those edges. Representation matters too. With an adjacency matrix, Prim just scans rows. With an edge list, Kruskal sorts it directly. And sometimes one requirement settles it. If a valid, connected partial network must exist at every stage, only Prim guarantees that. That's the data center build-out. Crews may need the network to be usable after each phase of construction, so they grow it outward from H.
Takeaways
Grow From Where You Stand
To summarize. Prim's algorithm grows one connected tree by always adding the cheapest frontier edge, the cheapest edge linking the tree to a new vertex. Kruskal's algorithm works from one global sorted order, so its accepted edges may form several fragments before merging. Prim's accepted edges are connected at every step. Both are greedy, both are guaranteed correct, and they can find the very same tree by entirely different routes. Prim tends to suit dense graphs, adjacency matrices, and staged construction. Kruskal tends to suit sparse graphs given as edge lists. Here's something to think about. Prim's algorithm is run twice on the same connected, weighted graph, from two different starting vertices. Must the two runs find the identical spanning tree? And what role, if any, does it play whether the edge weights are all distinct?
About Discrete Mathematics at UPI Study
Discrete Mathematics (Math 230) is a self-paced online course of 87 video lessons in 13 chapters, from logic and proof to counting, probability, graph theory and trees. It carries a NCCRS credit recommendation for 4 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take Discrete Mathematics with UPI Study
Where the credit goes
Universities that work with NCCRS
Discrete Mathematics carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Prim's Algorithm for Minimum Spanning Trees
What is Prim's algorithm, and what is a frontier edge?
How do you trace Prim's algorithm with a priority queue?
What is the difference between Prim's and Kruskal's algorithms?
When should I use Prim's algorithm instead of Kruskal's?
About the Discrete Mathematics course
How many college credits is Discrete Mathematics?
How much does Discrete Mathematics cost and what is included?
Which universities accept UPI Study credits?
How long does Discrete Mathematics take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 4 credits in Discrete Mathematics.
87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
4 credits · NCCRSEnroll →