[英]Neo4j 3.1 traversal API, how to find shortest Path between two nodes?
I am using Traversal API, but seems Traversal.expanderForTypes() deprecated and I do not know how to find shortest path between two nodes. 我正在使用Traversal API,但似乎不赞成Traversal.expanderForTypes(),并且我不知道如何找到两个节点之间的最短路径。
My method 我的方法
public Iterable<Path> shortestPath(long firstNodeId, long secondNodeId) {
Node firstNode = null;
Node secondNode = null;
try (Transaction tx = mainServiceBean.getDatabaseService().beginTx()) {
firstNode = mainServiceBean.getDatabaseService().getNodeById(firstNodeId);
secondNode = mainServiceBean.getDatabaseService().getNodeById(secondNodeId);
PathExpander expander = Traversal.expanderForTypes();//?
PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(expander, 4, 4);
tx.success();
return shortestPath.findAllPaths(firstNode, secondNode);
}
}
My nodes are cities and relationship between like this 我的节点是城市,彼此之间的关系是这样的
Node nodeACity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);
nodeACity .setProperty(City.NAME, CityNames.ACiTY.name());
Node nodeBCity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);
nodeBCity.setProperty(City.NAME, CityNames.BCity.name());
Relationship ab= nodeACity .createRelationshipTo(nodeBCity , NodesRelationship.DISTANCE_TO);
ab.setProperty("distance", 124.31);
Relationship ba= nodeBCity .createRelationshipTo(nodeACity , NodesRelationship.DISTANCE_TO);
ba.setProperty("distance", 124.31);
So relationship have property distance with value. 因此关系具有属性距离与价值。
How can use traversal API from neo4j 3? 如何使用neo4j 3中的遍历API? Seems a lot of changed. 似乎发生了很大变化。
You'll find couple of predefined PathExpander
implementations in PathExpanders
. 您将在PathExpanders
找到几个预定义的PathExpander
实现。
Is there a specific reason why you're modelling DISTANCE_TO
twice for given nodes a and b? 对于给定的节点a和b,为什么要对DISTANCE_TO
两次建模,是否有特定的原因? In most cases it's preferable to only have one relationship and ignore the direction during traversal. 在大多数情况下,最好仅具有一种关系,而在遍历期间忽略方向。 In this case you can use 在这种情况下,您可以使用
PathExpander expander = PathExpanders.forType(NodesRelationship.DISTANCE_TO);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.