Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_dijkstra.cc
Go to the documentation of this file.
1
13#include <gtest/gtest.h>
14
15#include <algorithm>
16#include <climits>
17#include <functional>
18#include <limits>
19#include <map>
20#include <random>
21#include <stdexcept>
22#include <utility>
23#include <vector>
24
25#include <Dijkstra.H>
26#include <tpl_graph.H>
27
28using namespace Aleph;
29
30// Helper iterator for multigraphs that visits ALL arcs including parallel ones
31// and provides the get_current_arc_ne() method required by Dijkstra
32template <class GT, class SA = Dft_Show_Arc<GT>>
33class Out_Iterator_AllArcs : public Out_Iterator<GT, SA>
34{
35public:
37 using Base::Base;
38
39 typename GT::Arc * get_current_arc_ne() const noexcept
40 {
41 return const_cast<Out_Iterator_AllArcs *>(this)->get_current_arc();
42 }
43};
44
45// Graph type for tests - uses double weights for distances
47using Node = GT::Node;
48using Arc = GT::Arc;
49
50// Digraph type for directed graph tests
53using DArc = DGT::Arc;
54
55struct BinHeapTag
56{
57 template <class G, class D, class A>
59};
60
61struct FibHeapTag
62{
63 template <class G, class D, class A>
65};
66
67template <typename HeapTag, typename Graph>
70 Dft_Show_Arc<Graph>, HeapTag::template Heap>;
71
72// Dijkstra with Out_Iterator_AllArcs for multigraphs (visits ALL arcs, including parallel)
73template <typename HeapTag, typename Graph>
76 Dft_Show_Arc<Graph>, HeapTag::template Heap>;
77
78template <typename HeapTag>
80
81template <typename HeapTag>
83
84template <typename HeapTag>
86
87template <typename HeapTag>
88class DijkstraHeapTest : public ::testing::Test {};
89
90template <typename HeapTag>
91class DijkstraDigraphHeapTest : public ::testing::Test {};
92
93using HeapTypes = ::testing::Types<BinHeapTag, FibHeapTag>;
96
97// ========== TEST 1: Basic Shortest Path ==========
98TYPED_TEST(DijkstraHeapTest, BasicShortestPath) {
99 GT g;
100 Node* n0 = g.insert_node(0);
101 Node* n1 = g.insert_node(1);
102 Node* n2 = g.insert_node(2);
103 Node* n3 = g.insert_node(3);
104 Node* n4 = g.insert_node(4);
105
106 g.insert_arc(n0, n1, 10.0);
107 g.insert_arc(n0, n2, 5.0);
108 g.insert_arc(n1, n2, 2.0);
109 g.insert_arc(n1, n3, 1.0);
110 g.insert_arc(n2, n1, 3.0);
111 g.insert_arc(n2, n3, 9.0);
112 g.insert_arc(n2, n4, 2.0);
113 g.insert_arc(n3, n4, 4.0);
114 g.insert_arc(n4, n0, 7.0);
115 g.insert_arc(n4, n3, 6.0);
116
118 Path<GT> path(g);
119 double d = dij(g, n0, n3, path);
120
121 // Path 0->2->1->3 costs 5+2+1=8 (undirected graph)
122 ASSERT_EQ(d, 8.0);
123
124 Path<GT>::Iterator it(path);
125 ASSERT_TRUE(it.has_curr());
126 EXPECT_EQ(it.get_current_node()->get_info(), 0);
127 it.next();
128 ASSERT_TRUE(it.has_curr());
129 EXPECT_EQ(it.get_current_node()->get_info(), 2);
130 it.next();
131 ASSERT_TRUE(it.has_curr());
132 EXPECT_EQ(it.get_current_node()->get_info(), 1);
133 it.next();
134 ASSERT_TRUE(it.has_curr());
135 EXPECT_EQ(it.get_current_node()->get_info(), 3);
136 it.next();
137 ASSERT_FALSE(it.has_curr());
138}
139
140// ========== TEST 2: Path to Self ==========
142 GT g;
143 Node* n0 = g.insert_node(0);
144
146 Path<GT> path(g);
147 double d = dij(g, n0, n0, path);
148
149 // The path of no arc, of cost 0, as in the other searches; it gave
150 // max() and no path
151 EXPECT_EQ(d, 0);
152 ASSERT_EQ(path.size(), 1u); // one node, no arc
153 EXPECT_EQ(path.get_first_node(), n0);
154}
155
156// ========== TEST 3: No Path Exists ==========
158 GT g;
159 Node* n0 = g.insert_node(0);
160 Node* n1 = g.insert_node(1);
161
162 // No arcs connecting the nodes
163
165 Path<GT> path(g);
166 double d = dij(g, n0, n1, path);
167
168 EXPECT_EQ(d, std::numeric_limits<double>::max());
169 EXPECT_TRUE(path.is_empty());
170}
171
172// ========== TEST 4: Compute Spanning Tree ==========
173TYPED_TEST(DijkstraHeapTest, ComputeSpanningTree) {
174 GT g;
175 Node* n0 = g.insert_node(0);
176 Node* n1 = g.insert_node(1);
177 Node* n2 = g.insert_node(2);
178 Node* n3 = g.insert_node(3);
179 Node* n4 = g.insert_node(4);
180
181 g.insert_arc(n0, n1, 10.0);
182 g.insert_arc(n0, n2, 5.0);
183 g.insert_arc(n1, n2, 2.0);
184 g.insert_arc(n1, n3, 1.0);
185 g.insert_arc(n2, n1, 3.0);
186 g.insert_arc(n2, n3, 9.0);
187 g.insert_arc(n2, n4, 2.0);
188 g.insert_arc(n3, n4, 4.0);
189 g.insert_arc(n4, n0, 7.0);
190 g.insert_arc(n4, n3, 6.0);
191
193 GT tree;
194 dij(g, n0, tree);
195
196 EXPECT_EQ(tree.get_num_nodes(), 5);
197 EXPECT_EQ(tree.get_num_arcs(), 4); // n-1 arcs in spanning tree
198}
199
200// ========== TEST 5: Update Path in Heap (Relaxation) ==========
201TYPED_TEST(DijkstraHeapTest, RelaxationUpdate) {
202 GT g;
203 Node* n0 = g.insert_node(0);
204 Node* n1 = g.insert_node(1);
205 Node* n2 = g.insert_node(2);
206
207 // Direct path 0->1 costs 10, but 0->2->1 costs 2
208 g.insert_arc(n0, n1, 10.0);
209 g.insert_arc(n0, n2, 1.0);
210 g.insert_arc(n2, n1, 1.0);
211
213 Path<GT> path(g);
214 double d = dij(g, n0, n1, path);
215
216 ASSERT_EQ(d, 2.0); // Should find the cheaper path through n2
217
218 Path<GT>::Iterator it(path);
219 ASSERT_TRUE(it.has_curr());
220 EXPECT_EQ(it.get_current_node()->get_info(), 0);
221 it.next();
222 ASSERT_TRUE(it.has_curr());
223 EXPECT_EQ(it.get_current_node()->get_info(), 2);
224 it.next();
225 ASSERT_TRUE(it.has_curr());
226 EXPECT_EQ(it.get_current_node()->get_info(), 1);
227 it.next();
228 ASSERT_FALSE(it.has_curr());
229}
230
231// ========== TEST 6: Paint Spanning Tree ==========
232TYPED_TEST(DijkstraHeapTest, PaintSpanningTree) {
233 GT g;
234 Node* n0 = g.insert_node(0);
235 Node* n1 = g.insert_node(1);
236 Node* n2 = g.insert_node(2);
237 Node* n3 = g.insert_node(3);
238
239 g.insert_arc(n0, n1, 1.0);
240 g.insert_arc(n0, n2, 4.0);
241 g.insert_arc(n1, n2, 2.0);
242 g.insert_arc(n1, n3, 5.0);
243 g.insert_arc(n2, n3, 1.0);
244
246 dij.paint_min_paths_tree(g, n0);
247
248 // After painting, we should be able to get min path to any node
249 Path<GT> path(g);
250 dij.get_min_path(n3, path);
251
252 // Verify path: 0 -> 1 -> 2 -> 3 (cost 4)
253 ASSERT_FALSE(path.is_empty());
254}
255
256// ========== TEST 7: Copy Painted Min Paths Tree ==========
257TYPED_TEST(DijkstraHeapTest, CopyPaintedMinPathsTree) {
258 GT g;
259 Node* n0 = g.insert_node(0);
260 Node* n1 = g.insert_node(1);
261 Node* n2 = g.insert_node(2);
262 Node* n3 = g.insert_node(3);
263
264 g.insert_arc(n0, n1, 1.0);
265 g.insert_arc(n0, n2, 4.0);
266 g.insert_arc(n1, n2, 2.0);
267 g.insert_arc(n1, n3, 5.0);
268 g.insert_arc(n2, n3, 1.0);
269
271 dij.paint_min_paths_tree(g, n0);
272
273 GT tree;
274 dij.copy_painted_min_paths_tree(g, tree);
275
276 EXPECT_EQ(tree.get_num_nodes(), 4);
277 EXPECT_EQ(tree.get_num_arcs(), 3); // n-1 arcs in tree
278}
279
280// ========== TEST 8: Single Node Graph ==========
281TYPED_TEST(DijkstraHeapTest, SingleNodeGraph) {
282 GT g;
283 Node* n0 = g.insert_node(0);
284
286 GT tree;
287 dij(g, n0, tree);
288
289 EXPECT_EQ(tree.get_num_nodes(), 1);
290 EXPECT_EQ(tree.get_num_arcs(), 0);
291}
292
293// ========== TEST 9: Linear Chain Graph ==========
294TYPED_TEST(DijkstraHeapTest, LinearChainGraph) {
295 GT g;
296 Node* n0 = g.insert_node(0);
297 Node* n1 = g.insert_node(1);
298 Node* n2 = g.insert_node(2);
299 Node* n3 = g.insert_node(3);
300
301 g.insert_arc(n0, n1, 1.0);
302 g.insert_arc(n1, n2, 1.0);
303 g.insert_arc(n2, n3, 1.0);
304
306 Path<GT> path(g);
307 double d = dij(g, n0, n3, path);
308
309 EXPECT_EQ(d, 3.0);
310}
311
312// ========== TEST 10: Complete Graph K4 ==========
313TYPED_TEST(DijkstraHeapTest, CompleteGraphK4) {
314 GT g;
315 Node* n0 = g.insert_node(0);
316 Node* n1 = g.insert_node(1);
317 Node* n2 = g.insert_node(2);
318 Node* n3 = g.insert_node(3);
319
320 // All edges with weight 1
321 g.insert_arc(n0, n1, 1.0);
322 g.insert_arc(n0, n2, 1.0);
323 g.insert_arc(n0, n3, 1.0);
324 g.insert_arc(n1, n2, 1.0);
325 g.insert_arc(n1, n3, 1.0);
326 g.insert_arc(n2, n3, 1.0);
327
329 Path<GT> path(g);
330 double d = dij(g, n0, n3, path);
331
332 EXPECT_EQ(d, 1.0); // Direct path
333}
334
335// ========== TEST 11: Graph with Self Loop ==========
336TYPED_TEST(DijkstraHeapTest, GraphWithSelfLoop) {
337 GT g;
338 Node* n0 = g.insert_node(0);
339 Node* n1 = g.insert_node(1);
340
341 g.insert_arc(n0, n0, 5.0); // Self loop
342 g.insert_arc(n0, n1, 2.0);
343
345 Path<GT> path(g);
346 double d = dij(g, n0, n1, path);
347
348 EXPECT_EQ(d, 2.0); // Self loop should be ignored
349}
350
351// ========== TEST 12: Digraph Basic Path ==========
353 DGT g;
354 DNode* n0 = g.insert_node(0);
355 DNode* n1 = g.insert_node(1);
356 DNode* n2 = g.insert_node(2);
357
358 g.insert_arc(n0, n1, 1.0);
359 g.insert_arc(n1, n2, 1.0);
360 // Note: no arc n2->n0, so no path back
361
363 Path<DGT> path(g);
364 double d = dij(g, n0, n2, path);
365
366 EXPECT_EQ(d, 2.0);
367}
368
369// ========== TEST 13: Digraph No Return Path ==========
371 DGT g;
372 DNode* n0 = g.insert_node(0);
373 DNode* n1 = g.insert_node(1);
374
375 g.insert_arc(n0, n1, 1.0); // Only forward direction
376
378
379 Path<DGT> path_forward(g);
380 double d1 = dij(g, n0, n1, path_forward);
381 EXPECT_EQ(d1, 1.0);
382
383 Path<DGT> path_backward(g);
384 double d2 = dij(g, n1, n0, path_backward);
385 EXPECT_EQ(d2, std::numeric_limits<double>::max());
386}
387
388// ========== TEST 14: Empty Graph ==========
390 GT g;
391
393 GT tree;
394
395 // Should handle empty graph gracefully - tree stays empty
396 // Note: calling dij on empty graph would need a null node, so we just check tree
397 EXPECT_EQ(tree.get_num_nodes(), 0);
398 EXPECT_EQ(tree.get_num_arcs(), 0);
399}
400
401// ========== TEST 15: Multiple Paths Same Cost ==========
402TYPED_TEST(DijkstraHeapTest, MultiplePathsSameCost) {
403 GT g;
404 Node* n0 = g.insert_node(0);
405 Node* n1 = g.insert_node(1);
406 Node* n2 = g.insert_node(2);
407 Node* n3 = g.insert_node(3);
408
409 // Two paths from n0 to n3 with same cost 2
410 // Path 1: n0 -> n1 -> n3 (cost 2)
411 // Path 2: n0 -> n2 -> n3 (cost 2)
412 g.insert_arc(n0, n1, 1.0);
413 g.insert_arc(n0, n2, 1.0);
414 g.insert_arc(n1, n3, 1.0);
415 g.insert_arc(n2, n3, 1.0);
416
418 Path<GT> path(g);
419 double d = dij(g, n0, n3, path);
420
421 EXPECT_EQ(d, 2.0);
422 ASSERT_FALSE(path.is_empty());
423}
424
425// ========== TEST 16: Large Weights ==========
427 GT g;
428 Node* n0 = g.insert_node(0);
429 Node* n1 = g.insert_node(1);
430 Node* n2 = g.insert_node(2);
431
432 g.insert_arc(n0, n1, 1e10);
433 g.insert_arc(n1, n2, 1e10);
434
436 Path<GT> path(g);
437 double d = dij(g, n0, n2, path);
438
439 EXPECT_EQ(d, 2e10);
440}
441
442// ========== TEST 17: Zero Weight Edges ==========
443TYPED_TEST(DijkstraHeapTest, ZeroWeightEdges) {
444 GT g;
445 Node* n0 = g.insert_node(0);
446 Node* n1 = g.insert_node(1);
447 Node* n2 = g.insert_node(2);
448
449 g.insert_arc(n0, n1, 0.0);
450 g.insert_arc(n1, n2, 0.0);
451
453 Path<GT> path(g);
454 double d = dij(g, n0, n2, path);
455
456 EXPECT_EQ(d, 0.0);
457}
458
459// ========== TEST 18: Fractional Weights ==========
460TYPED_TEST(DijkstraHeapTest, FractionalWeights) {
461 GT g;
462 Node* n0 = g.insert_node(0);
463 Node* n1 = g.insert_node(1);
464 Node* n2 = g.insert_node(2);
465
466 g.insert_arc(n0, n1, 0.5);
467 g.insert_arc(n1, n2, 0.3);
468
470 Path<GT> path(g);
471 double d = dij(g, n0, n2, path);
472
473 EXPECT_NEAR(d, 0.8, 1e-9);
474}
475
476// ========== TEST 19: Star Graph ==========
478 GT g;
479 Node* center = g.insert_node(0);
480 Node* n1 = g.insert_node(1);
481 Node* n2 = g.insert_node(2);
482 Node* n3 = g.insert_node(3);
483 Node* n4 = g.insert_node(4);
484
485 g.insert_arc(center, n1, 1.0);
486 g.insert_arc(center, n2, 2.0);
487 g.insert_arc(center, n3, 3.0);
488 g.insert_arc(center, n4, 4.0);
489
491 GT tree;
492 dij(g, center, tree);
493
494 EXPECT_EQ(tree.get_num_nodes(), 5);
495 EXPECT_EQ(tree.get_num_arcs(), 4);
496}
497
498// ========== TEST 20: Verify Path Correctness ==========
499TYPED_TEST(DijkstraHeapTest, VerifyPathNodes) {
500 GT g;
501 Node* n0 = g.insert_node(0);
502 Node* n1 = g.insert_node(1);
503 Node* n2 = g.insert_node(2);
504 Node* n3 = g.insert_node(3);
505
506 g.insert_arc(n0, n1, 1.0);
507 g.insert_arc(n1, n2, 2.0);
508 g.insert_arc(n2, n3, 3.0);
509 g.insert_arc(n0, n3, 100.0); // Much longer direct path
510
512 Path<GT> path(g);
513 double d = dij(g, n0, n3, path);
514
515 EXPECT_EQ(d, 6.0);
516
517 // Collect all nodes in path
518 DynList<int> path_nodes;
519 for (Path<GT>::Iterator it(path); it.has_curr(); it.next())
520 path_nodes.append(it.get_current_node()->get_info());
521
522 ASSERT_EQ(path_nodes.size(), 4);
523
524 auto pit = path_nodes.get_it();
525 EXPECT_EQ(pit.get_curr(), 0); pit.next();
526 EXPECT_EQ(pit.get_curr(), 1); pit.next();
527 EXPECT_EQ(pit.get_curr(), 2); pit.next();
528 EXPECT_EQ(pit.get_curr(), 3);
529}
530
531// ========== TEST 21: State Getters ==========
533 GT g;
534 Node* n0 = g.insert_node(0);
535 Node* n1 = g.insert_node(1);
536 g.insert_arc(n0, n1, 1.0);
537
539
540 // Before any computation
541 EXPECT_FALSE(dij.has_computation());
542 EXPECT_FALSE(dij.is_painted());
543 EXPECT_EQ(dij.get_start_node(), nullptr);
544 EXPECT_EQ(dij.get_graph(), nullptr);
545
546 // After painting
547 dij.paint_min_paths_tree(g, n0);
548
549 EXPECT_TRUE(dij.has_computation());
550 EXPECT_TRUE(dij.is_painted());
551 EXPECT_EQ(dij.get_start_node(), n0);
552 EXPECT_EQ(dij.get_graph(), &g);
553}
554
555// ========== TEST 22: Compute Partial Min Paths Tree ==========
556TYPED_TEST(DijkstraHeapTest, ComputePartialMinPathsTree) {
557 GT g;
558 Node* n0 = g.insert_node(0);
559 Node* n1 = g.insert_node(1);
560 Node* n2 = g.insert_node(2);
561 Node* n3 = g.insert_node(3);
562 Node* n4 = g.insert_node(4);
563
564 g.insert_arc(n0, n1, 1.0);
565 g.insert_arc(n1, n2, 1.0);
566 g.insert_arc(n2, n3, 1.0);
567 g.insert_arc(n3, n4, 1.0); // n4 should NOT be in partial tree
568
570 GT tree;
571 dij.compute_partial_min_paths_tree(g, n0, n3, tree);
572
573 // Tree should contain n0, n1, n2, n3 but NOT n4
574 EXPECT_EQ(tree.get_num_nodes(), 4);
575 EXPECT_EQ(tree.get_num_arcs(), 3);
576}
577
578// ========== TEST 23: Get Min Path from Tree ==========
579TYPED_TEST(DijkstraHeapTest, GetMinPathFromTree) {
580 GT g;
581 Node* n0 = g.insert_node(0);
582 Node* n1 = g.insert_node(1);
583 Node* n2 = g.insert_node(2);
584 Node* n3 = g.insert_node(3);
585
586 g.insert_arc(n0, n1, 1.0);
587 g.insert_arc(n1, n2, 2.0);
588 g.insert_arc(n2, n3, 3.0);
589
591 GT tree;
592 auto tree_start = dij.compute_min_paths_tree(g, n0, tree);
593
594 // Verify tree was built correctly
595 EXPECT_EQ(tree.get_num_nodes(), 4);
596 EXPECT_EQ(tree.get_num_arcs(), 3);
597 EXPECT_NE(tree_start, nullptr);
598
599 // Use find_min_path instead which is more straightforward
600 Path<GT> path(g);
601 double d = dij(g, n0, n3, path);
602
603 EXPECT_EQ(d, 6.0);
604 ASSERT_FALSE(path.is_empty());
605}
606
607// ========== TEST 24: Null Node Validation ==========
608TYPED_TEST(DijkstraHeapTest, NullNodeValidation) {
609 GT g;
610 Node* n0 = g.insert_node(0);
611
613 GT tree;
614
615 EXPECT_THROW(dij.compute_min_paths_tree(g, nullptr, tree), std::domain_error);
616 EXPECT_THROW(dij.compute_partial_min_paths_tree(g, nullptr, n0, tree), std::domain_error);
617 EXPECT_THROW(dij.compute_partial_min_paths_tree(g, n0, nullptr, tree), std::domain_error);
618 EXPECT_THROW(dij.paint_min_paths_tree(g, nullptr), std::domain_error);
619 EXPECT_THROW(dij.paint_partial_min_paths_tree(g, nullptr, n0), std::domain_error);
620 EXPECT_THROW(dij.paint_partial_min_paths_tree(g, n0, nullptr), std::domain_error);
621}
622
623// ========== TEST 25: Disconnected Graph ==========
624TYPED_TEST(DijkstraHeapTest, DisconnectedGraph) {
625 GT g;
626 Node* n0 = g.insert_node(0);
627 Node* n1 = g.insert_node(1);
628 Node* n2 = g.insert_node(2);
629 Node* n3 = g.insert_node(3);
630
631 // Two disconnected components: {n0, n1} and {n2, n3}
632 g.insert_arc(n0, n1, 1.0);
633 g.insert_arc(n2, n3, 1.0);
634
636 Path<GT> path(g);
637 double d = dij(g, n0, n3, path);
638
639 // n3 is not reachable from n0
640 EXPECT_EQ(d, std::numeric_limits<double>::max());
641 EXPECT_TRUE(path.is_empty());
642}
643
644// ========== TEST 26: Integer Weights ==========
645TYPED_TEST(DijkstraHeapTest, IntegerWeights) {
647
648 IGT g;
649 auto* n0 = g.insert_node(0);
650 auto* n1 = g.insert_node(1);
651 auto* n2 = g.insert_node(2);
652
653 g.insert_arc(n0, n1, 5);
654 g.insert_arc(n1, n2, 3);
655
657 Path<IGT> path(g);
658 int d = dij(g, n0, n2, path);
659
660 EXPECT_EQ(d, 8);
661}
662
663// ========== TEST 27: Get Distance After Painting ==========
664TYPED_TEST(DijkstraHeapTest, GetDistanceAfterPainting) {
665 GT g;
666 Node* n0 = g.insert_node(0);
667 Node* n1 = g.insert_node(1);
668 Node* n2 = g.insert_node(2);
669 Node* n3 = g.insert_node(3);
670
671 g.insert_arc(n0, n1, 1.0);
672 g.insert_arc(n1, n2, 2.0);
673 g.insert_arc(n2, n3, 3.0);
674
676 dij.paint_min_paths_tree(g, n0);
677
678 EXPECT_EQ(dij.get_distance(n0), 0.0);
679 EXPECT_EQ(dij.get_distance(n1), 1.0);
680 EXPECT_EQ(dij.get_distance(n2), 3.0);
681 EXPECT_EQ(dij.get_distance(n3), 6.0);
682}
683
684// ========== TEST 28: Get Distance Before Painting ==========
685TYPED_TEST(DijkstraHeapTest, GetDistanceBeforePainting) {
686 GT g;
687 Node* n0 = g.insert_node(0);
688
690
691 // Should throw because not painted yet
692 EXPECT_THROW(dij.get_distance(n0), std::domain_error);
693}
694
695// ========== TEST 29: Get Distance Unreachable Node ==========
696TYPED_TEST(DijkstraHeapTest, GetDistanceUnreachableNode) {
697 GT g;
698 Node* n0 = g.insert_node(0);
699 Node* n1 = g.insert_node(1);
700 Node* n2 = g.insert_node(2); // Not connected
701
702 g.insert_arc(n0, n1, 1.0);
703
705 dij.paint_min_paths_tree(g, n0);
706
707 // n2 is not reachable
708 EXPECT_THROW(dij.get_distance(n2), std::domain_error);
709}
710
711// ========== TEST 30: Multiple Successive Computations ==========
712TYPED_TEST(DijkstraHeapTest, MultipleSuccessiveComputations) {
713 GT g;
714 Node* n0 = g.insert_node(0);
715 Node* n1 = g.insert_node(1);
716 Node* n2 = g.insert_node(2);
717
718 g.insert_arc(n0, n1, 1.0);
719 g.insert_arc(n1, n2, 2.0);
720 g.insert_arc(n0, n2, 10.0);
721
723
724 // First computation from n0
725 Path<GT> path1(g);
726 double d1 = dij(g, n0, n2, path1);
727 EXPECT_EQ(d1, 3.0);
728
729 // Second computation from n2 (different start)
730 Path<GT> path2(g);
731 double d2 = dij(g, n2, n0, path2);
732 EXPECT_EQ(d2, 3.0); // Same in undirected graph
733}
734
735// ========== TEST 31: Paint Partial Returns False When Not Found ==========
736TYPED_TEST(DijkstraHeapTest, PaintPartialReturnsFalse) {
737 GT g;
738 Node* n0 = g.insert_node(0);
739 Node* n1 = g.insert_node(1);
740 Node* n2 = g.insert_node(2); // Disconnected
741
742 g.insert_arc(n0, n1, 1.0);
743
745 bool found = dij.paint_partial_min_paths_tree(g, n0, n2);
746
747 EXPECT_FALSE(found);
748}
749
750// ========== TEST 32: Paint Partial Returns True When Found ==========
751TYPED_TEST(DijkstraHeapTest, PaintPartialReturnsTrue) {
752 GT g;
753 Node* n0 = g.insert_node(0);
754 Node* n1 = g.insert_node(1);
755 Node* n2 = g.insert_node(2);
756
757 g.insert_arc(n0, n1, 1.0);
758 g.insert_arc(n1, n2, 2.0);
759
761 bool found = dij.paint_partial_min_paths_tree(g, n0, n2);
762
763 EXPECT_TRUE(found);
764
765 // Should be able to get path now
766 Path<GT> path(g);
767 double d = dij.get_min_path(n2, path);
768 EXPECT_EQ(d, 3.0);
769}
770
771// ========== TEST 33: Very Long Path ==========
773 GT g;
774 const int N = 100;
775
776 std::vector<Node*> nodes(N);
777 for (int i = 0; i < N; ++i)
778 nodes[i] = g.insert_node(i);
779
780 for (int i = 0; i < N - 1; ++i)
781 g.insert_arc(nodes[i], nodes[i+1], 1.0);
782
784 Path<GT> path(g);
785 double d = dij(g, nodes[0], nodes[N-1], path);
786
787 EXPECT_EQ(d, static_cast<double>(N - 1));
788}
789
790// ========== TEST 34: Dense Graph Performance ==========
791TYPED_TEST(DijkstraHeapTest, DenseGraphPerformance) {
792 GT g;
793 const int N = 50;
794
795 std::vector<Node*> nodes(N);
796 for (int i = 0; i < N; ++i)
797 nodes[i] = g.insert_node(i);
798
799 // Create complete graph
800 for (int i = 0; i < N; ++i)
801 for (int j = i + 1; j < N; ++j)
802 g.insert_arc(nodes[i], nodes[j], static_cast<double>(i + j));
803
805 GT tree;
806 dij(g, nodes[0], tree);
807
808 EXPECT_EQ(tree.get_num_nodes(), N);
809 EXPECT_EQ(tree.get_num_arcs(), N - 1);
810}
811
812// ========== TEST 35: Compute Spanning Tree on Disconnected Graph ==========
813TYPED_TEST(DijkstraHeapTest, ComputeSpanningTreeDisconnected) {
814 GT g;
815 Node* n0 = g.insert_node(0);
816 Node* n1 = g.insert_node(1);
817 Node* n2 = g.insert_node(2);
818 Node* n3 = g.insert_node(3);
819
820 g.insert_arc(n0, n1, 1.0);
821 g.insert_arc(n2, n3, 1.0);
822
824 GT tree;
825 dij.compute_min_paths_tree(g, n0, tree);
826
827 EXPECT_EQ(tree.get_num_nodes(), 2);
828 EXPECT_EQ(tree.get_num_arcs(), 1);
829}
830
831// ========== TEST 36: Parallel Arcs (Multigraph) ==========
832// Test that Dijkstra correctly handles graphs with multiple arcs between same nodes
833// NOTE: Uses DijkstraAllArcs which uses Out_Iterator_Ne to visit ALL arcs including parallel ones
834TYPED_TEST(DijkstraHeapTest, ParallelArcsChoosesMinimum) {
835 GT g;
836 Node* n0 = g.insert_node(0);
837 Node* n1 = g.insert_node(1);
838 Node* n2 = g.insert_node(2);
839
840 // Multiple arcs between n0 and n1 with different weights
841 g.insert_arc(n0, n1, 10.0); // expensive
842 g.insert_arc(n0, n1, 3.0); // cheap - should be chosen
843 g.insert_arc(n0, n1, 7.0); // medium
844
845 // Single arc to destination
846 g.insert_arc(n1, n2, 2.0);
847
848 // Check if graph supports parallel arcs
849 if (g.get_num_arcs() < 4) {
850 GTEST_SKIP() << "Graph type does not support parallel arcs (multigraph)";
851 }
852
854 Path<GT> path(g);
855 double d = dij(g, n0, n2, path);
856
857 // Shortest path should use the 3.0 arc: 3.0 + 2.0 = 5.0
858 EXPECT_EQ(d, 5.0);
859}
860
861// ========== TEST 37: Parallel Arcs in Both Directions ==========
862TYPED_TEST(DijkstraHeapTest, ParallelArcsBothDirections) {
863 GT g;
864 Node* n0 = g.insert_node(0);
865 Node* n1 = g.insert_node(1);
866
867 // Arcs in both directions with different weights
868 g.insert_arc(n0, n1, 5.0);
869 g.insert_arc(n1, n0, 3.0);
870 g.insert_arc(n0, n1, 2.0); // Best from n0 to n1
871
872 // Check if graph supports parallel arcs
873 if (g.get_num_arcs() < 3) {
874 GTEST_SKIP() << "Graph type does not support parallel arcs (multigraph)";
875 }
876
878 Path<GT> path(g);
879 double d = dij(g, n0, n1, path);
880
881 // In undirected graph, shortest should be 2.0
882 EXPECT_EQ(d, 2.0);
883}
884
885// ========== TEST 38: Complex Multigraph ==========
886TYPED_TEST(DijkstraHeapTest, ComplexMultigraph) {
887 GT g;
888 Node* n0 = g.insert_node(0);
889 Node* n1 = g.insert_node(1);
890 Node* n2 = g.insert_node(2);
891 Node* n3 = g.insert_node(3);
892
893 // Multiple paths with parallel arcs
894 g.insert_arc(n0, n1, 4.0);
895 g.insert_arc(n0, n1, 2.0); // Best n0->n1
896 g.insert_arc(n1, n2, 3.0);
897 g.insert_arc(n1, n2, 1.0); // Best n1->n2
898 g.insert_arc(n2, n3, 5.0);
899 g.insert_arc(n2, n3, 2.0); // Best n2->n3
900
901 // Alternative path
902 g.insert_arc(n0, n3, 10.0);
903
904 // Check if graph supports parallel arcs
905 if (g.get_num_arcs() < 7) {
906 GTEST_SKIP() << "Graph type does not support parallel arcs (multigraph)";
907 }
908
910 Path<GT> path(g);
911 double d = dij(g, n0, n3, path);
912
913 // Best path: 2.0 + 1.0 + 2.0 = 5.0
914 EXPECT_EQ(d, 5.0);
915}
916
917// ========== TEST 39: Get Min Path From Tree ==========
918TYPED_TEST(DijkstraHeapTest, GetMinPathFromTreeMethod) {
919 GT g;
920 Node* n0 = g.insert_node(0);
921 Node* n1 = g.insert_node(1);
922 Node* n2 = g.insert_node(2);
923 Node* n3 = g.insert_node(3);
924 Node* n4 = g.insert_node(4);
925
926 g.insert_arc(n0, n1, 1.0);
927 g.insert_arc(n1, n2, 2.0);
928 g.insert_arc(n2, n3, 3.0);
929 g.insert_arc(n3, n4, 4.0);
930 g.insert_arc(n0, n4, 100.0); // Long direct path
931
933 GT tree;
934
935 // First compute the spanning tree
936 auto tree_start = dij.compute_min_paths_tree(g, n0, tree);
937 ASSERT_NE(tree_start, nullptr);
938
939 // Now extract paths from the tree to different destinations
940 Path<GT> path_to_n2(g);
941 double d2 = dij.get_min_path(tree, n2, path_to_n2);
942 EXPECT_EQ(d2, 3.0); // 1 + 2
943 EXPECT_FALSE(path_to_n2.is_empty());
944
945 Path<GT> path_to_n4(g);
946 double d4 = dij.get_min_path(tree, n4, path_to_n4);
947 EXPECT_EQ(d4, 10.0); // 1 + 2 + 3 + 4
948 EXPECT_FALSE(path_to_n4.is_empty());
949
950 // Verify path nodes for n4
951 DynList<int> path_nodes;
952 for (Path<GT>::Iterator it(path_to_n4); it.has_curr(); it.next())
953 path_nodes.append(it.get_current_node()->get_info());
954
955 ASSERT_EQ(path_nodes.size(), 5);
956 auto pit = path_nodes.get_it();
957 EXPECT_EQ(pit.get_curr(), 0); pit.next();
958 EXPECT_EQ(pit.get_curr(), 1); pit.next();
959 EXPECT_EQ(pit.get_curr(), 2); pit.next();
960 EXPECT_EQ(pit.get_curr(), 3); pit.next();
961 EXPECT_EQ(pit.get_curr(), 4);
962}
963
964// ========== TEST 40: Get Min Path From Tree Multiple Queries ==========
965TYPED_TEST(DijkstraHeapTest, GetMinPathFromTreeMultipleQueries) {
966 GT g;
967 Node* n0 = g.insert_node(0);
968 Node* n1 = g.insert_node(1);
969 Node* n2 = g.insert_node(2);
970 Node* n3 = g.insert_node(3);
971
972 // Star graph from n0
973 g.insert_arc(n0, n1, 1.0);
974 g.insert_arc(n0, n2, 2.0);
975 g.insert_arc(n0, n3, 3.0);
976
978 GT tree;
979 dij.compute_min_paths_tree(g, n0, tree);
980
981 // Query multiple destinations from same tree (efficient!)
982 Path<GT> p1(g), p2(g), p3(g);
983
984 double d1 = dij.get_min_path(tree, n1, p1);
985 double d2 = dij.get_min_path(tree, n2, p2);
986 double d3 = dij.get_min_path(tree, n3, p3);
987
988 EXPECT_EQ(d1, 1.0);
989 EXPECT_EQ(d2, 2.0);
990 EXPECT_EQ(d3, 3.0);
991
992 // Each path should have 2 nodes (start + destination)
993 EXPECT_EQ(p1.size(), 2);
994 EXPECT_EQ(p2.size(), 2);
995 EXPECT_EQ(p3.size(), 2);
996}
997
998// ========== Distance policies and exceptions ==========
999
1000// Graph whose arcs hold their index. The weights are kept apart, so a
1001// distance that read the index would find other paths and other costs.
1003
1004// The policy of the report on 7.0.0: the weight of an arc times a factor.
1005struct Scaled_Distance
1006{
1007 using Distance_Type = int;
1008
1009 int factor = 1;
1010
1011 int operator()(IGT::Arc * a) const { return factor * a->get_info(); }
1012};
1013
1014// Counts the calls of a distance and makes one of them throw.
1016{
1017 long calls = 0;
1018 long fail_at = 0; // number of the call that throws; 0 for none
1019
1020 void count()
1021 {
1022 if (++calls == fail_at)
1023 throw std::runtime_error("arc weight not available");
1024 }
1025};
1026
1027// Weights read from a table by the index of the arc. A default-constructed
1028// one has no table and takes the index as the weight, so an algorithm that
1029// built its own instance would find other paths and other costs.
1031{
1032 using Distance_Type = long;
1033
1034 const std::vector<long> * weights = nullptr;
1036
1037 Table_Distance() = default;
1038
1039 explicit Table_Distance(const std::vector<long> & w,
1040 Distance_Failure * f = nullptr) noexcept
1041 : weights(&w), failure(f) {}
1042
1043 long operator()(IGT::Arc * a) const
1044 {
1045 if (failure != nullptr)
1046 failure->count();
1047 const auto i = static_cast<size_t>(a->get_info());
1048 return weights == nullptr ? static_cast<long>(i) : (*weights)[i];
1049 }
1050};
1051
1052template <typename HeapTag>
1055 HeapTag::template Heap>;
1056
1057// A random simple graph, maybe disconnected, with weights from 1 to 50.
1059{
1061 std::vector<IGT::Node *> nodes;
1062 std::vector<long> weights; // weights[i] is the weight of the arc with info i
1063
1064 Weighted_Graph(const size_t num_nodes, const size_t num_arcs,
1065 const unsigned seed)
1066 {
1067 std::mt19937 rng(seed);
1068 std::uniform_int_distribution<size_t> pick(0, num_nodes - 1);
1069 std::uniform_int_distribution<long> weight(1, 50);
1070
1071 for (size_t i = 0; i < num_nodes; ++i)
1072 nodes.push_back(g.insert_node(static_cast<int>(i)));
1073
1074 std::map<std::pair<size_t, size_t>, bool> used;
1075 while (weights.size() < num_arcs)
1076 {
1077 auto u = pick(rng), v = pick(rng);
1078 if (u == v)
1079 continue;
1080 if (u > v)
1081 std::swap(u, v);
1082 if (used[{u, v}])
1083 continue;
1084 used[{u, v}] = true;
1085 g.insert_arc(nodes[u], nodes[v], static_cast<int>(weights.size()));
1086 weights.push_back(weight(rng));
1087 }
1088 }
1089
1090 [[nodiscard]] long weight(IGT::Arc * a) const
1091 {
1092 return weights[static_cast<size_t>(a->get_info())];
1093 }
1094
1095 // Distances from s, by relaxing every arc until nothing changes; apart
1096 // from the algorithms under test. The unreachable nodes are not in it.
1097 std::map<IGT::Node *, long> distances_from(IGT::Node * s)
1098 {
1099 std::map<IGT::Node *, long> d{{s, 0}};
1100 for (bool changed = true; changed; )
1101 {
1102 changed = false;
1103 for (IGT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
1104 {
1105 auto a = it.get_curr();
1106 auto u = g.get_src_node(a);
1107 auto v = g.get_tgt_node(a);
1108 for (int dir = 0; dir < 2; ++dir, std::swap(u, v))
1109 if (d.contains(u) and
1110 (not d.contains(v) or d[u] + weight(a) < d[v]))
1111 {
1112 d[v] = d[u] + weight(a);
1113 changed = true;
1114 }
1115 }
1116 }
1117 return d;
1118 }
1119
1120 // Cost of a path, which must go from s to t
1121 long cost_of(const Path<IGT> & path, IGT::Node * s, IGT::Node * t) const
1122 {
1123 EXPECT_EQ(path.get_first_node(), s);
1124 EXPECT_EQ(path.get_last_node(), t);
1125 long total = 0;
1126 path.for_each_arc([this, &total](IGT::Arc * a) { total += weight(a); });
1127 return total;
1128 }
1129
1130 // Sum of the weights of the arcs of a copy of some arcs of g
1131 long weight_of(IGT & tree) const
1132 {
1133 long total = 0;
1134 for (IGT::Arc_Iterator it(tree); it.has_curr(); it.next_ne())
1135 total += weight(it.get_curr());
1136 return total;
1137 }
1138};
1139
1140// Every method used a default-constructed distance instead of the one
1141// given to the constructor, so a factor of 3 was ignored.
1142TYPED_TEST(DijkstraHeapTest, UsesTheDistanceItWasGiven) {
1143 IGT g;
1144 auto a = g.insert_node(0);
1145 auto b = g.insert_node(1);
1146 auto c = g.insert_node(2);
1147 g.insert_arc(a, b, 3);
1148 g.insert_arc(b, c, 4);
1149 g.insert_arc(a, c, 20);
1150
1152 Dft_Show_Arc<IGT>, TypeParam::template Heap>
1153 dij(Scaled_Distance{3});
1154
1155 Path<IGT> path(g);
1156 EXPECT_EQ(dij.find_min_path(g, a, c, path), 21);
1157
1158 dij.paint_min_paths_tree(g, a);
1159 EXPECT_EQ(dij.get_distance(c), 21);
1160 EXPECT_EQ(dij.get_min_path(c, path), 21);
1161 IGT painted;
1162 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, painted), 21);
1163
1164 IGT tree;
1165 dij.compute_min_paths_tree(g, a, tree);
1166 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1167
1168 dij.compute_partial_min_paths_tree(g, a, c, tree);
1169 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1170}
1171
1172// The weight of an arc times a factor, with no default constructor.
1174{
1175 using Distance_Type = int;
1176
1177 int factor;
1178
1179 explicit Factor_Only_Distance(int f) noexcept : factor(f) {}
1180
1181 int operator()(IGT::Arc * a) const { return factor * a->get_info(); }
1182};
1183
1184// ArcHeap built its comparator by default, so such a distance did not
1185// compile with it.
1186TYPED_TEST(DijkstraHeapTest, TakesADistanceWithoutADefaultConstructor) {
1187 IGT g;
1188 auto a = g.insert_node(0);
1189 auto b = g.insert_node(1);
1190 auto c = g.insert_node(2);
1191 g.insert_arc(a, b, 3);
1192 g.insert_arc(b, c, 4);
1193 g.insert_arc(a, c, 20);
1194
1196 Dft_Show_Arc<IGT>, TypeParam::template Heap>
1197 dij(Factor_Only_Distance(3));
1198
1199 Path<IGT> path(g);
1200 EXPECT_EQ(dij.find_min_path(g, a, c, path), 21);
1201
1202 dij.paint_min_paths_tree(g, a);
1203 EXPECT_EQ(dij.get_distance(c), 21);
1204 EXPECT_EQ(dij.get_min_path(c, path), 21);
1205 IGT painted;
1206 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, painted), 21);
1207
1208 IGT tree;
1209 dij.compute_min_paths_tree(g, a, tree);
1210 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1211
1212 dij.compute_partial_min_paths_tree(g, a, c, tree);
1213 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1214}
1215
1216// Total built its distance with Distance(), so it could take neither one
1217// with state nor one without a default constructor.
1218TEST(DijkstraTotal, UsesTheDistanceItWasGiven) {
1219 IGT g;
1220 auto a = g.insert_node(0);
1221 auto b = g.insert_node(1);
1222 auto c = g.insert_node(2);
1223 auto ab = g.insert_arc(a, b, 3);
1224 auto bc = g.insert_arc(b, c, 4);
1225
1229 doubled(Factor_Only_Distance(2));
1230 for (auto arc : {ab, bc})
1231 {
1232 EXPECT_TRUE(plain(arc));
1233 EXPECT_TRUE(tripled(arc));
1234 EXPECT_TRUE(doubled(arc));
1235 }
1236 EXPECT_EQ(plain.dist, 3 + 4);
1237 EXPECT_EQ(tripled.dist, 3 * (3 + 4));
1238 EXPECT_EQ(doubled.dist, 2 * (3 + 4));
1239}
1240
1241// Total::operator() was noexcept, so what the distance threw ended the
1242// program.
1243TEST(DijkstraTotal, WhatTheDistanceThrowsReachesTheCaller) {
1244 IGT g;
1245 auto a = g.insert_node(0);
1246 auto b = g.insert_node(1);
1247 auto c = g.insert_node(2);
1248 auto ab = g.insert_arc(a, b, 0); // the index of its weight
1249 auto bc = g.insert_arc(b, c, 1);
1250 const std::vector<long> weights = {3, 4};
1251
1252 Distance_Failure failure;
1253 failure.fail_at = 2;
1255 total(Table_Distance(weights, &failure));
1256 EXPECT_TRUE(total(ab));
1257 EXPECT_THROW(total(bc), std::runtime_error);
1258 EXPECT_EQ(total.dist, 3);
1259}
1260
1261// copy_painted_min_paths_tree() returned 0: Copy_Graph added the weights
1262// up in its own copy of the filter.
1263TYPED_TEST(DijkstraHeapTest, CopyOfThePaintedTreeReturnsItsWeight) {
1264 GT g;
1265 Node* n0 = g.insert_node(0);
1266 Node* n1 = g.insert_node(1);
1267 Node* n2 = g.insert_node(2);
1268 Node* n3 = g.insert_node(3);
1269 g.insert_arc(n0, n1, 1.0);
1270 g.insert_arc(n0, n2, 4.0);
1271 g.insert_arc(n1, n2, 2.0);
1272 g.insert_arc(n1, n3, 5.0);
1273 g.insert_arc(n2, n3, 1.0);
1274
1276 dij.paint_min_paths_tree(g, n0);
1277
1278 GT tree;
1279 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, tree), 4.0); // 1 + 2 + 1
1280 EXPECT_EQ(tree.get_num_arcs(), 3u);
1281}
1282
1283// A distance with state gives in every method the costs of an independent
1284// oracle.
1285TYPED_TEST(DijkstraHeapTest, DistanceWithStateMatchesAnOracle) {
1286 for (unsigned seed = 1; seed <= 3; ++seed)
1287 {
1288 Weighted_Graph w(30, 60, seed);
1289 auto s = w.nodes[0];
1290 auto dist = w.distances_from(s);
1291 ASSERT_GT(dist.size(), 1u) << "seed " << seed;
1292
1294
1295 for (auto t : w.nodes)
1296 {
1297 if (t == s)
1298 continue; // the path of no arc; see PathToSelf
1299
1300 Path<IGT> path(w.g);
1301 const long cost = dij.find_min_path(w.g, s, t, path);
1302 if (not dist.contains(t))
1303 {
1304 EXPECT_EQ(cost, std::numeric_limits<long>::max());
1305 continue;
1306 }
1307 EXPECT_EQ(cost, dist[t]) << "seed " << seed;
1308 EXPECT_EQ(w.cost_of(path, s, t), dist[t]) << "seed " << seed;
1309 }
1310
1311 dij.paint_min_paths_tree(w.g, s);
1312 for (auto [t, d] : dist)
1313 {
1314 EXPECT_EQ(dij.get_distance(t), d) << "seed " << seed;
1315 Path<IGT> path(w.g);
1316 EXPECT_EQ(dij.get_min_path(t, path), d) << "seed " << seed;
1317 EXPECT_EQ(w.cost_of(path, s, t), d) << "seed " << seed;
1318 }
1319
1320 IGT painted;
1321 const long painted_weight = dij.copy_painted_min_paths_tree(w.g, painted);
1322 EXPECT_EQ(painted.get_num_arcs(), dist.size() - 1);
1323 EXPECT_EQ(painted_weight, w.weight_of(painted)) << "seed " << seed;
1324
1325 IGT tree;
1326 dij.compute_min_paths_tree(w.g, s, tree);
1327 EXPECT_EQ(tree.get_num_nodes(), dist.size());
1328 for (auto [t, d] : dist)
1329 {
1330 Path<IGT> path(w.g);
1331 EXPECT_EQ(dij.get_min_path(tree, t, path), d) << "seed " << seed;
1332 EXPECT_EQ(w.cost_of(path, s, t), d) << "seed " << seed;
1333 }
1334
1335 for (auto [t, d] : dist)
1336 {
1337 IGT partial;
1338 dij.compute_partial_min_paths_tree(w.g, s, t, partial);
1339 Path<IGT> path(w.g);
1340 EXPECT_EQ(dij.get_min_path(partial, t, path), d) << "seed " << seed;
1341 }
1342 }
1343}
1344
1345// The cookies were allocated at the start and freed at the end, but not
1346// when the algorithm threw: a distance that failed leaked them. Each call
1347// of the distance fails in turn; the same object, on the same graph, must
1348// then give the right costs. LeakSanitizer reports any cookie left.
1349TYPED_TEST(DijkstraHeapTest, DistanceThatThrowsLeavesTheGraphReusable) {
1350 Weighted_Graph w(12, 24, 7);
1351 auto s = w.nodes[0];
1352 auto dist = w.distances_from(s);
1353 ASSERT_GT(dist.size(), 3u);
1354 auto far = std::max_element(dist.begin(), dist.end(),
1355 [](auto & x, auto & y)
1356 { return x.second < y.second; })->first;
1357
1358 Distance_Failure failure;
1359 Table_Dijkstra<TypeParam> dij{Table_Distance(w.weights, &failure)};
1360 IGT tree;
1361
1362 const std::vector<std::function<void()>> methods = {
1363 [&] {
1364 Path<IGT> path(w.g);
1365 EXPECT_EQ(dij.find_min_path(w.g, s, far, path), dist[far]);
1366 },
1367 [&] {
1368 dij.paint_min_paths_tree(w.g, s);
1369 for (auto [t, d] : dist)
1370 EXPECT_EQ(dij.get_distance(t), d);
1371 },
1372 [&] {
1373 dij.compute_min_paths_tree(w.g, s, tree);
1374 Path<IGT> path(w.g);
1375 EXPECT_EQ(dij.get_min_path(tree, far, path), dist[far]);
1376 },
1377 [&] {
1378 dij.compute_partial_min_paths_tree(w.g, s, far, tree);
1379 Path<IGT> path(w.g);
1380 EXPECT_EQ(dij.get_min_path(tree, far, path), dist[far]);
1381 }
1382 };
1383
1384 for (size_t m = 0; m < methods.size(); ++m)
1385 {
1386 failure = Distance_Failure();
1387 methods[m]();
1388 const long calls = failure.calls;
1389 ASSERT_GT(calls, 0) << "method " << m;
1390
1391 for (long k = 1; k <= calls; ++k)
1392 {
1393 failure.calls = 0;
1394 failure.fail_at = k;
1395 EXPECT_THROW(methods[m](), std::runtime_error)
1396 << "method " << m << ", call " << k;
1397
1398 failure.fail_at = 0;
1399 methods[m]();
1400 }
1401 }
1402}
1403
1404// The overflow of the report on 7.0.0: the cookies leaked when the sum of
1405// two weights overflowed.
1406TYPED_TEST(DijkstraHeapTest, OverflowLeavesTheGraphReusable) {
1407 IGT g;
1408 auto a = g.insert_node(0);
1409 auto b = g.insert_node(1);
1410 auto c = g.insert_node(2);
1411 auto big = g.insert_arc(a, b, INT_MAX - 10);
1412 g.insert_arc(b, c, 20);
1413
1415 IGT tree;
1416
1417 for (int m = 0; m < 4; ++m)
1418 {
1419 big->get_info() = INT_MAX - 10;
1420 Path<IGT> path(g);
1421 switch (m)
1422 {
1423 case 0:
1424 EXPECT_THROW(dij.find_min_path(g, a, c, path), std::overflow_error);
1425 break;
1426 case 1:
1427 EXPECT_THROW(dij.paint_min_paths_tree(g, a), std::overflow_error);
1428 break;
1429 case 2:
1430 EXPECT_THROW(dij.compute_min_paths_tree(g, a, tree),
1431 std::overflow_error);
1432 break;
1433 default:
1434 EXPECT_THROW(dij.compute_partial_min_paths_tree(g, a, c, tree),
1435 std::overflow_error);
1436 break;
1437 }
1438 EXPECT_FALSE(dij.is_painted()) << "method " << m;
1439
1440 big->get_info() = 1;
1441 EXPECT_EQ(dij.find_min_path(g, a, c, path), 21) << "method " << m;
1442 }
1443}
1444
1445// Computing a tree resets the bits and cookies of a previous painting, so
1446// the painting cannot be used any more.
1447TYPED_TEST(DijkstraHeapTest, ComputingATreeForgetsThePainting) {
1448 GT g;
1449 Node* n0 = g.insert_node(0);
1450 Node* n1 = g.insert_node(1);
1451 Node* n2 = g.insert_node(2);
1452 g.insert_arc(n0, n1, 1.0);
1453 g.insert_arc(n1, n2, 2.0);
1454
1456 dij.paint_min_paths_tree(g, n0);
1457 ASSERT_TRUE(dij.is_painted());
1458
1459 GT tree;
1460 dij.compute_min_paths_tree(g, n0, tree);
1461 EXPECT_FALSE(dij.is_painted());
1462 Path<GT> path(g);
1463 EXPECT_THROW(dij.get_min_path(n2, path), std::domain_error);
1464 EXPECT_THROW(dij.get_distance(n2), std::domain_error);
1465
1466 dij.paint_min_paths_tree(g, n0);
1467 EXPECT_EQ(dij.get_distance(n2), 3.0);
1468}
1469
1470// Checks that every node and arc of tree is mapped, both ways, to one of
1471// the graph with the same info.
1473{
1474 for (GT::Node_Iterator it(tree); it.has_curr(); it.next_ne())
1475 {
1476 auto tp = it.get_curr();
1477 auto gp = mapped_node<GT>(tp);
1478 ASSERT_NE(gp, nullptr);
1479 EXPECT_EQ(mapped_node<GT>(gp), tp);
1480 EXPECT_EQ(gp->get_info(), tp->get_info());
1481 }
1482 for (GT::Arc_Iterator it(tree); it.has_curr(); it.next_ne())
1483 {
1484 auto ta = it.get_curr();
1485 auto ga = mapped_arc<GT>(ta);
1486 ASSERT_NE(ga, nullptr);
1487 EXPECT_EQ(mapped_arc<GT>(ga), ta);
1488 EXPECT_EQ(ga->get_info(), ta->get_info());
1489 }
1490}
1491
1492// The arcs of the tree were mapped to the cookies freed at the end, not to
1493// the arcs of the graph.
1494TYPED_TEST(DijkstraHeapTest, TreeIsMappedToTheGraph) {
1495 GT g;
1496 Node* n0 = g.insert_node(0);
1497 Node* n1 = g.insert_node(1);
1498 Node* n2 = g.insert_node(2);
1499 Node* n3 = g.insert_node(3);
1500 g.insert_arc(n0, n1, 1.0);
1501 g.insert_arc(n0, n2, 4.0);
1502 g.insert_arc(n1, n2, 2.0);
1503 g.insert_arc(n1, n3, 5.0);
1504 g.insert_arc(n2, n3, 1.0);
1505
1507 GT tree;
1508 dij.compute_min_paths_tree(g, n0, tree);
1509 ASSERT_EQ(tree.get_num_arcs(), 3u);
1511
1512 dij.compute_partial_min_paths_tree(g, n0, n2, tree);
1514}
1515
1516// The copy of the painted tree was mapped through the cookies that held
1517// the painting: its nodes got the parents and its arcs the freed cookies,
1518// while get_distance() went on and returned 0.
1519TYPED_TEST(DijkstraHeapTest, CopyOfThePaintedTreeIsMappedAndEndsThePainting) {
1520 GT g;
1521 Node* n0 = g.insert_node(0);
1522 Node* n1 = g.insert_node(1);
1523 Node* n2 = g.insert_node(2);
1524 Node* n3 = g.insert_node(3);
1525 g.insert_arc(n0, n1, 1.0);
1526 g.insert_arc(n0, n2, 4.0);
1527 g.insert_arc(n1, n2, 2.0);
1528 g.insert_arc(n1, n3, 5.0);
1529 g.insert_arc(n2, n3, 1.0);
1530
1532 dij.paint_min_paths_tree(g, n0);
1533 ASSERT_EQ(dij.get_distance(n3), 4.0);
1534
1535 GT tree;
1536 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, tree), 4.0);
1537 ASSERT_EQ(tree.get_num_arcs(), 3u);
1539
1540 EXPECT_FALSE(dij.is_painted());
1541 EXPECT_THROW(dij.get_distance(n3), std::domain_error);
1542}
1543
1544// ========== GoogleTest main ==========
1545int main(int argc, char **argv)
1546{
1547 ::testing::InitGoogleTest(&argc, argv);
1548 return RUN_ALL_TESTS();
1549}
Dijkstra's shortest path algorithm.
::testing::Types< BinHeapTag, FibHeapTag > HeapTypes
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
int main()
long double w
Definition btreepic.C:153
int num_nodes
Definition btreepic.C:410
auto next() noexcept(noexcept(std::declval< I & >().next())) -> decltype(std::declval< I & >().next())
Move to the next item; what the underlying iterator throws.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
auto get_current_arc() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_graph.H:1843
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
typename BaseGraph::Arc Arc
Definition graph-dry.H:4381
typename BaseGraph::Node Node
Definition graph-dry.H:4380
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:117
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Paints on graph g the spanning tree of all shortest paths starting from start.
Definition Dijkstra.H:455
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
Definition Dijkstra.H:536
bool is_painted() const noexcept
Check if the graph has been painted.
bool has_computation() const noexcept
Check if a computation has been performed.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
GT::Node * compute_min_paths_tree(const GT &g, typename GT::Node *start, GT &tree)
Computes the spanning tree of all shortest paths from the start node.
Definition Dijkstra.H:185
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the partial spanning tree of all shortest paths from start that contains the path from start...
Definition Dijkstra.H:272
bool paint_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints on graph g the partial shortest paths tree starting from start and stopping when the end node ...
Definition Dijkstra.H:361
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Graph_Arc< double > Arc
The node class type.
Definition tpl_graph.H:444
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
Node * get_current_node() const
Return the current node of a path.
Definition tpl_graph.H:4420
Path on a graph.
Definition tpl_graph.H:3832
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Definition tpl_graph.H:4540
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
GT::Arc * get_current_arc_ne() const noexcept
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Factor_Only_Distance(int f) noexcept
int operator()(IGT::Arc *a) const
int operator()(IGT::Arc *a) const
Distance_Failure * failure
const std::vector< long > * weights
Table_Distance()=default
long operator()(IGT::Arc *a) const
Table_Distance(const std::vector< long > &w, Distance_Failure *f=nullptr) noexcept
long weight_of(IGT &tree) const
long weight(IGT::Arc *a) const
Weighted_Graph(const size_t num_nodes, const size_t num_arcs, const unsigned seed)
long cost_of(const Path< IGT > &path, IGT::Node *s, IGT::Node *t) const
std::vector< IGT::Node * > nodes
std::map< IGT::Node *, long > distances_from(IGT::Node *s)
std::vector< long > weights
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
DGT::Arc DArc
DGT::Node DNode
void expect_mapped_to_graph(GT &tree)
TYPED_TEST(DijkstraHeapTest, BasicShortestPath)
List_Graph< Graph_Node< int >, Graph_Arc< int > > IGT
TYPED_TEST_SUITE(DijkstraHeapTest, HeapTypes)
Generic graph and digraph implementations.