205#include <tclap/CmdLine.h>
208using namespace Aleph;
223 static constexpr double Max_Distance = numeric_limits<double>::infinity();
228 return arc->get_info();
337 double total_weight = 0;
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;
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();
402 total +=
ait.get_curr()->get_info();
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");
421 "Show detailed output",
false);
433 size_t num_edges =
edgesArg.getValue();
437 cout <<
"=== Minimum Spanning Tree Algorithms ===" <<
endl;
442 cout <<
"\nBuilding campus network..." <<
endl;
445 cout <<
"Network has " << g.
get_num_nodes() <<
" buildings and "
449 cout <<
"\n--- Kruskal's Algorithm ---" <<
endl;
450 cout <<
"Strategy: Sort edges, add if no cycle (uses Union-Find)" <<
endl;
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;
462 cout <<
"Time: " << fixed << setprecision(3) <<
prim_time <<
" ms" <<
endl;
468 cout <<
"\n--- Verification ---" <<
endl;
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"
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)
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)
Calcula el árbol abarcador mínimo de un grafo según el algoritmo de Prim.
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_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
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.
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::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.