gpt4 book ai didi

java - 如何排除某些节点

转载 作者:行者123 更新时间:2023-12-01 13:20:32 25 4
gpt4 key购买 nike

有没有办法根据属性从 Neo4j Dijkstra 算法中排除某些特定节点?

我知道,我可以在方法 forTypeAndDirection 中设置允许的关系和方向,但这对我的情况没有帮助。

假设我有以下代码:

try (Transaction tx = graphDb.beginTx()) {

Node startNode = graphDb.getNodeById(12353);
Node endNode = graphDb.getNodeById(12356);

CostEvaluator<Double> costEvaluator = new CostEvaluator<Double>() {
@Override
public Double getCost(Relationship relationship, Direction direction) {
Integer cost = Integer.parseInt(relationship.getProperty("cost").toString());
return cost.doubleValue();
}
};

PathFinder<WeightedPath> finder = GraphAlgoFactory.dijkstra(
PathExpanders.forTypeAndDirection( RelationshipTypes.RELATED, Direction.OUTGOING), costEvaluator );

WeightedPath path = finder.findSinglePath( startNode, endNode );
System.out.println(path.length());
tx.success();
}
}

当 Dijkstra 通过属性名称为“London”的节点时,如何停止执行该路径并在其他地方继续执行?

最佳答案

我最近遇到了类似的问题,我需要自定义 PathExpander 来执行一些遍历。我做了这样的事情(我对原始代码进行了一些修改以适合您的情况,但它仍然可能有错误,所以请仔细查看):

final private static class FilteringExpander implements PathExpander {
private final Direction direction;

private FilteringExpander(final Direction direction) {
this.direction = direction;
}

public FilteringExpander() {
this.direction = Direction.OUTGOING;
}

@Override
public Iterable<Relationship> expand(Path neoPath, BranchState state) {
if (!neoPath.endNode().getProperty("name").equals("London")) {
return neoPath.endNode().getRelationships(RelationshipTypes.RELATED, direction);
} else {
return Collections.emptyList();
}
}

@Override
public PathExpander reverse() {
return new FilteringExpander(direction.reverse());
}
}

希望它足够清楚。如果您有任何疑问,请随时问我。

关于java - 如何排除某些节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com