205#include <tclap/CmdLine.h>
208using namespace Aleph;
223 static constexpr double Max_Distance = numeric_limits<double>::infinity();
228 return arc->get_info();
299 uniform_real_distribution<double> weight_dist(1.0, 100.0);
302 vector<NetworkGraph::Node*>
nodes;
312 uniform_int_distribution<size_t> parent_dist(0, i - 1);
313 size_t parent = parent_dist(
rng);
318 size_t edges_to_add = num_edges - (
num_nodes - 1);
319 uniform_int_distribution<size_t> node_dist(0,
num_nodes - 1);
321 for (
size_t i = 0; i < edges_to_add; i++)
323 size_t a = node_dist(
rng);
324 size_t b = node_dist(
rng);
337 double total_weight = 0;
339 cout <<
"\n" << algorithm <<
" MST Edges:" << endl;
340 for (
auto ait = tree.
get_arc_it(); ait.has_curr(); ait.next())
342 auto* arc = ait.get_curr();
345 double weight = arc->get_info();
346 total_weight += weight;
348 cout <<
" " << setw(12) << left << src->get_info()
349 <<
" --- " << setw(5) << right << weight
350 <<
" --- " << tgt->get_info() << endl;
353 cout <<
"Total weight: " << total_weight << endl;
354 cout <<
"Edges in MST: " << tree.
get_num_arcs() << endl;
362 auto start = chrono::high_resolution_clock::now();
367 auto end = chrono::high_resolution_clock::now();
368 double ms = chrono::duration<double, milli>(end - start).count();
381 auto start = chrono::high_resolution_clock::now();
386 auto end = chrono::high_resolution_clock::now();
387 double ms = chrono::duration<double, milli>(end - start).count();
401 for (
auto ait = tree.
get_arc_it(); ait.has_curr(); ait.next())
402 total += ait.get_curr()->get_info();
406int main(
int argc,
char* argv[])
410 TCLAP::CmdLine
cmd(
"Minimum Spanning Tree Example",
' ',
"1.0");
412 TCLAP::SwitchArg benchArg(
"b",
"benchmark",
413 "Run performance benchmark",
false);
414 TCLAP::ValueArg<size_t> nodesArg(
"n",
"nodes",
415 "Number of nodes for benchmark",
false, 1000,
"size_t");
416 TCLAP::ValueArg<size_t> edgesArg(
"e",
"edges",
417 "Number of edges for benchmark",
false, 5000,
"size_t");
418 TCLAP::ValueArg<unsigned> seedArg(
"s",
"seed",
419 "Random seed",
false, 42,
"unsigned");
420 TCLAP::SwitchArg verboseArg(
"v",
"verbose",
421 "Show detailed output",
false);
429 cmd.parse(argc, argv);
431 bool benchmark = benchArg.getValue();
433 size_t num_edges = edgesArg.getValue();
434 unsigned seed = seedArg.getValue();
435 bool verbose = verboseArg.getValue();
437 cout <<
"=== Minimum Spanning Tree Algorithms ===" << endl;
442 cout <<
"\nBuilding campus network..." << endl;
445 cout <<
"Network has " << g.
get_num_nodes() <<
" buildings and "
446 << g.
get_num_arcs() <<
" potential cable routes." << endl;
449 cout <<
"\n--- Kruskal's Algorithm ---" << endl;
450 cout <<
"Strategy: Sort edges, add if no cycle (uses Union-Find)" << endl;
453 double kruskal_time =
run_kruskal(g, kruskal_tree,
true);
454 cout <<
"Time: " << fixed << setprecision(3) << kruskal_time <<
" ms" << endl;
457 cout <<
"\n--- Prim's Algorithm ---" << endl;
458 cout <<
"Strategy: Grow tree from start, always add cheapest edge" << endl;
461 double prim_time =
run_prim(g, prim_tree,
true);
462 cout <<
"Time: " << fixed << setprecision(3) << prim_time <<
" ms" << endl;
468 cout <<
"\n--- Verification ---" << endl;
469 cout <<
"Kruskal MST weight: " << kruskal_weight << endl;
470 cout <<
"Prim MST weight: " << prim_weight << endl;
472 if (
abs(kruskal_weight - prim_weight) < 1e-9)
473 cout <<
"Both algorithms found optimal MST!" << endl;
475 cout <<
"Warning: Weights differ (may have multiple optimal MSTs)" << endl;
480 cout <<
"\nGenerating random graph with " <<
num_nodes <<
" nodes and "
481 << num_edges <<
" edges..." << endl;
495 cout <<
"\n--- Performance Results ---" << endl;
496 cout << setw(15) <<
"Algorithm" << setw(15) <<
"Time (ms)"
497 << setw(15) <<
"MST Weight" << endl;
498 cout << string(45,
'-') << endl;
499 cout << setw(15) <<
"Kruskal"
500 << setw(15) << fixed << setprecision(3) << kruskal_time
502 cout << setw(15) <<
"Prim"
503 << setw(15) << fixed << setprecision(3) << prim_time
506 cout <<
"\n--- Complexity Analysis ---" << endl;
507 cout <<
"V = " <<
num_nodes <<
", E = " << num_edges << endl;
508 cout <<
"E/V ratio: " << fixed << setprecision(2)
509 << (double)num_edges /
num_nodes <<
" (";
511 double ratio = (double)num_edges /
num_nodes;
513 cout <<
"sparse graph -> Kruskal recommended";
515 cout <<
"dense graph -> Prim recommended";
517 cout <<
"medium density -> similar performance";
521 cout <<
"\n=== Algorithm Summary ===" << endl;
522 cout <<
"Kruskal: O(E log E), best for sparse graphs" << endl;
523 cout <<
"Prim: O(E log V), best for dense graphs" << endl;
527 catch (TCLAP::ArgException& e)
529 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Computes the minimum spanning tree of a graph using Prim's algorithm.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
DynArray< Graph::Node * > nodes
double mst_total_weight(NetworkGraph &tree)
Calculate total MST weight.
NetworkGraph build_campus_network()
Build a sample network graph.
double run_kruskal(NetworkGraph &g, NetworkGraph &tree, bool verbose)
Run Kruskal's algorithm.
NetworkGraph generate_random_graph(size_t num_nodes, size_t num_edges, unsigned seed)
Generate a random graph for performance testing.
double run_prim(NetworkGraph &g, NetworkGraph &tree, bool verbose)
Run Prim's algorithm.
void print_mst(NetworkGraph &tree, const string &algorithm)
Print MST result.
Main namespace for Aleph-w library functions.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
bool verbose
Flag enabling verbose output.
Node belonging to a graph implemented with a double linked adjacency list.
static constexpr double Zero_Distance
static constexpr double Max_Distance
double operator()(NetworkGraph::Arc *arc) const
Generic graph and digraph implementations.