Neo4j 中单向 "IN" 或 "OUT" 的最短路径
Shortest path in one direction "IN" or "OUT" in Neo4j
我有兴趣找到最短路径,但只能在一个方向上。例如,我有下图:the graph
当我考虑"INCOMING"方向时,"A and D"之间的最短路径应该是"A-C-D"。如果我考虑 "OUTGOING" 方向,最短路径应该是 "A-F-E-D"
根据我的实现,只能考虑"BOTH"方向:
PathExpander<Object> expander = Traversal.pathExpanderForAllTypes(Reldir);
PathFinder<Path> finder=GraphAlgoFactory.shortestPath(expander,maxDepth, 1);
Path path = finder.findSinglePath("A","D");
当我使用 Reldir="IN" 时,我遇到了这个异常:
Java.lang.NullPointerException at org.neo4j.kernel.Traversal.pathToString(Traversal.java)
有没有办法像在 OrientDB 中那样在 Neo4j 中使用 "IN" 或 "OUT" 方向?
尝试使用 INCOMING 而不是 IN。
Source
我已经实施了解决方案。但是,我不确定这是否是最好的方法:
Direction Reldir = Direction.valueOf(relation_direction);
PathExpander<Object> expander = Traversal.pathExpanderForAllTypes(Reldir);
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, maxDepth,1);
Path path = finder.findSinglePath(first_node, second_node);
PathPrinter pathPrinter = new PathPrinter("name");
Traversal.pathToString(path, pathPrinter);
static class PathPrinter implements Traversal.PathDescriptor<Path> {
private final String nodePropertyKey;
public PathPrinter(String nodePropertyKey) {
this.nodePropertyKey = nodePropertyKey;
}
public String nodeRepresentation(Path path, Node node)
{
System.out.println(node.getProperty(nodePropertyKey, "").toString()+" ");
}
有什么更有效的解决方案吗?
我有兴趣找到最短路径,但只能在一个方向上。例如,我有下图:the graph
当我考虑"INCOMING"方向时,"A and D"之间的最短路径应该是"A-C-D"。如果我考虑 "OUTGOING" 方向,最短路径应该是 "A-F-E-D"
根据我的实现,只能考虑"BOTH"方向:
PathExpander<Object> expander = Traversal.pathExpanderForAllTypes(Reldir);
PathFinder<Path> finder=GraphAlgoFactory.shortestPath(expander,maxDepth, 1);
Path path = finder.findSinglePath("A","D");
当我使用 Reldir="IN" 时,我遇到了这个异常:
Java.lang.NullPointerException at org.neo4j.kernel.Traversal.pathToString(Traversal.java)
有没有办法像在 OrientDB 中那样在 Neo4j 中使用 "IN" 或 "OUT" 方向?
尝试使用 INCOMING 而不是 IN。 Source
我已经实施了解决方案。但是,我不确定这是否是最好的方法:
Direction Reldir = Direction.valueOf(relation_direction);
PathExpander<Object> expander = Traversal.pathExpanderForAllTypes(Reldir);
PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, maxDepth,1);
Path path = finder.findSinglePath(first_node, second_node);
PathPrinter pathPrinter = new PathPrinter("name");
Traversal.pathToString(path, pathPrinter);
static class PathPrinter implements Traversal.PathDescriptor<Path> {
private final String nodePropertyKey;
public PathPrinter(String nodePropertyKey) {
this.nodePropertyKey = nodePropertyKey;
}
public String nodeRepresentation(Path path, Node node)
{
System.out.println(node.getProperty(nodePropertyKey, "").toString()+" ");
}
有什么更有效的解决方案吗?