Skip to content

Commit eb062b2

Browse files
authored
fix weighted shortest path result not stable (#1280)
Change-Id: Ic97d8c92bd97d6203b7b366a2c6e2f661932e05f
1 parent 9bc985c commit eb062b2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ public String writeWeightedPath(NodeWithWeight path,
287287
@Override
288288
public String writeWeightedPaths(WeightedPaths paths,
289289
Iterator<Vertex> vertices) {
290-
return JsonUtil.toJson(ImmutableMap.of("paths", paths.toMap(),
290+
Map<Id, Map<String, Object>> pathMap = paths == null ?
291+
ImmutableMap.of() :
292+
paths.toMap();
293+
return JsonUtil.toJson(ImmutableMap.of("paths", pathMap,
291294
"vertices", vertices));
292295
}
293296

hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/SingleSourceShortestPathTraverser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.HashSet;
2525
import java.util.Iterator;
26+
import java.util.LinkedHashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Set;
@@ -36,6 +37,7 @@
3637
import com.baidu.hugegraph.type.define.Directions;
3738
import com.baidu.hugegraph.util.CollectionUtil;
3839
import com.baidu.hugegraph.util.E;
40+
import com.baidu.hugegraph.util.InsertionOrderUtil;
3941
import com.google.common.collect.ImmutableMap;
4042
import com.google.common.collect.ImmutableSet;
4143

@@ -168,7 +170,7 @@ public void forward() {
168170
Map<Id, NodeWithWeight> sorted = CollectionUtil.sortByValue(
169171
this.findingNodes, true);
170172
double minWeight = 0;
171-
Set<NodeWithWeight> newSources = new HashSet<>();
173+
Set<NodeWithWeight> newSources = InsertionOrderUtil.newSet();
172174
for (Map.Entry<Id, NodeWithWeight> entry : sorted.entrySet()) {
173175
Id id = entry.getKey();
174176
NodeWithWeight wn = entry.getValue();
@@ -265,7 +267,7 @@ public int compareTo(NodeWithWeight other) {
265267
}
266268
}
267269

268-
public static class WeightedPaths extends HashMap<Id, NodeWithWeight> {
270+
public static class WeightedPaths extends LinkedHashMap<Id, NodeWithWeight> {
269271

270272
private static final long serialVersionUID = -313873642177730993L;
271273

0 commit comments

Comments
 (0)