-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_dijkstra_shortest_paths.cpp
More file actions
723 lines (581 loc) · 29.6 KB
/
Copy pathtest_dijkstra_shortest_paths.cpp
File metadata and controls
723 lines (581 loc) · 29.6 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/**
* @file test_dijkstra_shortest_paths.cpp
* @brief Tests for Dijkstra's shortest path algorithms from dijkstra_shortest_paths.hpp
*/
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <graph/algorithm/dijkstra_shortest_paths.hpp>
#include "../common/graph_fixtures.hpp"
#include "../common/algorithm_test_types.hpp"
using namespace graph;
using namespace graph::adj_list;
using namespace graph::test;
using namespace graph::test::fixtures;
using namespace graph::test::algorithm;
// Simple visitor to count events (needs to be at namespace scope for member templates)
struct CountingVisitor {
int vertices_discovered = 0;
int vertices_examined = 0;
int edges_relaxed = 0;
int edges_not_relaxed = 0;
template <typename G, typename T>
void on_discover_vertex(const G&, const T&) {
++vertices_discovered;
}
template <typename G, typename T>
void on_examine_vertex(const G&, const T&) {
++vertices_examined;
}
template <typename G, typename T>
void on_edge_relaxed(const G&, const T&) {
++edges_relaxed;
}
template <typename G, typename T>
void on_edge_not_relaxed(const G&, const T&) {
++edges_not_relaxed;
}
};
TEST_CASE("dijkstra_shortest_paths - CLRS example", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Validate against known results from CLRS Figure 24.6
REQUIRE(distance[0] == clrs_dijkstra_results::distances_from_0[0]); // s: 0
REQUIRE(distance[1] == clrs_dijkstra_results::distances_from_0[1]); // t: 8
REQUIRE(distance[2] == clrs_dijkstra_results::distances_from_0[2]); // x: 9
REQUIRE(distance[3] == clrs_dijkstra_results::distances_from_0[3]); // y: 5
REQUIRE(distance[4] == clrs_dijkstra_results::distances_from_0[4]); // z: 7
}
TEST_CASE("dijkstra_shortest_paths - path graph", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = path_graph_4_weighted<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Path: 0 -> 1 -> 2 -> 3 with weight 1 each
for (size_t i = 0; i < path_graph_4_results::num_vertices; ++i) {
REQUIRE(distance[i] == path_graph_4_results::distances[i]);
}
}
TEST_CASE("dijkstra_shortest_distances - no predecessors", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distance(num_vertices(g));
init_shortest_paths(g, distance);
// Test distances-only variant (no predecessor tracking)
dijkstra_shortest_distances(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Validate distances match expected results
REQUIRE(distance[0] == clrs_dijkstra_results::distances_from_0[0]); // s: 0
REQUIRE(distance[1] == clrs_dijkstra_results::distances_from_0[1]); // t: 8
REQUIRE(distance[2] == clrs_dijkstra_results::distances_from_0[2]); // x: 9
REQUIRE(distance[3] == clrs_dijkstra_results::distances_from_0[3]); // y: 5
REQUIRE(distance[4] == clrs_dijkstra_results::distances_from_0[4]); // z: 7
}
TEST_CASE("dijkstra_shortest_paths - multi-source", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
// Start from vertices 0 and 3
std::vector<vertex_id_t<Graph>> sources = {0, 3};
dijkstra_shortest_paths(g, sources,
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Both source vertices should have distance 0
REQUIRE(distance[0] == 0);
REQUIRE(distance[3] == 0);
// Other distances should be minimum from either source
REQUIRE(distance[1] <= 8); // Can reach from source 0 or 3
REQUIRE(distance[4] <= 7); // Can reach from source 0 or 3
}
TEST_CASE("dijkstra_shortest_distances - multi-source", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distance(num_vertices(g));
init_shortest_paths(g, distance);
// Start from vertices 0 and 3
std::vector<vertex_id_t<Graph>> sources = {0, 3};
dijkstra_shortest_distances(g, sources,
container_value_fn(distance),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Both source vertices should have distance 0
REQUIRE(distance[0] == 0);
REQUIRE(distance[3] == 0);
}
TEST_CASE("dijkstra_shortest_paths - with visitor", "[algorithm][dijkstra_shortest_paths][visitor]") {
using Graph = vov_weighted;
auto g = path_graph_4_weighted<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
CountingVisitor visitor;
dijkstra_shortest_paths(
g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }, visitor);
// Verify visitor was called (should have discovered all 4 vertices, examined them, and relaxed edges)
REQUIRE(visitor.vertices_discovered == 4);
REQUIRE(visitor.vertices_examined == 4);
REQUIRE(visitor.edges_relaxed == 3); // 3 edges in path graph
}
TEST_CASE("dijkstra_shortest_paths - unweighted graph (default weight)", "[algorithm][dijkstra_shortest_paths]") {
using Graph = std::vector<std::vector<int>>;
// Create simple unweighted graph: 0 -> 1 -> 2 -> 3
Graph g(4);
g[0].push_back(1);
g[1].push_back(2);
g[2].push_back(3);
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
// Use default weight function (returns 1 for all edges)
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor));
REQUIRE(distance[0] == 0);
REQUIRE(distance[1] == 1);
REQUIRE(distance[2] == 2);
REQUIRE(distance[3] == 3);
}
TEST_CASE("dijkstra_shortest_paths - predecessor path reconstruction", "[algorithm][dijkstra_shortest_paths]") {
using Graph = vov_weighted;
auto g = path_graph_4_weighted<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Reconstruct path from 0 to 3: should be 0 -> 1 -> 2 -> 3
std::vector<vertex_id_t<Graph>> path;
auto current = vertex_id_t<Graph>(3);
while (current != vertex_id_t<Graph>(0)) {
path.push_back(current);
current = predecessor[current];
}
path.push_back(vertex_id_t<Graph>(0));
std::reverse(path.begin(), path.end());
REQUIRE(path.size() == 4);
REQUIRE(path[0] == 0);
REQUIRE(path[1] == 1);
REQUIRE(path[2] == 2);
REQUIRE(path[3] == 3);
}
TEST_CASE("dijkstra_shortest_paths - unreachable vertices", "[algorithm][dijkstra_shortest_paths]") {
using Graph = std::vector<std::vector<int>>;
// Create disconnected graph: 0 -> 1, 2 -> 3 (0,1 and 2,3 are separate)
Graph g(4);
g[0].push_back(1);
g[2].push_back(3);
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor));
// Vertices 0 and 1 should be reachable
REQUIRE(distance[0] == 0);
REQUIRE(distance[1] == 1);
// Vertices 2 and 3 should be unreachable (infinite distance)
REQUIRE(distance[2] == std::numeric_limits<int>::max());
REQUIRE(distance[3] == std::numeric_limits<int>::max());
}
// =============================================================================
// Vertex-ID Visitor Tests
// =============================================================================
// Visitor that accepts vertex ids instead of vertex descriptors
struct IdCountingVisitor {
int vertices_discovered = 0;
int vertices_examined = 0;
template <typename G>
void on_discover_vertex(const G&, const vertex_id_t<G>&) {
++vertices_discovered;
}
template <typename G>
void on_examine_vertex(const G&, const vertex_id_t<G>&) {
++vertices_examined;
}
template <typename G, typename T>
void on_edge_relaxed(const G&, const T&) {}
};
TEST_CASE("dijkstra_shortest_paths - vertex id visitor", "[algorithm][dijkstra_shortest_paths][visitor_id]") {
using Graph = vov_weighted;
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distance(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distance, predecessor);
IdCountingVisitor visitor;
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distance),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }, visitor);
// All 5 vertices should be discovered via id-based callbacks
// (examine may be called more than once per vertex due to re-enqueue)
REQUIRE(visitor.vertices_discovered == 5);
REQUIRE(visitor.vertices_examined >= 5);
// Results should still be correct
REQUIRE(distance[0] == 0);
REQUIRE(distance[1] == clrs_dijkstra_results::distances_from_0[1]);
}
// =============================================================================
// Error / Exception Condition Tests
// =============================================================================
TEST_CASE("dijkstra_shortest_paths - source vertex out of range throws", "[algorithm][dijkstra_shortest_paths][error]") {
using Graph = vov_weighted;
auto g = path_graph_4_weighted<Graph>(); // 4 vertices (ids 0-3)
std::vector<int> distances(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distances, predecessor);
CHECK_THROWS_AS(dijkstra_shortest_paths(g, vertex_id_t<Graph>(99),
container_value_fn(distances),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }),
std::out_of_range);
}
TEST_CASE("dijkstra_shortest_paths - negative edge weight throws", "[algorithm][dijkstra_shortest_paths][error]") {
using Graph = vov_weighted; // int edge values (signed)
auto g = path_graph_4_weighted<Graph>(); // 0->1->2->3
std::vector<int> distances(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distances, predecessor);
// Weight function that always returns a negative value triggers the signed-weight guard
CHECK_THROWS_AS(dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distances),
container_value_fn(predecessor),
[](const auto&, const auto&) { return -1; }),
std::out_of_range);
}
TEST_CASE("dijkstra_shortest_paths - infinite weight edge leaves vertex unreachable",
"[algorithm][dijkstra_shortest_paths][error]") {
using Graph = vov_weighted;
using distance_type = int;
// When the edge weight equals the infinity sentinel, combine(0, INF) = INF which is
// NOT strictly less than INF, so relax_target returns false. The neighbor remains
// undiscovered (distance stays at INF) and the edge is treated as not relaxed.
// This matches BGL semantics: no spurious "internal invariant" exception is thrown
// when a custom Compare/Combine combination cannot improve over the infinity sentinel.
auto g = path_graph_4_weighted<Graph>(); // 0->1->2->3
std::vector<distance_type> distances(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distances, predecessor);
const auto INF = infinite_distance<distance_type>();
CHECK_NOTHROW(dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distances),
container_value_fn(predecessor),
[INF](const auto&, const auto&) { return INF; }));
// Source has distance 0; all other vertices remain unreachable (distance == INF)
// because every edge weight is INF and relaxation cannot improve the sentinel.
CHECK(distances[0] == 0);
for (vertex_id_t<Graph> v = 1; v < num_vertices(g); ++v) {
CHECK(distances[v] == INF);
}
}
TEST_CASE("dijkstra_shortest_paths - on_edge_not_relaxed visitor callback", "[algorithm][dijkstra_shortest_paths][visitor]") {
using Graph = vov_weighted;
// The CLRS graph has multiple paths to the same vertex; revisiting an already-optimal
// vertex triggers on_edge_not_relaxed (e.g. z->s, t->y, x->z are not relaxed).
auto g = clrs_dijkstra_graph<Graph>();
std::vector<int> distances(num_vertices(g));
std::vector<vertex_id_t<Graph>> predecessor(num_vertices(g));
init_shortest_paths(g, distances, predecessor);
CountingVisitor visitor;
dijkstra_shortest_paths(g, vertex_id_t<Graph>(0),
container_value_fn(distances),
container_value_fn(predecessor),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }, visitor);
CHECK(visitor.edges_not_relaxed > 0);
REQUIRE(distances[0] == 0);
REQUIRE(distances[1] == clrs_dijkstra_results::distances_from_0[1]); // t: 8
REQUIRE(distances[3] == clrs_dijkstra_results::distances_from_0[3]); // y: 5
}
// =============================================================================
// Sparse (Map-Based) Graph Tests
// =============================================================================
#include "../common/map_graph_fixtures.hpp"
#include <graph/adj_list/vertex_property_map.hpp>
using namespace graph::test::map_fixtures;
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - sparse CLRS example",
"[algorithm][dijkstra_shortest_paths][sparse]",
SPARSE_VERTEX_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
const auto& exp = clrs_dijkstra_sparse_expected{};
auto g = map_fixtures::clrs_dijkstra_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
// Initialize predecessors: each vertex points to itself
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
dijkstra_shortest_paths(g, id_type(exp.s),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Validate distances against known CLRS results
for (size_t i = 0; i < exp.num_vertices; ++i) {
REQUIRE(distances[exp.vertex_ids[i]] == exp.distances[i]);
}
// Source predecessor is itself
REQUIRE(predecessors[exp.s] == exp.s);
}
TEMPLATE_TEST_CASE("dijkstra_shortest_distances - sparse CLRS example",
"[algorithm][dijkstra_shortest_paths][sparse]",
SPARSE_VERTEX_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
const auto& exp = clrs_dijkstra_sparse_expected{};
auto g = map_fixtures::clrs_dijkstra_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
dijkstra_shortest_distances(g, id_type(exp.s),
container_value_fn(distances),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
for (size_t i = 0; i < exp.num_vertices; ++i) {
REQUIRE(distances[exp.vertex_ids[i]] == exp.distances[i]);
}
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - sparse multi-source",
"[algorithm][dijkstra_shortest_paths][sparse][multi_source]",
SPARSE_VERTEX_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
const auto& exp = clrs_dijkstra_sparse_expected{};
auto g = map_fixtures::clrs_dijkstra_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
// Start from s (10) and y (40)
std::vector<id_type> sources = {exp.s, exp.y};
dijkstra_shortest_paths(g, sources,
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Both sources should have distance 0
REQUIRE(distances[exp.s] == 0);
REQUIRE(distances[exp.y] == 0);
// All vertices should be reachable
for (size_t i = 0; i < exp.num_vertices; ++i) {
REQUIRE(distances[exp.vertex_ids[i]] < infinite_distance<int>());
}
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - sparse with visitor",
"[algorithm][dijkstra_shortest_paths][sparse][visitor]",
SPARSE_VERTEX_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
const auto& exp = clrs_dijkstra_sparse_expected{};
auto g = map_fixtures::clrs_dijkstra_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
CountingVisitor visitor;
dijkstra_shortest_paths(
g, id_type(exp.s),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }, visitor);
REQUIRE(visitor.vertices_discovered == static_cast<int>(exp.num_vertices));
REQUIRE(visitor.vertices_examined >= static_cast<int>(exp.num_vertices));
REQUIRE(visitor.edges_relaxed > 0);
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - sparse source not in graph throws",
"[algorithm][dijkstra_shortest_paths][sparse][error]",
SPARSE_VERTEX_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
auto g = map_fixtures::clrs_dijkstra_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
// Vertex ID 999 does not exist in the sparse graph
CHECK_THROWS_AS(dijkstra_shortest_paths(g, id_type(999),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }),
std::out_of_range);
}
// =============================================================================
// Non-Integral Vertex ID Tests (String IDs)
//
// These tests verify Dijkstra works with mapped_adjacency_list graphs whose
// vertex_id_t is std::string — a non-integral, non-arithmetic key type.
// =============================================================================
#include <graph/container/dynamic_graph.hpp>
// String-VId graph types with int edge weights (EV=int, VV=void, GV=void, VId=string)
using mov_string_int = graph::container::dynamic_graph<
int, void, void, std::string, false,
graph::container::mov_graph_traits<int, void, void, std::string, false>>;
using uov_string_int = graph::container::dynamic_graph<
int, void, void, std::string, false,
graph::container::uov_graph_traits<int, void, void, std::string, false>>;
using mos_string_int = graph::container::dynamic_graph<
int, void, void, std::string, false,
graph::container::mos_graph_traits<int, void, void, std::string, false>>;
// CLRS Dijkstra graph with string vertex IDs: s, t, x, y, z
// Same topology/weights as CLRS Figure 24.6
template <typename Graph>
Graph clrs_dijkstra_string_graph() {
using S = std::string;
return Graph({
{S("s"), S("t"), 10}, {S("s"), S("y"), 5}, // s -> t(10), s -> y(5)
{S("t"), S("x"), 1}, {S("t"), S("y"), 2}, // t -> x(1), t -> y(2)
{S("x"), S("z"), 4}, // x -> z(4)
{S("y"), S("t"), 3}, {S("y"), S("x"), 9},
{S("y"), S("z"), 2}, // y -> t(3), y -> x(9), y -> z(2)
{S("z"), S("s"), 7}, {S("z"), S("x"), 6}, // z -> s(7), z -> x(6)
});
}
// Expected shortest distances from "s"
struct clrs_string_expected {
static constexpr size_t num_vertices = 5;
// Ordered by CLRS labeling: s=0, t=8, x=9, y=5, z=7
static inline const std::vector<std::pair<std::string, int>> distances = {
{"s", 0}, {"t", 8}, {"x", 9}, {"y", 5}, {"z", 7}};
static inline const std::string source = "s";
};
#define STRING_VID_TYPES mov_string_int, uov_string_int, mos_string_int
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - string vertex IDs",
"[algorithm][dijkstra_shortest_paths][string_id]",
STRING_VID_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
static_assert(std::is_same_v<id_type, std::string>, "vertex_id_t must be std::string");
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
dijkstra_shortest_paths(g, std::string("s"),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Validate all distances
for (const auto& [vid, expected_dist] : clrs_string_expected::distances) {
REQUIRE(distances[vid] == expected_dist);
}
// Source predecessor is itself
REQUIRE(predecessors[std::string("s")] == std::string("s"));
// Verify predecessor chain for t: t's predecessor should be y (path s->y->t, cost 5+3=8)
REQUIRE(predecessors[std::string("t")] == std::string("y"));
}
TEMPLATE_TEST_CASE("dijkstra_shortest_distances - string vertex IDs",
"[algorithm][dijkstra_shortest_paths][string_id]",
STRING_VID_TYPES) {
using Graph = TestType;
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
dijkstra_shortest_distances(g, std::string("s"),
container_value_fn(distances),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
for (const auto& [vid, expected_dist] : clrs_string_expected::distances) {
REQUIRE(distances[vid] == expected_dist);
}
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - string vertex IDs multi-source",
"[algorithm][dijkstra_shortest_paths][string_id][multi_source]",
STRING_VID_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
// Start from "s" and "y"
std::vector<id_type> sources = {std::string("s"), std::string("y")};
dijkstra_shortest_paths(g, sources,
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Both sources should have distance 0
REQUIRE(distances[std::string("s")] == 0);
REQUIRE(distances[std::string("y")] == 0);
// All vertices should be reachable
for (const auto& [vid, _] : clrs_string_expected::distances) {
REQUIRE(distances[vid] < infinite_distance<int>());
}
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - string vertex IDs with visitor",
"[algorithm][dijkstra_shortest_paths][string_id][visitor]",
STRING_VID_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
CountingVisitor visitor;
dijkstra_shortest_paths(
g, std::string("s"),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }, visitor);
REQUIRE(visitor.vertices_discovered == static_cast<int>(clrs_string_expected::num_vertices));
REQUIRE(visitor.vertices_examined >= static_cast<int>(clrs_string_expected::num_vertices));
REQUIRE(visitor.edges_relaxed > 0);
REQUIRE(visitor.edges_not_relaxed > 0);
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - string vertex IDs invalid source throws",
"[algorithm][dijkstra_shortest_paths][string_id][error]",
STRING_VID_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
// "nonexistent" is not a vertex in the graph
CHECK_THROWS_AS(dijkstra_shortest_paths(g, std::string("nonexistent"),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); }),
std::out_of_range);
}
TEMPLATE_TEST_CASE("dijkstra_shortest_paths - string vertex IDs path reconstruction",
"[algorithm][dijkstra_shortest_paths][string_id]",
STRING_VID_TYPES) {
using Graph = TestType;
using id_type = vertex_id_t<Graph>;
auto g = clrs_dijkstra_string_graph<Graph>();
auto distances = make_vertex_property_map<Graph, int>(g, infinite_distance<int>());
auto predecessors = make_vertex_property_map<Graph, id_type>(g, id_type{});
for (auto&& [uid, u] : views::vertexlist(g))
predecessors[uid] = uid;
dijkstra_shortest_paths(g, std::string("s"),
container_value_fn(distances),
container_value_fn(predecessors),
[](const auto& graph_ref, const auto& uv) { return edge_value(graph_ref, uv); });
// Reconstruct path from "s" to "x": should be s -> y -> t -> x (cost 5+3+1=9)
std::vector<std::string> path;
std::string current = "x";
while (current != "s") {
path.push_back(current);
current = predecessors[current];
}
path.push_back("s");
std::reverse(path.begin(), path.end());
REQUIRE(path.size() == 4);
REQUIRE(path[0] == "s");
REQUIRE(path[1] == "y");
REQUIRE(path[2] == "t");
REQUIRE(path[3] == "x");
}