From 372faf63eb51f6bdddf2a99d7b07364e29231fa6 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 12 Aug 2021 11:49:46 +0800 Subject: [PATCH] improve shortest path by traversing from less direction Change-Id: I630dfa362882e4721a33a0dc42c3e32ea7477022 --- .../algorithm/ShortestPathTraverser.java | 16 +++++++--------- .../records/DoubleWayMultiPathsRecords.java | 5 +++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java index 151928964b..a2a5b1319e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/ShortestPathTraverser.java @@ -120,16 +120,10 @@ public PathSet allShortestPaths(Id sourceV, Id targetV, Directions dir, Traverser traverser = new Traverser(sourceV, targetV, dir, labelMap, degree, skipDegree, capacity); while (true) { + paths = traverser.forward() ? + traverser.forward(true) : traverser.backward(true); // Found, reach max depth or reach capacity, stop searching - if (!(paths = traverser.forward(true)).isEmpty() || - --depth <= 0) { - break; - } - checkCapacity(traverser.capacity, traverser.accessed(), - "shortest path"); - - if (!(paths = traverser.backward(true)).isEmpty() || - --depth <= 0) { + if (!paths.isEmpty() || --depth <= 0) { break; } checkCapacity(traverser.capacity, traverser.accessed(), @@ -238,6 +232,10 @@ public PathSet backward(boolean all) { return results; } + public boolean forward() { + return this.record.lessSources(); + } + private boolean superNode(Id vertex, Directions direction) { if (this.skipDegree <= 0L) { return false; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java index ad76673e33..4ba60eede5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/records/DoubleWayMultiPathsRecords.java @@ -112,6 +112,11 @@ public PathSet findPath(Id target, Function filter, return results; } + public boolean lessSources() { + return this.sourceRecords.peek().size() <= + this.targetRecords.peek().size(); + } + @Override public long accessed() { return this.accessed;