212#include <tclap/CmdLine.h>
215using namespace Aleph;
243 cout <<
"\n" << string(60,
'=') <<
endl;
244 cout <<
"Binary Heap: Basic Operations" <<
endl;
247 cout <<
"\n--- Integer Min-Heap ---" <<
endl;
252 vector<int> values = {5, 3, 8, 1, 9, 2, 7, 4, 6};
253 cout <<
"Inserting: ";
265 cout <<
"\nExtracting in order: ";
272 cout <<
"\n--- Task Priority Queue ---" <<
endl;
284 cout <<
"Processing tasks by priority:" <<
endl;
298 cout <<
"\n" << string(60,
'=') <<
endl;
299 cout <<
"Fibonacci Heap: Advanced Operations" <<
endl;
305 cout <<
"\n--- Insert and Decrease-Key ---" <<
endl;
309 cout <<
"Inserting: ";
310 for (
int v : {50, 30, 80, 10, 90, 20, 70})
320 cout <<
"\nDecreasing 80 to 5..." <<
endl;
325 cout <<
"Decreasing 90 to 3..." <<
endl;
329 cout <<
"\nExtracting all: ";
335 cout <<
"\n--- Heap Merge (O(1) in Fibonacci Heap) ---" <<
endl;
340 for (
int v : {10, 30, 50})
348 for (
int v : {20, 40, 60})
356 cout <<
"After merge (heap1 absorbs heap2):" <<
endl;
361 cout <<
"\nExtracting merged heap: ";
372 cout <<
"\n" << string(60,
'=') <<
endl;
373 cout <<
"Practical Example: Event-Driven Simulation" <<
endl;
398 cout <<
"\nSimulation timeline:" <<
endl;
405 cout <<
" t=" <<
setw(4) << e.time <<
": " << e.description <<
endl;
414 cout <<
"\n" << string(60,
'=') <<
endl;
415 cout <<
"Performance Comparison (n = " << n <<
")" <<
endl;
422 vector<int> values(n);
423 for (
int i = 0; i < n; ++i)
430 auto start = chrono::high_resolution_clock::now();
435 auto mid = chrono::high_resolution_clock::now();
440 auto end = chrono::high_resolution_clock::now();
442 auto insert_time = chrono::duration_cast<chrono::microseconds>(
mid - start).count();
443 auto extract_time = chrono::duration_cast<chrono::microseconds>(end -
mid).count();
445 cout <<
"\nBinary Heap (DynBinHeap):" <<
endl;
455 auto start = chrono::high_resolution_clock::now();
460 auto mid = chrono::high_resolution_clock::now();
465 auto end = chrono::high_resolution_clock::now();
467 auto insert_time = chrono::duration_cast<chrono::microseconds>(
mid - start).count();
468 auto extract_time = chrono::duration_cast<chrono::microseconds>(end -
mid).count();
470 cout <<
"\nFibonacci Heap:" <<
endl;
477 cout <<
"\n--- Decrease-Key Performance ---" <<
endl;
489 for (
int i = 0; i < n; ++i)
492 auto start = chrono::high_resolution_clock::now();
500 auto end = chrono::high_resolution_clock::now();
501 auto time = chrono::duration_cast<chrono::microseconds>(end - start).count();
507 cout <<
"\n--- Analysis ---" <<
endl;
508 cout <<
"Binary Heap: Better cache locality, simpler, good for general use" <<
endl;
509 cout <<
"Fibonacci Heap: Better for algorithms with many decrease-key ops" <<
endl;
510 cout <<
" (e.g., Dijkstra, Prim where decrease-key dominates)" <<
endl;
518 cout <<
"\n" << string(60,
'=') <<
endl;
519 cout <<
"Max-Heap: Largest Element First" <<
endl;
525 cout <<
"\nInserting: ";
526 for (
int v : {5, 3, 8, 1, 9, 2, 7})
535 cout <<
"Extracting in descending order: ";
543 cout <<
"\nFibonacci Max-Heap: ";
544 for (
int v : {15, 13, 18, 11, 19, 12, 17})
552 cout <<
"Extracting: ";
562 TCLAP::CmdLine
cmd(
"Heap Data Structures Example",
' ',
"1.0");
564 TCLAP::ValueArg<int>
nArg(
"n",
"count",
565 "Number of elements for performance test",
false, 10000,
"int");
566 TCLAP::SwitchArg
basicArg(
"b",
"basic",
567 "Show basic binary heap operations",
false);
568 TCLAP::SwitchArg
fibArg(
"f",
"fibonacci",
569 "Show Fibonacci heap operations",
false);
570 TCLAP::SwitchArg
simArg(
"s",
"simulation",
571 "Show event simulation example",
false);
572 TCLAP::SwitchArg
perfArg(
"p",
"performance",
573 "Run performance comparison",
false);
574 TCLAP::SwitchArg
maxArg(
"m",
"max",
575 "Show max-heap examples",
false);
576 TCLAP::SwitchArg
allArg(
"a",
"all",
577 "Run all demos",
false);
589 int n =
nArg.getValue();
600 cout <<
"=== Priority Queues: Binary Heap vs Fibonacci Heap ===" <<
endl;
617 cout <<
"\n=== Summary ===" <<
endl;
618 cout <<
"DynBinHeap: General-purpose, simple, cache-friendly" <<
endl;
619 cout <<
"Fibonacci_Heap: O(1) amortized decrease-key, best for graph algorithms" <<
endl;
620 cout <<
"Use DynBinHeap for most cases; Fibonacci for Dijkstra/Prim with many updates" <<
endl;
624 catch (TCLAP::ArgException& e)
626 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
bool operator<(const Time &l, const Time &r)
Dynamic heap of elements of type T ordered by a comparison functor.
T & top() const
Return a reference to the smallest element.
T get()
Alias for getMin().
T & insert(const T &item)
Insert a copy of item into the heap.
T & insert(const T &item)
Insert a new item by copy.
Implementation of a Fibonacci Heap priority queue.
Node * insert(const T &val)
Inserts a new element (copy).
T extract_min()
Extracts and returns the minimum element.
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
bool is_empty() const noexcept
Checks if the heap is empty.
bool is_empty() const noexcept
const size_t & size() const noexcept
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
void demo_max_heap()
Demonstrate max-heap usage.
void demo_event_simulation()
Practical example: Event-driven simulation.
void demo_binary_heap()
Demonstrate basic binary heap operations.
void demo_fibonacci_heap()
Demonstrate Fibonacci heap operations.
void demo_performance_comparison(int n)
Performance comparison between heap types.
Main namespace for Aleph-w library functions.
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Task with priority for job scheduling.
bool operator>(const Task &other) const
Task(const string &n, int p, int d)
bool operator<(const Task &other) const
Dynamic binary heap with node-based storage.
Fibonacci Heap implementation.