fix weighted shortest path result not stable (#1280)

Change-Id: Ic97d8c92bd97d6203b7b366a2c6e2f661932e05f
This commit is contained in:
zhoney 2020-11-25 19:08:44 +08:00 committed by GitHub
parent 9bc985c067
commit eb062b2b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -287,7 +287,10 @@ public class JsonSerializer implements Serializer {
@Override
public String writeWeightedPaths(WeightedPaths paths,
Iterator<Vertex> vertices) {
return JsonUtil.toJson(ImmutableMap.of("paths", paths.toMap(),
Map<Id, Map<String, Object>> pathMap = paths == null ?
ImmutableMap.of() :
paths.toMap();
return JsonUtil.toJson(ImmutableMap.of("paths", pathMap,
"vertices", vertices));
}

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
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.structure.HugeEdge;
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 class SingleSourceShortestPathTraverser extends HugeTraverser {
Map<Id, NodeWithWeight> sorted = CollectionUtil.sortByValue(
this.findingNodes, true);
double minWeight = 0;
Set<NodeWithWeight> newSources = new HashSet<>();
Set<NodeWithWeight> newSources = InsertionOrderUtil.newSet();
for (Map.Entry<Id, NodeWithWeight> entry : sorted.entrySet()) {
Id id = entry.getKey();
NodeWithWeight wn = entry.getValue();
@ -265,7 +267,7 @@ public class SingleSourceShortestPathTraverser extends HugeTraverser {
}
}
public static class WeightedPaths extends HashMap<Id, NodeWithWeight> {
public static class WeightedPaths extends LinkedHashMap<Id, NodeWithWeight> {
private static final long serialVersionUID = -313873642177730993L;