-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark.cpp
More file actions
295 lines (259 loc) · 10.9 KB
/
Copy pathBenchmark.cpp
File metadata and controls
295 lines (259 loc) · 10.9 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "Benchmark.h"
#include "MST.h"
#include "Traversal.h"
#include "ShortestPath.h"
#include "MaxFlow.h"
#include "TSP.h"
#include "SCC.h"
#include "Centrality.h"
#include "Colors.h"
#include <iostream>
#include <iomanip>
#include <chrono>
#include <random>
#include <algorithm>
#include <sstream>
using Clock = std::chrono::steady_clock;
static long long ms(Clock::time_point t0) {
return std::chrono::duration_cast<std::chrono::milliseconds>(
Clock::now() - t0).count();
}
// -----------------------------------------------------------------------------
// Random connected graph generator
// Guarantees connectivity by first building a random spanning tree (n-1 edges),
// then appending extra random edges.
// -----------------------------------------------------------------------------
Graph Benchmark::generateRandom(int n, int extraEdges, int seed) {
std::mt19937 rng(seed);
Graph g(n, false);
// Spanning tree via random Prüfer-like insertion
std::vector<int> inTree = {0};
std::vector<int> notIn;
for (int i = 1; i < n; i++) notIn.push_back(i);
while (!notIn.empty()) {
std::uniform_int_distribution<int> inDist(0, (int)inTree.size() - 1);
std::uniform_int_distribution<int> outDist(0, (int)notIn.size() - 1);
int u = inTree[inDist(rng)];
int vi = outDist(rng);
int v = notIn[vi];
notIn.erase(notIn.begin() + vi);
inTree.push_back(v);
std::uniform_int_distribution<int> wDist(10, 200);
std::uniform_int_distribution<int> rDist(0, 10);
g.addEdge(u, v, wDist(rng), rDist(rng), true);
}
// Extra random edges
std::uniform_int_distribution<int> nodeDist(0, n - 1);
std::uniform_int_distribution<int> wDist(10, 200);
std::uniform_int_distribution<int> rDist(0, 10);
int added = 0, attempts = 0;
while (added < extraEdges && attempts < extraEdges * 10) {
int u = nodeDist(rng), v = nodeDist(rng);
attempts++;
if (u == v) continue;
g.addEdge(u, v, wDist(rng), rDist(rng), true);
added++;
}
// Name nodes
for (int i = 0; i < n; i++)
g.setName(i, "B" + std::to_string(i));
return g;
}
// -----------------------------------------------------------------------------
// Run all benchmarks
// -----------------------------------------------------------------------------
struct BenchRow {
int n, e;
std::string algo;
std::string theory;
long long timeMs;
std::string result;
};
void Benchmark::runAll() {
std::cout << "\n " << CLR_BORDER << "+" << std::string(72, '=') << "+" << CLR_RESET << "\n";
std::cout << " " << CLR_BORDER << "| " << CLR_RESULT
<< std::left << std::setw(70)
<< "ALGORITHM BENCHMARK - Empirical vs Theoretical Complexity"
<< CLR_BORDER << "|" << CLR_RESET << "\n";
std::cout << " " << CLR_BORDER << "+" << std::string(72, '=') << "+" << CLR_RESET << "\n";
std::cout << " " << CLR_RESULT
<< " Generating random connected graphs with seed=42...\n"
<< " (highlighted rows >100 ms flag algorithms that may bottleneck large inputs)\n"
<< CLR_RESET;
std::vector<BenchRow> rows;
// Graph sizes: n=10, 50, 100, 200 with ~2n edges (sparse)
const int sizes[] = {10, 50, 100, 200};
const int extraEdge[] = {10, 50, 100, 200};
for (int si = 0; si < 4; si++) {
int n = sizes[si];
int extra = extraEdge[si];
Graph g = generateRandom(n, extra, 42);
int E = static_cast<int>(g.edgeList.size());
// 1. Kruskal MST
{
auto t0 = Clock::now();
MSTResult r = MST::kruskal(g);
long long t = ms(t0);
rows.push_back({n, E, "Kruskal MST", "O(E log E)",
t, r.valid ? "w=" + std::to_string(r.totalWeight) : "disconn"});
}
// 2. Prim MST
{
auto t0 = Clock::now();
MSTResult r = MST::prim(g, 0);
long long t = ms(t0);
rows.push_back({n, E, "Prim MST", "O((V+E) log V)",
t, r.valid ? "w=" + std::to_string(r.totalWeight) : "disconn"});
}
// 3. BFS
{
std::vector<int> par(n, -1);
auto t0 = Clock::now();
auto ord = Traversal::bfs(g, 0, par);
long long t = ms(t0);
rows.push_back({n, E, "BFS", "O(V + E)",
t, std::to_string(ord.size()) + " visited"});
}
// 4. DFS
{
std::vector<int> par(n, -1);
auto t0 = Clock::now();
auto ord = Traversal::dfs(g, 0, par);
long long t = ms(t0);
rows.push_back({n, E, "DFS", "O(V + E)",
t, std::to_string(ord.size()) + " visited"});
}
// 5. Dijkstra
{
auto t0 = Clock::now();
auto r = ShortestPath::dijkstra(g, 0);
long long t = ms(t0);
int reach = 0;
for (int i = 1; i < n; i++) if (r.dist[i] < INF) reach++;
rows.push_back({n, E, "Dijkstra", "O((V+E) log V)",
t, std::to_string(reach) + " paths"});
}
// 6. Bellman-Ford
{
auto t0 = Clock::now();
auto r = ShortestPath::bellmanFord(g, 0);
long long t = ms(t0);
rows.push_back({n, E, "Bellman-Ford", "O(V * E)",
t, r.hasNegCycle ? "neg-cycle" : "ok"});
}
// 7. Floyd-Warshall (skip n=200 to avoid multi-second run in demo)
if (n <= 100) {
auto t0 = Clock::now();
auto r = ShortestPath::floydWarshall(g);
long long t = ms(t0);
rows.push_back({n, E, "Floyd-Warshall", "O(V^3)",
t, r.hasNegCycle ? "neg-cycle" : "all-pairs"});
} else {
rows.push_back({n, E, "Floyd-Warshall", "O(V^3)", -1, "skipped (n>100)"});
}
// 8. Edmonds-Karp Max Flow
{
auto t0 = Clock::now();
auto r = MaxFlow::edmondsKarp(g, 0, n - 1);
long long t = ms(t0);
rows.push_back({n, E, "Edmonds-Karp", "O(V * E^2)",
t, "flow=" + std::to_string(r.maxFlow)});
}
// 9. TSP Nearest Neighbor
{
auto t0 = Clock::now();
auto r = TSP::nearestNeighbor(g, 0);
long long t = ms(t0);
rows.push_back({n, E, "TSP NearNeighbor", "O(V^2)",
t, r.found ? "cost=" + std::to_string(r.cost) : "no circuit"});
}
// 10. TSP Brute Force (only n<=10 — O(n!) is lethal beyond that)
if (n <= 10) {
auto t0 = Clock::now();
auto r = TSP::bruteForce(g, 0);
long long t = ms(t0);
rows.push_back({n, E, "TSP BruteForce", "O(V!)",
t, r.found ? "cost=" + std::to_string(r.cost) : "no circuit"});
} else {
rows.push_back({n, E, "TSP BruteForce", "O(V!)", -1,
"skipped (n>" + std::to_string(10) + ")"});
}
// 11. SCC / Connected Components
{
auto t0 = Clock::now();
auto r = SCC::analyze(g);
long long t = ms(t0);
rows.push_back({n, E, "SCC (Tarjan's)", "O(V + E)",
t, std::to_string(r.numComponents) + " component(s)"});
}
// 12. Centrality (skip n=200 — O(V*(V+E)logV) is significant)
if (n <= 100) {
auto t0 = Clock::now();
auto r = Centrality::analyze(g);
long long t = ms(t0);
// Find most central node
int top = 0;
for (int i = 1; i < n; i++)
if (r.composite[i] > r.composite[top]) top = i;
rows.push_back({n, E, "Centrality", "O(V*(V+E)logV)",
t, "top=[" + std::to_string(top) + "]"});
} else {
rows.push_back({n, E, "Centrality", "O(V*(V+E)logV)", -1, "skipped (n>100)"});
}
}
// Replace -1 times with marker
for (auto& r : rows)
if (r.timeMs < 0) r.timeMs = -1; // printed as "---"
// Custom print to handle -1
std::cout << "\n " << CLR_BORDER
<< "+======+======+====================+===================+========+==================+"
<< CLR_RESET << "\n";
std::cout << " " << CLR_BORDER << "| "
<< CLR_RESULT << std::left
<< std::setw(5) << "V"
<< std::setw(6) << "E"
<< std::setw(21) << "Algorithm"
<< std::setw(20) << "Complexity"
<< std::setw(9) << "ms"
<< std::setw(18) << "Result"
<< CLR_BORDER << "|" << CLR_RESET << "\n";
std::cout << " " << CLR_BORDER
<< "+------+------+--------------------+-------------------+--------+------------------+"
<< CLR_RESET << "\n";
int prevN = -1;
for (const auto& r : rows) {
if (r.n != prevN && prevN != -1) {
std::cout << " " << CLR_BORDER
<< "+------+------+--------------------+-------------------+--------+------------------+"
<< CLR_RESET << "\n";
}
prevN = r.n;
std::string algoStr = r.algo.size() > 19 ? r.algo.substr(0, 19) : r.algo;
std::string theoryStr = r.theory.size() > 18 ? r.theory.substr(0, 18) : r.theory;
std::string resStr = r.result.size() > 17 ? r.result.substr(0, 17) : r.result;
const char* col = (r.timeMs > 100) ? CLR_WARN :
(r.timeMs < 0) ? CLR_BORDER : CLR_RESULT;
std::string timeStr = (r.timeMs < 0) ? "---" : std::to_string(r.timeMs);
std::cout << " " << CLR_BORDER << "| " << col
<< std::left
<< std::setw(5) << r.n
<< std::setw(6) << r.e
<< std::setw(21) << algoStr
<< std::setw(20) << theoryStr
<< std::setw(9) << timeStr
<< std::setw(18) << resStr
<< CLR_BORDER << "|" << CLR_RESET << "\n";
}
std::cout << " " << CLR_BORDER
<< "+======+======+====================+===================+========+==================+"
<< CLR_RESET << "\n";
std::cout << "\n " << CLR_RESULT
<< " Key observations:\n"
<< " O(V!) : TSP Brute Force explodes — 12! = 479M ops\n"
<< " O(V^3) : Floyd-Warshall scales cubically — costly at V=200\n"
<< " O(V*E^2) : Edmonds-Karp is acceptable for sparse graphs\n"
<< " O((V+E)logV): Dijkstra/Prim/A* are fast even at V=200\n"
<< " O(V+E) : BFS/DFS/Tarjan SCC are near-linear — always cheap"
<< CLR_RESET << "\n";
std::cout << " " << CLR_BORDER << std::string(74, '-') << CLR_RESET << "\n";
}