Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mst_example.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
197#include <iostream>
198#include <iomanip>
199#include <string>
200#include <chrono>
201#include <random>
202#include <tpl_graph.H>
203#include <Kruskal.H>
204#include <Prim.H>
205#include <tclap/CmdLine.h>
206
207using namespace std;
208using namespace Aleph;
209
210// Node: location name
212
213// Arc: cable length (cost)
215
216// Undirected graph for network design
218
219// Distance accessor
221{
223 static constexpr double Max_Distance = numeric_limits<double>::infinity();
224 static constexpr double Zero_Distance = 0.0;
225
226 double operator()(NetworkGraph::Arc* arc) const
227 {
228 return arc->get_info();
229 }
230};
231
249{
250 NetworkGraph g;
251
252 // Buildings
253 auto library = g.insert_node("Library");
254 auto admin = g.insert_node("AdminBldg");
255 auto gym = g.insert_node("Gym");
256 auto scilab = g.insert_node("SciLab");
257 auto mainhall = g.insert_node("MainHall");
258 auto dorm = g.insert_node("Dorm");
259 auto artstudio = g.insert_node("ArtStudio");
260 auto cafeteria = g.insert_node("Cafeteria");
261 auto theater = g.insert_node("Theater");
262
263 // Potential cable routes (with lengths in meters)
264 g.insert_arc(library, admin, 15);
265 g.insert_arc(admin, gym, 20);
266 g.insert_arc(library, scilab, 12);
268 g.insert_arc(gym, dorm, 25);
269 g.insert_arc(scilab, mainhall, 10);
270 g.insert_arc(mainhall, dorm, 18);
273 g.insert_arc(dorm, theater, 16);
276
277 // Additional cross-connections for more interesting MST
280 g.insert_arc(admin, dorm, 30);
281
282 return g;
283}
284
289 unsigned seed)
290{
291 NetworkGraph g;
292 if (num_nodes == 0)
293 return g;
294
295 if (num_edges < num_nodes - 1)
296 num_edges = num_nodes - 1;
297
298 mt19937 rng(seed);
300
301 // Create nodes
303 for (size_t i = 0; i < num_nodes; i++)
304 nodes.push_back(g.insert_node("N" + to_string(i)));
305
306 if (num_nodes == 1)
307 return g;
308
309 // First, create a spanning tree to ensure connectivity
310 for (size_t i = 1; i < num_nodes; i++)
311 {
313 size_t parent = parent_dist(rng);
314 g.insert_arc(nodes[parent], nodes[i], weight_dist(rng));
315 }
316
317 // Add remaining edges
318 size_t edges_to_add = num_edges - (num_nodes - 1);
320
321 for (size_t i = 0; i < edges_to_add; i++)
322 {
323 size_t a = node_dist(rng);
324 size_t b = node_dist(rng);
325 if (a != b)
327 }
328
329 return g;
330}
331
335void print_mst(NetworkGraph& tree, const string& algorithm)
336{
337 double total_weight = 0;
338
339 cout << "\n" << algorithm << " MST Edges:" << endl;
340 for (auto ait = tree.get_arc_it(); ait.has_curr(); ait.next())
341 {
342 auto* arc = ait.get_curr();
343 auto* src = tree.get_src_node(arc);
344 auto* tgt = tree.get_tgt_node(arc);
345 double weight = arc->get_info();
346 total_weight += weight;
347
348 cout << " " << setw(12) << left << src->get_info()
349 << " --- " << setw(5) << right << weight
350 << " --- " << tgt->get_info() << endl;
351 }
352
353 cout << "Total weight: " << total_weight << endl;
354 cout << "Edges in MST: " << tree.get_num_arcs() << endl;
355}
356
361{
362 auto start = chrono::high_resolution_clock::now();
363
365 kruskal(g, tree);
366
367 auto end = chrono::high_resolution_clock::now();
368 double ms = chrono::duration<double, milli>(end - start).count();
369
370 if (verbose)
371 print_mst(tree, "Kruskal's");
372
373 return ms;
374}
375
380{
381 auto start = chrono::high_resolution_clock::now();
382
384 prim(g, tree);
385
386 auto end = chrono::high_resolution_clock::now();
387 double ms = chrono::duration<double, milli>(end - start).count();
388
389 if (verbose)
390 print_mst(tree, "Prim's");
391
392 return ms;
393}
394
399{
400 double total = 0;
401 for (auto ait = tree.get_arc_it(); ait.has_curr(); ait.next())
402 total += ait.get_curr()->get_info();
403 return total;
404}
405
406int main(int argc, char* argv[])
407{
408 try
409 {
410 TCLAP::CmdLine cmd("Minimum Spanning Tree Example", ' ', "1.0");
411
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);
422
423 cmd.add(benchArg);
424 cmd.add(nodesArg);
425 cmd.add(edgesArg);
426 cmd.add(seedArg);
427 cmd.add(verboseArg);
428
429 cmd.parse(argc, argv);
430
431 bool benchmark = benchArg.getValue();
432 size_t num_nodes = nodesArg.getValue();
433 size_t num_edges = edgesArg.getValue();
434 unsigned seed = seedArg.getValue();
435 bool verbose = verboseArg.getValue();
436
437 cout << "=== Minimum Spanning Tree Algorithms ===" << endl;
438
439 if (!benchmark)
440 {
441 // Demo with small graph
442 cout << "\nBuilding campus network..." << endl;
444
445 cout << "Network has " << g.get_num_nodes() << " buildings and "
446 << g.get_num_arcs() << " potential cable routes." << endl;
447
448 // Run Kruskal
449 cout << "\n--- Kruskal's Algorithm ---" << endl;
450 cout << "Strategy: Sort edges, add if no cycle (uses Union-Find)" << endl;
451
453 double kruskal_time = run_kruskal(g, kruskal_tree, true);
454 cout << "Time: " << fixed << setprecision(3) << kruskal_time << " ms" << endl;
455
456 // Run Prim
457 cout << "\n--- Prim's Algorithm ---" << endl;
458 cout << "Strategy: Grow tree from start, always add cheapest edge" << endl;
459
461 double prim_time = run_prim(g, prim_tree, true);
462 cout << "Time: " << fixed << setprecision(3) << prim_time << " ms" << endl;
463
464 // Verify both give same total weight
467
468 cout << "\n--- Verification ---" << endl;
469 cout << "Kruskal MST weight: " << kruskal_weight << endl;
470 cout << "Prim MST weight: " << prim_weight << endl;
471
472 if (abs(kruskal_weight - prim_weight) < 1e-9)
473 cout << "Both algorithms found optimal MST!" << endl;
474 else
475 cout << "Warning: Weights differ (may have multiple optimal MSTs)" << endl;
476 }
477 else
478 {
479 // Performance benchmark
480 cout << "\nGenerating random graph with " << num_nodes << " nodes and "
481 << num_edges << " edges..." << endl;
482
484 cout << "Graph created: " << g.get_num_nodes() << " nodes, "
485 << g.get_num_arcs() << " edges" << endl;
486
487 // Benchmark Kruskal
490
491 // Benchmark Prim
493 double prim_time = run_prim(g, prim_tree, verbose);
494
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
504 << setw(15) << setprecision(2) << mst_total_weight(prim_tree) << endl;
505
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 << " (";
510
511 double ratio = (double)num_edges / num_nodes;
512 if (ratio < 2)
513 cout << "sparse graph -> Kruskal recommended";
514 else if (ratio > 10)
515 cout << "dense graph -> Prim recommended";
516 else
517 cout << "medium density -> similar performance";
518 cout << ")" << endl;
519 }
520
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;
524
525 return 0;
526 }
527 catch (TCLAP::ArgException& e)
528 {
529 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
530 return 1;
531 }
532}
533
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
int main()
int num_nodes
Definition btreepic.C:410
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
Definition Kruskal.H:90
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:433
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
Calcula el árbol abarcador mínimo de un grafo según el algoritmo de Prim.
Definition Prim.H:214
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:2802
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
static mt19937 rng
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4051
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
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.
Definition ah-arena.H:89
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
bool verbose
Flag enabling verbose output.
Definition ahDefs.C:45
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:121
double Distance_Type
static constexpr double Zero_Distance
static constexpr double Max_Distance
double operator()(NetworkGraph::Arc *arc) const
Generic graph and digraph implementations.