How to implement Prim's algorithm with a Fibonacci heap?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Prim's algorithm builds a minimum spanning tree by repeatedly choosing the cheapest edge that connects the growing tree to a new vertex. A Fibonacci heap improves the theoretical efficiency of the priority-queue part because decrease_key is amortized O(1), which is exactly the operation Prim performs frequently on dense graphs.
Why a Fibonacci Heap Helps
A standard heap-based Prim implementation already works well in practice, but a Fibonacci heap gives the classic asymptotic bound of O(E + V log V) because:
- '
insertis amortizedO(1)' - '
decrease_keyis amortizedO(1)' - '
extract_minis amortizedO(log V)'
Prim uses decrease_key whenever it finds a cheaper connection to a vertex, so the data structure matters.
Minimal Runnable Python Implementation
The following example implements the core Fibonacci-heap operations needed for Prim's algorithm:
This code returns the edges of the minimum spanning tree together with the chosen edge weights.
How Prim Uses decrease_key
The important connection between the heap and the graph algorithm is this:
- Each vertex sits in the heap with its current best known connection cost
- When a cheaper edge to that vertex is found, Prim calls
decrease_key - The next
extract_minpicks the cheapest frontier vertex
That is why Fibonacci heaps are a natural theoretical match for Prim's algorithm.
Common Pitfalls
The biggest mistake is implementing the graph logic correctly but not keeping heap handles for vertices. Without stable references to heap nodes, decrease_key becomes awkward or impossible.
Another issue is underestimating the complexity of Fibonacci-heap pointer operations. Bugs often appear in circular doubly linked lists, child promotion during extract_min, or cascading cuts after decrease_key.
Developers also sometimes choose Fibonacci heaps for every MST problem even though a binary heap is often simpler and faster in practice for ordinary input sizes. The Fibonacci heap wins mainly in theory and in carefully chosen workloads.
Finally, remember that Prim assumes a connected graph if you expect a single spanning tree. On a disconnected graph, the algorithm naturally yields a spanning forest instead.
Summary
- Prim's algorithm repeatedly adds the cheapest edge connecting the tree to a new vertex.
- Fibonacci heaps improve the theoretical complexity because
decrease_keyis amortizedO(1). - A practical implementation needs insert, extract-min, decrease-key, cut, and cascading-cut.
- Keep heap-node handles for vertices so Prim can update priorities efficiently.
- Binary heaps are often simpler in practice, but Fibonacci heaps explain the classic asymptotic result.
Related reading
- How to implement range search in KD-Tree
- How to implement RSI Divergence in Python
- How to implement segment trees with lazy propagation?
- How to implement strlen as fast as possible
- How to implement request-reply (synchronous) messaging paradigm in Kafka?
- How to implement single-consumer-multi-queue model for rabbitMQ
- How to implement the Bayesian average algorithm for a binary rating system
- How to improve the performance of Leetcode 4sum-ii challenge

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.