From eb062b2b026decd7b8256bda44004b193ee9b4a5 Mon Sep 17 00:00:00 2001 From: zhoney Date: Wed, 25 Nov 2020 19:08:44 +0800 Subject: [PATCH] fix weighted shortest path result not stable (#1280) Change-Id: Ic97d8c92bd97d6203b7b366a2c6e2f661932e05f --- .../java/com/baidu/hugegraph/serializer/JsonSerializer.java | 5 ++++- .../algorithm/SingleSourceShortestPathTraverser.java | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java index 6706080dd4..04680da683 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java @@ -287,7 +287,10 @@ public String writeWeightedPath(NodeWithWeight path, @Override public String writeWeightedPaths(WeightedPaths paths, Iterator vertices) { - return JsonUtil.toJson(ImmutableMap.of("paths", paths.toMap(), + Map> pathMap = paths == null ? + ImmutableMap.of() : + paths.toMap(); + return JsonUtil.toJson(ImmutableMap.of("paths", pathMap, "vertices", vertices)); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java index 053767fef3..9b89be5b62 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -36,6 +37,7 @@ import com.baidu.hugegraph.type.define.Directions; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.InsertionOrderUtil; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -168,7 +170,7 @@ public void forward() { Map sorted = CollectionUtil.sortByValue( this.findingNodes, true); double minWeight = 0; - Set newSources = new HashSet<>(); + Set newSources = InsertionOrderUtil.newSet(); for (Map.Entry entry : sorted.entrySet()) { Id id = entry.getKey(); NodeWithWeight wn = entry.getValue(); @@ -265,7 +267,7 @@ public int compareTo(NodeWithWeight other) { } } - public static class WeightedPaths extends HashMap { + public static class WeightedPaths extends LinkedHashMap { private static final long serialVersionUID = -313873642177730993L;