|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Minimum Spanning Tree algorithms: Kruskal and Prim. More...
#include <iostream>#include <iomanip>#include <string>#include <chrono>#include <random>#include <tpl_graph.H>#include <Kruskal.H>#include <Prim.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Classes | |
| struct | CableLength |
Typedefs | |
| using | LocationNode = Graph_Node< string > |
| using | CableArc = Graph_Arc< double > |
| using | NetworkGraph = List_Graph< LocationNode, CableArc > |
Functions | |
| NetworkGraph | build_campus_network () |
| Build a sample network graph. | |
| NetworkGraph | generate_random_graph (size_t num_nodes, size_t num_edges, unsigned seed) |
| Generate a random graph for performance testing. | |
| void | print_mst (NetworkGraph &tree, const string &algorithm) |
| Print MST result. | |
| double | run_kruskal (NetworkGraph &g, NetworkGraph &tree, bool verbose) |
| Run Kruskal's algorithm. | |
| double | run_prim (NetworkGraph &g, NetworkGraph &tree, bool verbose) |
| Run Prim's algorithm. | |
| double | mst_total_weight (NetworkGraph &tree) |
| Calculate total MST weight. | |
| int | main (int argc, char *argv[]) |
Minimum Spanning Tree algorithms: Kruskal and Prim.
This example demonstrates two classic greedy algorithms for finding the Minimum Spanning Tree (MST) of a weighted undirected graph. Both algorithms are optimal and produce an MST with the same minimum total weight; when multiple optimal MSTs exist, the chosen edge set can differ. The algorithms differ in their approach and performance characteristics.
Given a connected, undirected graph with weighted edges, a spanning tree is:
A minimum spanning tree is the spanning tree with minimum total edge weight.
Key properties:
Strategy: Greedily add edges in order of increasing weight
Algorithm:
Key insight: Add smallest edge that doesn't create cycle
Data structures:
Time complexity: O(E log E) = O(E log V)
Space complexity: O(V) for Union-Find
Best for: Sparse graphs (E ≈ V)
Strategy: Grow MST from a starting vertex, always adding minimum edge
Algorithm:
Key insight: Always add cheapest edge connecting current MST to outside
Data structures:
Time complexity:
Space complexity: O(V) for priority queue
Best for: Dense graphs (E ≈ V²)
| Algorithm | Time (Binary Heap) | Time (Fibonacci Heap) | Best For |
|---|---|---|---|
| Kruskal | O(E log E) | O(E log E) | Sparse (E ≈ V) |
| Prim | O(E log V) | O(E + V log V) | Dense (E ≈ V²) |
Note: For sparse graphs, Kruskal is often faster. For dense graphs, Prim with Fibonacci heap is better.
✅ Graph is sparse (few edges) ✅ Edges already sorted (or sorting is cheap) ✅ Simple implementation preferred ✅ Parallel processing needed (edges independent)
✅ Graph is dense (many edges) ✅ Have good priority queue implementation ✅ Need to start from specific vertex ✅ Graph represented as adjacency matrix
Problem: Connect 5 cities with minimum cost
MST solution: Connect with minimum total cost
Definition in file mst_example.C.
Definition at line 214 of file mst_example.C.
| using LocationNode = Graph_Node<string> |
Definition at line 211 of file mst_example.C.
| using NetworkGraph = List_Graph<LocationNode, CableArc> |
Definition at line 217 of file mst_example.C.
| NetworkGraph build_campus_network | ( | ) |
Build a sample network graph.
Represents buildings that need to be connected with network cables. Edge weights represent cable lengths (cost).
Library ----15---- AdminBldg ----20---- Gym | | | 12 8 25 | | | SciLab -----10----- MainHall ----18---- Dorm | | | 22 14 16 | | | ArtStudio ---9---- Cafeteria ---11--- Theater
Definition at line 248 of file mst_example.C.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and Aleph::maps().
Referenced by main().
| NetworkGraph generate_random_graph | ( | size_t | num_nodes, |
| size_t | num_edges, | ||
| unsigned | seed | ||
| ) |
Generate a random graph for performance testing.
Definition at line 288 of file mst_example.C.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::maps(), nodes, num_nodes, rng, and Aleph::to_string().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 406 of file mst_example.C.
References abs(), build_campus_network(), generate_random_graph(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::maps(), mst_total_weight(), num_nodes, run_kruskal(), run_prim(), and Aleph::verbose.
| double mst_total_weight | ( | NetworkGraph & | tree | ) |
Calculate total MST weight.
Definition at line 398 of file mst_example.C.
References GraphCommon< GT, Node, Arc >::get_arc_it(), and Aleph::maps().
Referenced by main().
| void print_mst | ( | NetworkGraph & | tree, |
| const string & | algorithm | ||
| ) |
Print MST result.
Definition at line 335 of file mst_example.C.
References GraphCommon< GT, Node, Arc >::get_arc_it(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), and Aleph::maps().
Referenced by run_kruskal(), and run_prim().
| double run_kruskal | ( | NetworkGraph & | g, |
| NetworkGraph & | tree, | ||
| bool | verbose | ||
| ) |
Run Kruskal's algorithm.
Definition at line 360 of file mst_example.C.
References Aleph::maps(), print_mst(), and Aleph::verbose.
Referenced by main().
| double run_prim | ( | NetworkGraph & | g, |
| NetworkGraph & | tree, | ||
| bool | verbose | ||
| ) |
Run Prim's algorithm.
Definition at line 379 of file mst_example.C.
References Aleph::maps(), print_mst(), and Aleph::verbose.
Referenced by main().