Prim's algorithm

hip-hop, educational · 2:24

Listen on 93

Lyrics

[Verse 1]
Got a graph with vertices scattered around
Need to connect them with minimum cost found
Prim's algorithm gonna show us the way
Start with one vertex, let's begin today
Keep a priority queue of edges so neat
Pick the smallest weight, make connections complete
Growing our tree one edge at a time
Finding that spanning tree, rhythm and rhyme

[Chorus]
Start small, grow tall, minimum spanning tree
Pick the lightest edge that connects you and me
Cut property guarantees we're on the right track
Prim's algorithm, never looking back
Greedy choice, optimal voice, MST is the key
O of V squared with arrays, or V log V with heap, you see

[Verse 2]
Initialize with arbitrary vertex as root
Mark it visited, that's our starting suit
For every neighbor, add edge to the queue
Weighted by distance, keeping costs true
Extract minimum from priority storage
Cross the cut boundary, that's our voyage
Add new vertex to our growing set
Another edge chosen, minimum debt

[Chorus]
Start small, grow tall, minimum spanning tree
Pick the lightest edge that connects you and me
Cut property guarantees we're on the right track
Prim's algorithm, never looking back
Greedy choice, optimal voice, MST is the key
O of V squared with arrays, or V log V with heap, you see

[Bridge]
Cut respect means we're crossing the divide
Between visited and unvisited side
Light edge theorem proves our greedy way
Safe choice every step, never led astray
Update distances as we explore
Each vertex connected, can't ask for more

[Verse 3]
Dense graphs benefit from the matrix approach
Sparse graphs prefer heaps, that's no reproach
Fibonacci heaps can decrease the key
Making updates fast as fast can be
When all vertices join our spanning tree
Total weight minimized, algorithm free
Connected components now unified
Prim's guarantee, mathematically verified

[Outro]
From one to all, we built it right
Minimum spanning tree shining bright
Prim's algorithm, the optimal way
Connecting graphs efficiently every day

← Kosaraju's algorithm | Kruskal's algorithm →