Skip to content

Commit

Permalink
fix weighted shortest path result not stable (apache#1280)
Browse files Browse the repository at this point in the history
Change-Id: Ic97d8c92bd97d6203b7b366a2c6e2f661932e05f
  • Loading branch information
zhoney authored and tmljob committed Dec 10, 2020
1 parent f7b8b87 commit 5b6d58f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ public String writeWeightedPath(NodeWithWeight path,
@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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -168,7 +170,7 @@ public void forward() {
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();
Expand Down Expand Up @@ -265,7 +267,7 @@ public int compareTo(NodeWithWeight other) {
}
}

public static class WeightedPaths extends HashMap<Id, NodeWithWeight> {
public static class WeightedPaths extends LinkedHashMap<Id, NodeWithWeight> {

private static final long serialVersionUID = -313873642177730993L;

Expand Down

0 comments on commit 5b6d58f

Please sign in to comment.