forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest_paths_test.cc
731 lines (693 loc) · 31.7 KB
/
shortest_paths_test.cc
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
724
725
726
727
728
729
730
731
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/graph/shortest_paths.h"
#include <algorithm>
#include <random>
#include <vector>
#include "absl/base/macros.h"
#include "absl/random/random.h"
#include "gtest/gtest.h"
#include "ortools/graph/strongly_connected_components.h"
#include "ortools/util/zvector.h"
namespace operations_research {
void CheckPathDataPair(const PathContainer& container,
const PathContainer& distance_container,
PathDistance expected_distance,
NodeIndex expected_predecessor, NodeIndex tail,
NodeIndex head) {
EXPECT_EQ(expected_distance, container.GetDistance(tail, head));
EXPECT_EQ(expected_distance, distance_container.GetDistance(tail, head));
EXPECT_EQ(expected_predecessor,
container.GetPenultimateNodeInPath(tail, head));
EXPECT_DEATH(distance_container.GetPenultimateNodeInPath(tail, head),
"Path not stored.");
// Checking path between tail and head.
std::vector<NodeIndex> paths;
container.GetPath(tail, head, &paths);
if (tail == head) {
EXPECT_GE(1, paths.size());
if (!paths.empty()) {
EXPECT_EQ(tail, paths.back());
}
} else if (!paths.empty()) {
EXPECT_EQ(tail, paths[0]);
NodeIndex current = head;
for (int i = paths.size() - 1; i >= 0; --i) {
EXPECT_EQ(current, paths[i]);
current = container.GetPenultimateNodeInPath(tail, current);
}
}
EXPECT_DEATH(distance_container.GetPath(tail, head, &paths),
"Path not stored.");
}
template <class GraphType>
void CheckPathDataRow(const GraphType& graph, const PathContainer& container,
const PathContainer& distance_container,
const NodeIndex expected_paths[],
const PathDistance expected_distances[], NodeIndex tail) {
int index = tail * graph.num_nodes();
for (typename GraphType::NodeIterator iterator(graph); iterator.Ok();
iterator.Next()) {
const NodeIndex head(iterator.Index());
CheckPathDataPair(container, distance_container, expected_distances[index],
expected_paths[index], tail, head);
++index;
}
}
template <class GraphType>
void CheckPathDataRowFromGraph(const GraphType& graph,
const PathContainer& container,
const PathContainer& distance_container,
const NodeIndex expected_paths[],
const PathDistance expected_distances[],
NodeIndex tail) {
int index = tail * graph.num_nodes();
for (typename GraphType::NodeIndex head : graph.AllNodes()) {
CheckPathDataPair(container, distance_container, expected_distances[index],
expected_paths[index], tail, head);
++index;
}
}
template <class GraphType>
void CheckPathData(const GraphType& graph, const PathContainer& container,
const PathContainer& distance_container,
const NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
for (typename GraphType::NodeIterator iterator(graph); iterator.Ok();
iterator.Next()) {
const NodeIndex tail(iterator.Index());
CheckPathDataRow(graph, container, distance_container, expected_paths,
expected_distances, tail);
}
}
template <class GraphType>
void CheckPathDataFromGraph(const GraphType& graph,
const PathContainer& container,
const PathContainer& distance_container,
const NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
for (typename GraphType::NodeIndex tail : graph.AllNodes()) {
CheckPathDataRowFromGraph(graph, container, distance_container,
expected_paths, expected_distances, tail);
}
}
#define BUILD_CONTAINERS() \
PathContainer container; \
PathContainer::BuildInMemoryCompactPathContainer(&container); \
PathContainer distance_container; \
PathContainer::BuildPathDistanceContainer(&distance_container)
template <class GraphType>
void TestShortestPathsFromGraph(const GraphType& graph,
const ZVector<PathDistance>& lengths,
const NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
const int kThreads = 10;
const NodeIndex source = typename GraphType::NodeIterator(graph).Index();
std::vector<NodeIndex> some_nodes;
int index = 0;
std::mt19937 randomizer(12345);
for (typename GraphType::NodeIterator iterator(graph); iterator.Ok();
iterator.Next()) {
if (absl::Bernoulli(randomizer, 1.0 / 2)) {
some_nodes.push_back(iterator.Index());
}
++index;
}
// All-pair shortest paths.
{
BUILD_CONTAINERS();
ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths, kThreads,
&container);
ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths, kThreads,
&distance_container);
CheckPathData(graph, container, distance_container, expected_paths,
expected_distances);
}
// One-to-all shortest paths.
{
BUILD_CONTAINERS();
ComputeOneToAllShortestPaths(graph, lengths, source, &container);
ComputeOneToAllShortestPaths(graph, lengths, source, &distance_container);
CheckPathDataRow(graph, container, distance_container, expected_paths,
expected_distances, source);
}
// Many-to-all shortest paths.
{
BUILD_CONTAINERS();
ComputeManyToAllShortestPathsWithMultipleThreads(graph, lengths, some_nodes,
kThreads, &container);
ComputeManyToAllShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, kThreads, &distance_container);
for (int i = 0; i < some_nodes.size(); ++i) {
CheckPathDataRow(graph, container, distance_container, expected_paths,
expected_distances, some_nodes[i]);
}
}
// Many-to-all shortest paths with duplicates.
{
BUILD_CONTAINERS();
std::vector<NodeIndex> sources(3, source);
ComputeManyToAllShortestPathsWithMultipleThreads(graph, lengths, sources,
kThreads, &container);
ComputeManyToAllShortestPathsWithMultipleThreads(
graph, lengths, sources, kThreads, &distance_container);
for (int i = 0; i < sources.size(); ++i) {
CheckPathDataRow(graph, container, distance_container, expected_paths,
expected_distances, sources[i]);
}
}
// One-to-many shortest paths.
{
BUILD_CONTAINERS();
ComputeOneToManyShortestPaths(graph, lengths, source, some_nodes,
&container);
ComputeOneToManyShortestPaths(graph, lengths, source, some_nodes,
&distance_container);
index = source * graph.num_nodes();
for (int i = 0; i < some_nodes.size(); ++i) {
CheckPathDataPair(container, distance_container,
expected_distances[index + some_nodes[i]],
expected_paths[index + some_nodes[i]], source,
some_nodes[i]);
}
}
// Many-to-many shortest paths.
{
BUILD_CONTAINERS();
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, some_nodes, kThreads, &container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, some_nodes, kThreads, &distance_container);
for (int i = 0; i < some_nodes.size(); ++i) {
index = some_nodes[i] * graph.num_nodes();
for (int j = 0; j < some_nodes.size(); ++j) {
CheckPathDataPair(container, distance_container,
expected_distances[index + some_nodes[j]],
expected_paths[index + some_nodes[j]], some_nodes[i],
some_nodes[j]);
}
}
}
}
template <class GraphType>
void TestShortestPathsFromGraph(const GraphType& graph,
const std::vector<PathDistance>& lengths,
const NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
const int kThreads = 32;
const typename GraphType::NodeIndex source = 0;
std::vector<typename GraphType::NodeIndex> some_nodes;
int index = 0;
std::mt19937 randomizer(12345);
for (const typename GraphType::NodeIndex node : graph.AllNodes()) {
if (absl::Bernoulli(randomizer, 1.0 / 2)) {
some_nodes.push_back(node);
}
++index;
}
// All-pair shortest paths.
{
BUILD_CONTAINERS();
ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths, kThreads,
&container);
ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths, kThreads,
&distance_container);
CheckPathDataFromGraph(graph, container, distance_container, expected_paths,
expected_distances);
}
// One-to-all shortest paths.
{
BUILD_CONTAINERS();
ComputeOneToAllShortestPaths(graph, lengths, source, &container);
ComputeOneToAllShortestPaths(graph, lengths, source, &distance_container);
CheckPathDataRowFromGraph(graph, container, distance_container,
expected_paths, expected_distances, source);
}
// Many-to-all shortest paths.
{
BUILD_CONTAINERS();
ComputeManyToAllShortestPathsWithMultipleThreads(graph, lengths, some_nodes,
kThreads, &container);
ComputeManyToAllShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, kThreads, &distance_container);
for (int i = 0; i < some_nodes.size(); ++i) {
CheckPathDataRowFromGraph(graph, container, distance_container,
expected_paths, expected_distances,
some_nodes[i]);
}
}
// Many-to-all shortest paths with duplicates.
{
BUILD_CONTAINERS();
std::vector<NodeIndex> sources(3, source);
ComputeManyToAllShortestPathsWithMultipleThreads(graph, lengths, sources,
kThreads, &container);
ComputeManyToAllShortestPathsWithMultipleThreads(
graph, lengths, sources, kThreads, &distance_container);
for (int i = 0; i < sources.size(); ++i) {
CheckPathDataRowFromGraph(graph, container, distance_container,
expected_paths, expected_distances, sources[i]);
}
}
// One-to-many shortest paths.
{
BUILD_CONTAINERS();
ComputeOneToManyShortestPaths(graph, lengths, source, some_nodes,
&container);
ComputeOneToManyShortestPaths(graph, lengths, source, some_nodes,
&distance_container);
index = source * graph.num_nodes();
for (int i = 0; i < some_nodes.size(); ++i) {
CheckPathDataPair(container, distance_container,
expected_distances[index + some_nodes[i]],
expected_paths[index + some_nodes[i]], source,
some_nodes[i]);
}
} // Many-to-many shortest paths.
{
BUILD_CONTAINERS();
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, some_nodes, kThreads, &container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, some_nodes, some_nodes, kThreads, &distance_container);
for (int i = 0; i < some_nodes.size(); ++i) {
index = some_nodes[i] * graph.num_nodes();
for (int j = 0; j < some_nodes.size(); ++j) {
CheckPathDataPair(container, distance_container,
expected_distances[index + some_nodes[j]],
expected_paths[index + some_nodes[j]], some_nodes[i],
some_nodes[j]);
}
}
}
}
#undef BUILD_CONTAINERS
template <class GraphType>
void TestShortestPaths(int num_nodes, int num_arcs, const NodeIndex arcs[][2],
const PathDistance arc_lengths[],
const NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
GraphType graph(num_nodes, num_arcs);
ZVector<PathDistance> lengths(0, num_arcs - 1);
for (int i = 0; i < num_arcs; ++i) {
lengths[graph.AddArc(arcs[i][0], arcs[i][1])] = arc_lengths[i];
}
TestShortestPathsFromGraph(graph, lengths, expected_paths,
expected_distances);
}
template <class GraphType>
void TestShortestPathsFromGraph(
int num_nodes, int num_arcs, const typename GraphType::NodeIndex arcs[][2],
const PathDistance arc_lengths[],
const typename GraphType::NodeIndex expected_paths[],
const PathDistance expected_distances[]) {
GraphType graph(num_nodes, num_arcs);
std::vector<PathDistance> lengths(num_arcs);
for (int i = 0; i < num_arcs; ++i) {
lengths[graph.AddArc(arcs[i][0], arcs[i][1])] = arc_lengths[i];
}
std::vector<typename GraphType::ArcIndex> permutation;
graph.Build(&permutation);
util::Permute(permutation, &lengths);
TestShortestPathsFromGraph(graph, lengths, expected_paths,
expected_distances);
}
// Series of shortest paths tests on small graphs.
// Empty fixture templates to collect the types of graphs on which
// we want to base the shortest paths template instances that we
// test.
template <typename GraphType>
class ShortestPathsDeathTest : public testing::Test {};
template <typename GraphType>
class ShortestPathsTest : public testing::Test {};
typedef testing::Types<StarGraph, ForwardStarGraph>
EbertGraphTypesForShortestPathsTesting;
TYPED_TEST_SUITE(ShortestPathsDeathTest,
EbertGraphTypesForShortestPathsTesting);
TYPED_TEST_SUITE(ShortestPathsTest, EbertGraphTypesForShortestPathsTesting);
template <typename GraphType>
class GraphShortestPathsDeathTest : public testing::Test {};
template <typename GraphType>
class GraphShortestPathsTest : public testing::Test {};
typedef testing::Types<ListGraph<>, StaticGraph<>, ReverseArcListGraph<>,
ReverseArcStaticGraph<>, ReverseArcMixedGraph<> >
GraphTypesForShortestPathsTesting;
TYPED_TEST_SUITE(GraphShortestPathsDeathTest,
GraphTypesForShortestPathsTesting);
TYPED_TEST_SUITE(GraphShortestPathsTest, GraphTypesForShortestPathsTesting);
// Test on an empty graph.
TYPED_TEST(ShortestPathsDeathTest, ShortestPathsEmptyGraph) {
const int kExpectedPaths[] = {};
const PathDistance kExpectedDistances[] = {};
TypeParam graph;
ZVector<PathDistance> lengths;
TestShortestPathsFromGraph(graph, lengths, kExpectedPaths,
kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, ShortestPathsEmptyGraph) {
const int kExpectedPaths[] = {};
const PathDistance kExpectedDistances[] = {};
TypeParam graph;
std::vector<PathDistance> lengths;
TestShortestPathsFromGraph(graph, lengths, kExpectedPaths,
kExpectedDistances);
}
// Test on a disconnected graph (set of nodes pointing to themselves).
TYPED_TEST(ShortestPathsDeathTest, ShortestPathsAllDisconnected) {
const NodeIndex kUnconnected = TypeParam::kNilNode;
const int kNodes = 3;
const NodeIndex kArcs[][2] = {{0, 0}, {1, 1}, {2, 2}};
const PathDistance kArcLengths[] = {0, 0, 0};
const int kExpectedPaths[] = {0, kUnconnected, kUnconnected, kUnconnected,
1, kUnconnected, kUnconnected, kUnconnected,
2};
const PathDistance kExpectedDistances[] = {0,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0};
TestShortestPaths<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths), kArcs,
kArcLengths, kExpectedPaths, kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, ShortestPathsAllDisconnected) {
const typename TypeParam::NodeIndex kUnconnected = -1;
const int kNodes = 3;
const typename TypeParam::NodeIndex kArcs[][2] = {{0, 0}, {1, 1}, {2, 2}};
const PathDistance kArcLengths[] = {0, 0, 0};
const int kExpectedPaths[] = {0, kUnconnected, kUnconnected, kUnconnected,
1, kUnconnected, kUnconnected, kUnconnected,
2};
const PathDistance kExpectedDistances[] = {0,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0};
TestShortestPathsFromGraph<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths),
kArcs, kArcLengths, kExpectedPaths,
kExpectedDistances);
}
// 1 1 1
// -> 0 ---> 2 ------> 4 <--- 1
// | | | |
// | |4 1| |
// | | 1 | 3|
// |1 ---> 3 ---> 5 <- |
// | || |
// --------------- ----------
//
TYPED_TEST(ShortestPathsDeathTest, ShortestPaths1) {
const int kNodes = 6;
const NodeIndex kArcs[][2] = {{0, 2}, {0, 3}, {1, 4}, {2, 4},
{3, 5}, {4, 5}, {5, 0}, {5, 1}};
const PathDistance kArcLengths[] = {1, 4, 1, 1, 1, 1, 1, 3};
const int kExpectedPaths[] = {5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 1, 4,
5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 2, 3,
5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 2, 4};
const PathDistance kExpectedDistances[] = {
4, 6, 1, 4, 2, 3, 3, 5, 4, 7, 1, 2, 3, 5, 4, 7, 1, 2,
2, 4, 3, 6, 4, 1, 2, 4, 3, 6, 4, 1, 1, 3, 2, 5, 3, 4};
TestShortestPaths<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths), kArcs,
kArcLengths, kExpectedPaths, kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, ShortestPaths1) {
const int kNodes = 6;
const typename TypeParam::NodeIndex kArcs[][2] = {
{0, 2}, {0, 3}, {1, 4}, {2, 4}, {3, 5}, {4, 5}, {5, 0}, {5, 1}};
const PathDistance kArcLengths[] = {1, 4, 1, 1, 1, 1, 1, 3};
const int kExpectedPaths[] = {5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 1, 4,
5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 2, 3,
5, 5, 0, 0, 2, 4, 5, 5, 0, 0, 2, 4};
const PathDistance kExpectedDistances[] = {
4, 6, 1, 4, 2, 3, 3, 5, 4, 7, 1, 2, 3, 5, 4, 7, 1, 2,
2, 4, 3, 6, 4, 1, 2, 4, 3, 6, 4, 1, 1, 3, 2, 5, 3, 4};
TestShortestPathsFromGraph<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths),
kArcs, kArcLengths, kExpectedPaths,
kExpectedDistances);
}
// 0
// ---
// | | 1 4
// -> 0 -----> 1 -----> 4 --
// || | ^ |
// ||3 |1 1| |
// || | | |
// | ------> 2 ------- |
// | |
// | 1 |
// ----------------------
TYPED_TEST(ShortestPathsDeathTest, ShortestPaths2) {
const int kNodes = 4;
const NodeIndex kArcs[][2] = {{0, 1}, {0, 0}, {0, 2}, {1, 2},
{1, 3}, {2, 3}, {3, 0}};
const PathDistance kArcLengths[] = {1, 0, 3, 1, 4, 1, 1};
const int kExpectedPaths[] = {0, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2};
const PathDistance kExpectedDistances[] = {0, 1, 2, 3, 3, 4, 1, 2,
2, 3, 4, 1, 1, 2, 3, 4};
TestShortestPaths<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths), kArcs,
kArcLengths, kExpectedPaths, kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, ShortestPaths2) {
const int kNodes = 4;
const typename TypeParam::NodeIndex kArcs[][2] = {
{0, 1}, {0, 0}, {0, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 0}};
const PathDistance kArcLengths[] = {1, 0, 3, 1, 4, 1, 1};
const int kExpectedPaths[] = {0, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2};
const PathDistance kExpectedDistances[] = {0, 1, 2, 3, 3, 4, 1, 2,
2, 3, 4, 1, 1, 2, 3, 4};
TestShortestPathsFromGraph<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths),
kArcs, kArcLengths, kExpectedPaths,
kExpectedDistances);
}
TYPED_TEST(ShortestPathsDeathTest, MismatchedData) {
TypeParam graph(2, 2);
graph.AddArc(0, 1);
graph.AddArc(1, 0);
ZVector<PathDistance> lengths(0, 0);
lengths[0] = 0;
PathContainer container;
PathContainer::BuildInMemoryCompactPathContainer(&container);
EXPECT_DEATH(ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths,
1, &container),
"Number of arcs in graph must match arc length vector size");
}
TYPED_TEST(GraphShortestPathsDeathTest, MismatchedData) {
TypeParam graph(2, 2);
graph.AddArc(0, 1);
graph.AddArc(1, 0);
std::vector<PathDistance> lengths = {0};
PathContainer container;
PathContainer::BuildInMemoryCompactPathContainer(&container);
EXPECT_DEATH(ComputeAllToAllShortestPathsWithMultipleThreads(graph, lengths,
1, &container),
"Number of arcs in graph must match arc length vector size");
}
// Test the case where some sources are not strongly connected to themselves.
TYPED_TEST(ShortestPathsDeathTest, SourceNotConnectedToItself) {
const int kNodes = 3;
const NodeIndex kArcs[][2] = {{1, 2}, {2, 2}};
const PathDistance kArcLengths[] = {1, 0};
const int kExpectedPaths[] = {-1, -1, -1, -1, -1, 1, -1, -1, 2};
const PathDistance kExpectedDistances[] = {kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
1,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0};
TestShortestPaths<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths), kArcs,
kArcLengths, kExpectedPaths, kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, SourceNotConnectedToItself) {
const int kNodes = 3;
const typename TypeParam::NodeIndex kArcs[][2] = {{1, 2}, {2, 2}};
const PathDistance kArcLengths[] = {1, 0};
const int kExpectedPaths[] = {-1, -1, -1, -1, -1, 1, -1, -1, 2};
const PathDistance kExpectedDistances[] = {kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
1,
kDisconnectedPathDistance,
kDisconnectedPathDistance,
0};
TestShortestPathsFromGraph<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths),
kArcs, kArcLengths, kExpectedPaths,
kExpectedDistances);
}
// Test the case where the graph is a multigraph, a graph with parallel arcs
// (arcs which have the same end nodes).
TYPED_TEST(ShortestPathsDeathTest, Multigraph) {
const int kNodes = 4;
const NodeIndex kArcs[][2] = {{0, 1}, {0, 1}, {0, 2}, {0, 2}, {1, 3},
{2, 3}, {1, 3}, {2, 3}, {3, 0}};
const PathDistance kArcLengths[] = {2, 1, 1, 2, 2, 2, 1, 1, 1};
const int kExpectedPaths[] = {3, 0, 0, 2, 3, 0, 0, 1, 3, 0, 0, 2, 3, 0, 0, 2};
const PathDistance kExpectedDistances[] = {3, 1, 1, 2, 2, 3, 3, 1,
2, 3, 3, 1, 1, 2, 2, 3};
TestShortestPaths<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths), kArcs,
kArcLengths, kExpectedPaths, kExpectedDistances);
}
TYPED_TEST(GraphShortestPathsDeathTest, Multigraph) {
const int kNodes = 4;
const typename TypeParam::NodeIndex kArcs[][2] = {
{0, 1}, {0, 1}, {0, 2}, {0, 2}, {1, 3}, {2, 3}, {1, 3}, {2, 3}, {3, 0}};
const PathDistance kArcLengths[] = {2, 3, 1, 2, 2, 2, 1, 1, 1};
const int kExpectedPaths[] = {3, 0, 0, 2, 3, 0, 0, 1, 3, 0, 0, 2, 3, 0, 0, 2};
const PathDistance kExpectedDistances[] = {3, 2, 1, 2, 2, 4, 3, 1,
2, 4, 3, 1, 1, 3, 2, 3};
TestShortestPathsFromGraph<TypeParam>(kNodes, ABSL_ARRAYSIZE(kArcLengths),
kArcs, kArcLengths, kExpectedPaths,
kExpectedDistances);
}
// Large test on a random strongly connected graph with 10,000,000 nodes and
// 50,000,000 arcs.
// Shortest paths are computed between 10 randomly chosen nodes.
TYPED_TEST(ShortestPathsTest, DISABLED_LargeRandomShortestPaths) {
const int kSize = 10000000;
const int kDegree = 4;
const int max_distance = 50;
const PathDistance kConnectionArcLength = 300;
std::mt19937 randomizer(12345);
TypeParam graph(kSize, kSize + kSize * kDegree);
ZVector<PathDistance> lengths(0, kSize + (kSize * kDegree) - 1);
for (int i = 0; i < kSize; ++i) {
const NodeIndex tail(absl::Uniform(randomizer, 0, kSize));
for (int j = 0; j < kDegree; ++j) {
const NodeIndex head(absl::Uniform(randomizer, 0, kSize));
const PathDistance length =
1 + absl::Uniform(randomizer, 0, max_distance);
lengths.Set(graph.AddArc(tail, head), length);
}
}
for (typename TypeParam::NodeIterator iterator(graph); iterator.Ok();) {
const NodeIndex node_index = iterator.Index();
iterator.Next();
if (iterator.Ok()) {
const NodeIndex next_node_index = iterator.Index();
lengths.Set(graph.AddArc(node_index, next_node_index),
kConnectionArcLength);
} else {
const NodeIndex first_node_index =
typename TypeParam::NodeIterator(graph).Index();
lengths.Set(graph.AddArc(node_index, first_node_index),
kConnectionArcLength);
}
}
std::vector<ConnectedComponent> components;
FindStronglyConnectedComponents(graph, &components);
CHECK_EQ(1, components.size());
CHECK_EQ(kSize, components[0].size());
const int kSourceSize = 10;
const int source_size = std::min(graph.num_nodes(), kSourceSize);
std::vector<NodeIndex> sources(source_size, 0);
for (int i = 0; i < source_size; ++i) {
sources[i] = absl::Uniform(randomizer, 0, graph.num_nodes());
}
const int kThreads = 10;
PathContainer container;
PathContainer::BuildInMemoryCompactPathContainer(&container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, sources, sources, kThreads, &container);
PathContainer distance_container;
PathContainer::BuildPathDistanceContainer(&distance_container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, sources, sources, kThreads, &distance_container);
for (int tail = 0; tail < sources.size(); ++tail) {
for (int head = 0; head < sources.size(); ++head) {
EXPECT_NE(TypeParam::kNilNode, container.GetPenultimateNodeInPath(
sources[tail], sources[head]));
EXPECT_NE(kDisconnectedPathDistance,
container.GetDistance(sources[tail], sources[head]));
EXPECT_NE(kDisconnectedPathDistance,
distance_container.GetDistance(sources[tail], sources[head]));
}
}
}
TYPED_TEST(GraphShortestPathsTest, DISABLED_LargeRandomShortestPaths) {
const int kSize = 10000000;
const int kDegree = 4;
const int max_distance = 50;
const PathDistance kConnectionArcLength = 300;
std::mt19937 randomizer(12345);
TypeParam graph(kSize, kSize + kSize * kDegree);
std::vector<PathDistance> lengths;
for (int i = 0; i < kSize; ++i) {
const typename TypeParam::NodeIndex tail(
absl::Uniform(randomizer, 0, kSize));
for (int j = 0; j < kDegree; ++j) {
const typename TypeParam::NodeIndex head(
absl::Uniform(randomizer, 0, kSize));
const PathDistance length =
1 + absl::Uniform(randomizer, 0, max_distance);
graph.AddArc(tail, head);
lengths.push_back(length);
}
}
typename TypeParam::NodeIndex prev_index = -1;
typename TypeParam::NodeIndex first_index = -1;
for (const typename TypeParam::NodeIndex node_index : graph.AllNodes()) {
if (prev_index != -1) {
graph.AddArc(prev_index, node_index);
lengths.push_back(kConnectionArcLength);
} else {
first_index = node_index;
}
prev_index = node_index;
}
graph.AddArc(prev_index, first_index);
lengths.push_back(kConnectionArcLength);
std::vector<typename TypeParam::ArcIndex> permutation;
graph.Build(&permutation);
util::Permute(permutation, &lengths);
std::vector<std::vector<typename TypeParam::NodeIndex> > components;
::FindStronglyConnectedComponents(graph.num_nodes(), graph, &components);
CHECK_EQ(1, components.size());
CHECK_EQ(kSize, components[0].size());
const int kSourceSize = 10;
const int source_size = std::min(graph.num_nodes(), kSourceSize);
std::vector<typename TypeParam::NodeIndex> sources(source_size, 0);
for (int i = 0; i < source_size; ++i) {
sources[i] = absl::Uniform(randomizer, 0, graph.num_nodes());
}
const int kThreads = 10;
PathContainer container;
PathContainer::BuildInMemoryCompactPathContainer(&container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, sources, sources, kThreads, &container);
PathContainer distance_container;
PathContainer::BuildPathDistanceContainer(&distance_container);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, lengths, sources, sources, kThreads, &distance_container);
for (int tail = 0; tail < sources.size(); ++tail) {
for (int head = 0; head < sources.size(); ++head) {
EXPECT_NE(
-1, container.GetPenultimateNodeInPath(sources[tail], sources[head]));
EXPECT_NE(kDisconnectedPathDistance,
container.GetDistance(sources[tail], sources[head]));
EXPECT_NE(kDisconnectedPathDistance,
distance_container.GetDistance(sources[tail], sources[head]));
}
}
}
} // namespace operations_research