-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst_usage_example.cpp
More file actions
118 lines (94 loc) · 3.95 KB
/
mst_usage_example.cpp
File metadata and controls
118 lines (94 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @file mst_usage_example.cpp
* @brief Example demonstrating the enhanced MST API with return values and validation
*/
#include "graph/algorithm/mst.hpp"
#include "graph/edge_list/edge_list_descriptor.hpp"
#include "graph/container/dynamic_graph.hpp"
#include <iostream>
#include <vector>
#include <format>
using namespace graph;
int main() {
// Example 1: Kruskal's algorithm with return values
std::cout << "=== Kruskal's Algorithm Example ===\n\n";
using Edge = edge_descriptor<uint32_t, int>;
std::vector<Edge> edges = {{0, 1, 4}, {1, 2, 8}, {2, 3, 7}, {3, 0, 9}, {0, 2, 2}, {1, 3, 5}};
std::cout << "Input edges (source, target, weight):\n";
for (const auto& e : edges) {
std::cout << std::format(" ({}, {}, {})\n", e.source_id, e.target_id, e.value);
}
std::vector<Edge> mst;
auto [total_weight, num_components] = kruskal(edges, mst);
std::cout << "\nMinimum Spanning Tree:\n";
for (const auto& e : mst) {
std::cout << std::format(" ({}, {}, {})\n", e.source_id, e.target_id, e.value);
}
std::cout << std::format("\nTotal MST weight: {}\n", total_weight);
std::cout << std::format("Number of components: {}\n", num_components);
// Example 2: Disconnected graph
std::cout << "\n=== Disconnected Graph Example ===\n\n";
std::vector<Edge> disconnected_edges = {
{0, 1, 1},
{1, 2, 2}, // First component
{3, 4, 3},
{4, 5, 4} // Second component
};
std::vector<Edge> forest;
auto [forest_weight, components] = kruskal(disconnected_edges, forest);
std::cout << std::format("Spanning Forest:\n");
std::cout << std::format(" Total weight: {}\n", forest_weight);
std::cout << std::format(" Components: {}\n", components);
std::cout << std::format(" Edges in forest: {}\n", forest.size());
// Example 3: Maximum Spanning Tree
std::cout << "\n=== Maximum Spanning Tree Example ===\n\n";
std::vector<Edge> max_edges = {{0, 1, 4}, {1, 2, 8}, {0, 2, 2}};
std::vector<Edge> max_st;
auto [max_weight, _] = kruskal(max_edges, max_st, std::greater<int>{});
std::cout << "Maximum Spanning Tree:\n";
for (const auto& e : max_st) {
std::cout << std::format(" ({}, {}, {})\n", e.source_id, e.target_id, e.value);
}
std::cout << std::format("Total weight: {}\n", max_weight);
// Example 4: Prim's algorithm with validation
std::cout << "\n=== Prim's Algorithm Example ===\n\n";
using Graph = container::dynamic_graph<int, void, void, uint32_t, false, container::vov_graph_traits<int>>;
// Create undirected weighted graph
Graph g({{0, 1, 4}, {1, 0, 4}, {1, 2, 8}, {2, 1, 8}, {2, 0, 11}, {0, 2, 11}, {0, 2, 2}, {2, 0, 2}});
size_t n = num_vertices(g);
std::vector<uint32_t> predecessor(n);
std::vector<int> weight(n);
try {
init_shortest_paths(g, weight, predecessor);
auto total_wt = prim(g, 0, predecessor, weight);
std::cout << "MST from vertex 0:\n";
std::cout << std::format(" Total weight: {}\n", total_wt);
std::cout << "\n MST edges (predecessor -> vertex: weight):\n";
for (size_t v = 0; v < n; ++v) {
if (v != 0 && predecessor[v] != v) {
std::cout << std::format(" {} -> {}: {}\n", predecessor[v], v, weight[v]);
}
}
} catch (const std::out_of_range& e) {
std::cout << "Error: " << e.what() << "\n";
}
// Example 5: Demonstrate input validation
std::cout << "\n=== Input Validation Example ===\n\n";
try {
std::vector<uint32_t> small_pred(2); // Too small!
std::vector<int> small_wt(2);
init_shortest_paths(g, small_wt, small_pred);
prim(g, 0, small_pred, small_wt);
} catch (const std::out_of_range& e) {
std::cout << "Caught expected error:\n " << e.what() << "\n";
}
try {
std::vector<uint32_t> pred(n);
std::vector<int> wt(n);
init_shortest_paths(g, wt, pred);
prim(g, 999, pred, wt); // Invalid seed
} catch (const std::out_of_range& e) {
std::cout << "\nCaught expected error:\n " << e.what() << "\n";
}
return 0;
}