212#include <tclap/CmdLine.h>
215using namespace Aleph;
243 cout <<
"\n" << string(60,
'=') << endl;
244 cout <<
"Binary Heap: Basic Operations" << endl;
245 cout << string(60,
'=') << endl;
247 cout <<
"\n--- Integer Min-Heap ---" << endl;
252 vector<int> values = {5, 3, 8, 1, 9, 2, 7, 4, 6};
253 cout <<
"Inserting: ";
261 cout <<
"Heap size: " << heap.
size() << endl;
262 cout <<
"Minimum: " << heap.
top() << endl;
265 cout <<
"\nExtracting in order: ";
268 cout << heap.
get() <<
" ";
272 cout <<
"\n--- Task Priority Queue ---" << endl;
277 task_queue.
insert(
Task(
"Background sync", 5, 1000));
279 task_queue.
insert(
Task(
"Network request", 3, 200));
281 task_queue.
insert(
Task(
"Critical alert", 0, 5));
282 task_queue.
insert(
Task(
"Data processing", 2, 500));
284 cout <<
"Processing tasks by priority:" << endl;
289 cout <<
" " << order++ <<
". " << t << endl;
298 cout <<
"\n" << string(60,
'=') << endl;
299 cout <<
"Fibonacci Heap: Advanced Operations" << endl;
300 cout << string(60,
'=') << endl;
305 cout <<
"\n--- Insert and Decrease-Key ---" << endl;
307 vector<Fibonacci_Heap<int>::Node*> handles;
309 cout <<
"Inserting: ";
310 for (
int v : {50, 30, 80, 10, 90, 20, 70})
313 handles.push_back(fib_heap.
insert(v));
317 cout <<
"Current minimum: " << fib_heap.
get_min() << endl;
320 cout <<
"\nDecreasing 80 to 5..." << endl;
322 cout <<
"New minimum: " << fib_heap.
get_min() << endl;
325 cout <<
"Decreasing 90 to 3..." << endl;
327 cout <<
"New minimum: " << fib_heap.
get_min() << 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;
357 cout <<
" Heap 1 size: " << heap1.
size() << endl;
358 cout <<
" Heap 1 min: " << heap1.
get_min() << endl;
359 cout <<
" Heap 2 size: " << heap2.
size() <<
" (empty)" << endl;
361 cout <<
"\nExtracting merged heap: ";
372 cout <<
"\n" << string(60,
'=') << endl;
373 cout <<
"Practical Example: Event-Driven Simulation" << endl;
374 cout << string(60,
'=') << endl;
381 Event() : time(0), description(
"") {}
382 Event(
double t,
const string& d) : time(t), description(d) {}
384 bool operator<(
const Event& other)
const {
return time < other.time; }
390 event_queue.
insert(Event(1.5,
"Customer arrives"));
391 event_queue.
insert(Event(0.5,
"Open store"));
392 event_queue.
insert(Event(3.0,
"Customer leaves"));
393 event_queue.
insert(Event(2.0,
"Process payment"));
394 event_queue.
insert(Event(4.5,
"Delivery arrives"));
395 event_queue.
insert(Event(2.5,
"Answer phone"));
396 event_queue.
insert(Event(5.0,
"Close store"));
398 cout <<
"\nSimulation timeline:" << endl;
399 cout << string(40,
'-') << endl;
403 Event e = event_queue.
get();
404 cout << fixed << setprecision(1);
405 cout <<
" t=" << setw(4) << e.time <<
": " << e.description << endl;
414 cout <<
"\n" << string(60,
'=') << endl;
415 cout <<
"Performance Comparison (n = " << n <<
")" << endl;
416 cout << string(60,
'=') << endl;
420 uniform_int_distribution<> dis(1, n * 10);
422 vector<int> values(n);
423 for (
int i = 0; i < n; ++i)
424 values[i] = dis(gen);
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;
446 cout <<
" Insert " << n <<
" elements: " << insert_time <<
" us" << endl;
447 cout <<
" Extract all elements: " << extract_time <<
" us" << endl;
448 cout <<
" Total: " << (insert_time + extract_time) <<
" us" << 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;
471 cout <<
" Insert " << n <<
" elements: " << insert_time <<
" us" << endl;
472 cout <<
" Extract all elements: " << extract_time <<
" us" << endl;
473 cout <<
" Total: " << (insert_time + extract_time) <<
" us" << endl;
477 cout <<
"\n--- Decrease-Key Performance ---" << endl;
479 int decreases = n / 2;
480 vector<int> decrease_amounts(decreases);
481 for (
int i = 0; i < decreases; ++i)
482 decrease_amounts[i] = dis(gen);
487 vector<Fibonacci_Heap<int>::Node*> handles(n);
489 for (
int i = 0; i < n; ++i)
490 handles[i] = heap.
insert(values[i] + n * 10);
492 auto start = chrono::high_resolution_clock::now();
494 for (
int i = 0; i < decreases; ++i)
500 auto end = chrono::high_resolution_clock::now();
501 auto time = chrono::duration_cast<chrono::microseconds>(end - start).count();
503 cout <<
"\nFibonacci Heap decrease-key (" << decreases <<
" operations): " << time <<
" us" << endl;
504 cout <<
" Average per decrease-key: " << (time * 1000.0 / decreases) <<
" ns" << endl;
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;
520 cout << string(60,
'=') << endl;
525 cout <<
"\nInserting: ";
526 for (
int v : {5, 3, 8, 1, 9, 2, 7})
533 cout <<
"Maximum: " << max_heap.
top() << endl;
535 cout <<
"Extracting in descending order: ";
537 cout << max_heap.
get() <<
" ";
543 cout <<
"\nFibonacci Max-Heap: ";
544 for (
int v : {15, 13, 18, 11, 19, 12, 17})
551 cout <<
"Maximum: " << fib_max.
get_min() << endl;
552 cout <<
"Extracting: ";
558int main(
int argc,
char* argv[])
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);
587 cmd.parse(argc, argv);
589 int n = nArg.getValue();
590 bool runBasic = basicArg.getValue();
591 bool runFib = fibArg.getValue();
592 bool runSim = simArg.getValue();
593 bool runPerf = perfArg.getValue();
594 bool runMax = maxArg.getValue();
595 bool runAll = allArg.getValue();
597 if (not runBasic
and not runFib
and not runSim
and not runPerf
and not runMax)
600 cout <<
"=== Priority Queues: Binary Heap vs Fibonacci Heap ===" << endl;
602 if (runAll or runBasic)
605 if (runAll or runFib)
608 if (runAll or runSim)
611 if (runAll or runMax)
614 if (runAll or runPerf)
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 get()
Alias for getMin().
T & insert(const T &item)
Insert a copy of item into the heap.
const T & top() const
Return a reference to the smallest element.
Implementation of a Fibonacci Heap priority queue.
const T & get_min() const
Returns the minimum element without removing it.
Node * insert(const T &val)
Inserts a new element (copy).
void merge(Fibonacci_Heap &other)
Merges another heap into this one.
size_t size() const noexcept
Returns the number of elements in the heap.
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
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.
and
Check uniqueness with explicit hash + equality functors.
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
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.