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;
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;
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: ";
331 cout <<
fib_heap.extract_min() <<
" ";
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: ";
363 cout <<
heap1.extract_min() <<
" ";
372 cout <<
"\n" << string(60,
'=') <<
endl;
373 cout <<
"Practical Example: Event-Driven Simulation" <<
endl;
374 cout << string(60,
'=') <<
endl;
394 event_queue.insert(Event(4.5,
"Delivery arrives"));
398 cout <<
"\nSimulation timeline:" <<
endl;
399 cout << string(40,
'-') <<
endl;
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;
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;
446 cout <<
" Insert " << n <<
" elements: " <<
insert_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;
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();
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})
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: ";
554 cout <<
fib_max.extract_min() <<
" ";
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.
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).
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.