在neo4j中找到k-shortest路径

时间:2017-07-17 20:51:27

标签: neo4j shortest-path

我正在使用GraphAlgoFactory.shortestPath(PathExpander expander,int maxDepth,int maxHitCount)来查找k最短路径,但问题是,我没有得到两个节点之间的直接路径。例如,我有像

这样的关系
n1--n2
n1--n3
n3--n2

如果我试图找到n1和n2之间的路径,我只得到单个路径为n1 - n3 - n2,但实际上,有两条路径,其中一条是从n1到n2的直接路径。

CustomPathExpander expander = toExpander(constraints, db, Collections.<FakeNode>emptyList());

List<Path> result = new LinkedList<Path>();
for (Path path : GraphAlgoFactory.shortestPath(expander, 3, 3).findAllPaths(n1, n2)) 
{
result.add(path);
}


static CustomPathExpander toExpander(String constraints, FakeGraphDatabase db, Iterable<FakeNode> extraNodes) {
    return toExpander(toMap(constraints), db, extraNodes);
}


static CustomPathExpander toExpander(Map<String,Object> c, FakeGraphDatabase db, Iterable<FakeNode> extraNodes) {
    Map<String,String> directions = (Map<String, String>) (c == null ? null : c.get("dir"));
    Map<String,Object> constraints = (Map<String, Object>) (c == null ? null : c.get("c"));
    Map<String,Object> inline = (Map<String,Object>)(c == null ? null : c.get("inline"));

    DirectionContraints d = new DirectionContraints(directions);
    IPathConstraint path = PathConstraints.parse(constraints);
    InlineRelationships rel = InlineRelationships.of(inline, db);

    return new CustomPathExpander(d, path, rel, extraNodes, c != null && c.get("acyclic") == Boolean.TRUE);
}

0 个答案:

没有答案