|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Priority Queues: Binary Heap vs Fibonacci Heap. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <chrono>#include <random>#include <tpl_dynBinHeap.H>#include <tpl_fibonacci_heap.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Classes | |
| struct | Task |
| Task with priority for job scheduling. More... | |
Functions | |
| ostream & | operator<< (ostream &os, const Task &t) |
| void | demo_binary_heap () |
| Demonstrate basic binary heap operations. | |
| void | demo_fibonacci_heap () |
| Demonstrate Fibonacci heap operations. | |
| void | demo_event_simulation () |
| Practical example: Event-driven simulation. | |
| void | demo_performance_comparison (int n) |
| Performance comparison between heap types. | |
| void | demo_max_heap () |
| Demonstrate max-heap usage. | |
| int | main (int argc, char *argv[]) |
Priority Queues: Binary Heap vs Fibonacci Heap.
This example compares two fundamental heap implementations for priority queues, demonstrating when to use each and their performance characteristics. Priority queues are essential for many algorithms, and choosing the right implementation can significantly impact performance.
A priority queue is a data structure that supports:
Elements are ordered by priority (typically: lower value = higher priority).
A complete binary tree stored in an array satisfying the heap property (parent ≤ children for min-heap).
Characteristics:
Operations:
A more sophisticated heap structure using a collection of trees with lazy consolidation, achieving better amortized complexity.
Characteristics:
Operations:
| Operation | Binary Heap | Fibonacci Heap | Winner |
|---|---|---|---|
| insert | O(log n) | O(1) amortized | Fibonacci |
| find-min | O(1) | O(1) | Tie |
| extract-min | O(log n) | O(log n) amortized | Tie |
| decrease-key | O(log n) | O(1) amortized | Fibonacci |
| merge | O(n) | O(1) | Fibonacci |
Note: Amortized complexity means average over many operations. Individual operations may be slower, but overall performance is better.
✅ General-purpose priority queue
✅ Heap sort
✅ Decrease-key is rare
✅ Memory matters
✅ Many decrease-key operations
✅ Theoretical optimality needed
✅ Merge operations needed
Despite better theoretical complexity, Fibonacci heaps often have:
Result: Binary heaps are often faster in practice for typical workloads!
Rule of thumb: Use binary heap unless you have a specific need for Fibonacci heap's advantages (many decrease-key operations).
Dijkstra's algorithm for shortest paths:
For dense graphs (E ≈ V²), Fibonacci heap provides significant speedup.
Definition in file heap_example.C.
| void demo_binary_heap | ( | ) |
Demonstrate basic binary heap operations.
Definition at line 241 of file heap_example.C.
References Aleph::DynList< T >::get(), Aleph::DynBinHeap< T, Compare >::get(), Aleph::DynList< T >::insert(), Aleph::DynBinHeap< T, Compare >::insert(), Aleph::HTList::is_empty(), Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), Aleph::maps(), Aleph::GenBinHeap< NodeType, Key, Compare >::size(), and Aleph::DynBinHeap< T, Compare >::top().
Referenced by main().
| void demo_event_simulation | ( | ) |
Practical example: Event-driven simulation.
Definition at line 370 of file heap_example.C.
References Aleph::DynList< T >::get(), Aleph::DynList< T >::insert(), Aleph::HTList::is_empty(), Aleph::maps(), and operator<().
Referenced by main().
| void demo_fibonacci_heap | ( | ) |
Demonstrate Fibonacci heap operations.
Definition at line 296 of file heap_example.C.
References Aleph::DynList< T >::insert(), Aleph::HTList::is_empty(), Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void demo_max_heap | ( | ) |
Demonstrate max-heap usage.
Definition at line 516 of file heap_example.C.
References Aleph::DynList< T >::get(), Aleph::DynList< T >::insert(), Aleph::HTList::is_empty(), Aleph::maps(), and Aleph::DynList< T >::top().
Referenced by main().
| void demo_performance_comparison | ( | int | n | ) |
Performance comparison between heap types.
Definition at line 412 of file heap_example.C.
References Aleph::Fibonacci_Heap< T, Compare >::decrease_key(), Aleph::Fibonacci_Heap< T, Compare >::extract_min(), Aleph::DynBinHeap< T, Compare >::get(), Aleph::DynBinHeap< T, Compare >::insert(), Aleph::Fibonacci_Heap< T, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), Aleph::Fibonacci_Heap< T, Compare >::is_empty(), and Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 558 of file heap_example.C.
References demo_binary_heap(), demo_event_simulation(), demo_fibonacci_heap(), demo_max_heap(), demo_performance_comparison(), and Aleph::maps().
| ostream & operator<< | ( | ostream & | os, |
| const Task & | t | ||
| ) |
Definition at line 233 of file heap_example.C.
References Task::duration_ms, Aleph::maps(), Task::name, and Task::priority.