28
28
import org .neo4j .gds .async .AsyncAlgorithmCaller ;
29
29
import org .neo4j .gds .collections .ha .HugeLongArray ;
30
30
import org .neo4j .gds .collections .haa .HugeAtomicLongArray ;
31
+ import org .neo4j .gds .core .utils .paged .ParalleLongPageCreator ;
31
32
import org .neo4j .gds .core .utils .progress .JobId ;
32
33
import org .neo4j .gds .dag .longestPath .DagLongestPath ;
33
34
import org .neo4j .gds .dag .longestPath .DagLongestPathParameters ;
@@ -115,6 +116,10 @@ public CompletableFuture<Stream<AllShortestPathsStreamResult>> allShortestPaths(
115
116
AllShortestPathsParameters parameters ,
116
117
JobId jobId
117
118
) {
119
+ if (graph .isEmpty ()) {
120
+ return CompletableFuture .completedFuture (Stream .empty ());
121
+ }
122
+
118
123
// Create ProgressTracker
119
124
// `allShortestPaths` doesn't use progress tracker (yet 🤔)
120
125
var progressTracker = progressTrackerFactory .nullTracker ();
@@ -142,6 +147,11 @@ public CompletableFuture<BellmanFordResult> bellmanFord(
142
147
JobId jobId ,
143
148
boolean logProgress
144
149
) {
150
+ // If the input graph is empty return a completed future with empty result
151
+ if (graph .isEmpty ()) {
152
+ return CompletableFuture .completedFuture (BellmanFordResult .EMPTY );
153
+ }
154
+
145
155
// Create ProgressTracker
146
156
var progressTracker = progressTrackerFactory .create (
147
157
BellmanFordProgressTask .create (),
@@ -174,6 +184,11 @@ public CompletableFuture<HugeLongArray> breadthFirstSearch(
174
184
JobId jobId ,
175
185
boolean logProgress
176
186
) {
187
+ // If the input graph is empty return a completed future with empty result
188
+ if (graph .isEmpty ()) {
189
+ return CompletableFuture .completedFuture (HugeLongArray .newArray (0L ));
190
+ }
191
+
177
192
// Create ProgressTracker
178
193
var progressTracker = progressTrackerFactory .create (
179
194
BFSProgressTask .create (),
@@ -211,6 +226,11 @@ public CompletableFuture<PathFindingResult> deltaStepping(
211
226
JobId jobId ,
212
227
boolean logProgress
213
228
) {
229
+ // If the input graph is empty return a completed future with empty result
230
+ if (graph .isEmpty ()) {
231
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
232
+ }
233
+
214
234
// Create ProgressTracker
215
235
var progressTracker = progressTrackerFactory .create (
216
236
DeltaSteppingProgressTask .create (),
@@ -235,8 +255,12 @@ public CompletableFuture<HugeLongArray> depthFirstSearch(
235
255
TraversalParameters parameters ,
236
256
JobId jobId ,
237
257
boolean logProgress
238
-
239
258
) {
259
+ // If the input graph is empty return a completed future with empty result
260
+ if (graph .isEmpty ()) {
261
+ return CompletableFuture .completedFuture (HugeLongArray .newArray (0L ));
262
+ }
263
+
240
264
// Create ProgressTracker
241
265
var progressTracker = progressTrackerFactory .create (
242
266
DFSProgressTask .create (),
@@ -272,6 +296,11 @@ public CompletableFuture<SpanningTree> kSpanningTree(
272
296
JobId jobId ,
273
297
boolean logProgress
274
298
) {
299
+ // If the input graph is empty return a completed future with empty result
300
+ if (graph .isEmpty ()) {
301
+ return CompletableFuture .completedFuture (SpanningTree .EMPTY );
302
+ }
303
+
275
304
// Create ProgressTracker
276
305
var progressTracker = progressTrackerFactory .create (
277
306
KSpanningTreeTask .create (graph .relationshipCount ()),
@@ -303,6 +332,11 @@ public CompletableFuture<PathFindingResult> longestPath(
303
332
JobId jobId ,
304
333
boolean logProgress
305
334
) {
335
+ // If the input graph is empty return a completed future with empty result
336
+ if (graph .isEmpty ()) {
337
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
338
+ }
339
+
306
340
// Create ProgressTracker
307
341
var progressTracker = progressTrackerFactory .create (
308
342
LongestPathTask .create (graph .nodeCount ()),
@@ -332,6 +366,11 @@ public CompletableFuture<Stream<long[]>> randomWalk(
332
366
JobId jobId ,
333
367
boolean logProgress
334
368
) {
369
+ // If the input graph is empty return a completed future with empty result
370
+ if (graph .isEmpty ()) {
371
+ return CompletableFuture .completedFuture (Stream .empty ());
372
+ }
373
+
335
374
// Create ProgressTracker
336
375
var progressTracker = progressTrackerFactory .create (
337
376
RandomWalkProgressTask .create (graph ),
@@ -361,6 +400,13 @@ public CompletableFuture<HugeAtomicLongArray> randomWalkCountingNodeVisits(
361
400
JobId jobId ,
362
401
boolean logProgress
363
402
) {
403
+ // If the input graph is empty return a completed future with empty result
404
+ if (graph .isEmpty ()) {
405
+ return CompletableFuture .completedFuture (HugeAtomicLongArray .of (
406
+ 0 ,
407
+ ParalleLongPageCreator .passThrough (parameters .concurrency ())
408
+ ));
409
+ }
364
410
365
411
// Create ProgressTracker
366
412
var progressTracker = progressTrackerFactory .create (
@@ -392,6 +438,11 @@ public CompletableFuture<PrizeSteinerTreeResult> pcst(
392
438
JobId jobId ,
393
439
boolean logProgress
394
440
) {
441
+ // If the input graph is empty return a completed future with empty result
442
+ if (graph .isEmpty ()) {
443
+ return CompletableFuture .completedFuture (PrizeSteinerTreeResult .EMPTY );
444
+ }
445
+
395
446
// Create ProgressTracker
396
447
var progressTracker = progressTrackerFactory .create (
397
448
PCSTProgressTrackerTaskCreator .progressTask (graph .nodeCount (), graph .relationshipCount ()),
@@ -421,6 +472,11 @@ public CompletableFuture<PathFindingResult> singlePairShortestPathAStar(
421
472
JobId jobId ,
422
473
boolean logProgress
423
474
) {
475
+ // If the input graph is empty return a completed future with empty result
476
+ if (graph .isEmpty ()) {
477
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
478
+ }
479
+
424
480
// Create ProgressTracker
425
481
var progressTracker = progressTrackerFactory .create (
426
482
RelationshipCountProgressTaskFactory .create (AlgorithmLabel .AStar , graph .relationshipCount ()),
@@ -450,6 +506,11 @@ public CompletableFuture<PathFindingResult> singlePairShortestPathDijkstra(
450
506
JobId jobId ,
451
507
boolean logProgress
452
508
) {
509
+ // If the input graph is empty return a completed future with empty result
510
+ if (graph .isEmpty ()) {
511
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
512
+ }
513
+
453
514
// Create ProgressTracker
454
515
var progressTracker = progressTrackerFactory .create (
455
516
RelationshipCountProgressTaskFactory .create (AlgorithmLabel .Dijkstra , graph .relationshipCount ()),
@@ -482,6 +543,11 @@ public CompletableFuture<PathFindingResult> singlePairShortestPathYens(
482
543
JobId jobId ,
483
544
boolean logProgress
484
545
) {
546
+ // If the input graph is empty return a completed future with empty result
547
+ if (graph .isEmpty ()) {
548
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
549
+ }
550
+
485
551
// Create ProgressTracker
486
552
var progressTracker = progressTrackerFactory .create (
487
553
YensProgressTask .create (
@@ -514,6 +580,11 @@ public CompletableFuture<PathFindingResult> singleSourceShortestPathDijkstra(
514
580
JobId jobId ,
515
581
boolean logProgress
516
582
) {
583
+ // If the input graph is empty return a completed future with empty result
584
+ if (graph .isEmpty ()) {
585
+ return CompletableFuture .completedFuture (PathFindingResult .EMPTY );
586
+ }
587
+
517
588
// Create ProgressTracker
518
589
var progressTracker = progressTrackerFactory .create (
519
590
RelationshipCountProgressTaskFactory .create (AlgorithmLabel .SingleSourceDijkstra , graph .relationshipCount ()),
@@ -545,6 +616,11 @@ public CompletableFuture<SpanningTree> spanningTree(
545
616
JobId jobId ,
546
617
boolean logProgress
547
618
) {
619
+ // If the input graph is empty return a completed future with empty result
620
+ if (graph .isEmpty ()) {
621
+ return CompletableFuture .completedFuture (SpanningTree .EMPTY );
622
+ }
623
+
548
624
// Create ProgressTracker
549
625
var progressTracker = progressTrackerFactory .create (
550
626
RelationshipCountProgressTaskFactory .create (AlgorithmLabel .SpanningTree , graph .relationshipCount ()),
@@ -575,6 +651,10 @@ public CompletableFuture<SteinerTreeResult> steinerTree(
575
651
JobId jobId ,
576
652
boolean logProgress
577
653
) {
654
+ // If the input graph is empty return a completed future with empty result
655
+ if (graph .isEmpty ()) {
656
+ return CompletableFuture .completedFuture (SteinerTreeResult .EMPTY );
657
+ }
578
658
579
659
// Create ProgressTracker
580
660
var progressTracker = progressTrackerFactory .create (
@@ -616,6 +696,11 @@ public CompletableFuture<TopologicalSortResult> topologicalSort(
616
696
JobId jobId ,
617
697
boolean logProgress
618
698
) {
699
+ // If the input graph is empty return a completed future with empty result
700
+ if (graph .isEmpty ()) {
701
+ return CompletableFuture .completedFuture (TopologicalSortResult .EMPTY );
702
+ }
703
+
619
704
// Create ProgressTracker
620
705
var progressTracker = progressTrackerFactory .create (
621
706
TopSortTask .create (graph ),
0 commit comments