1

我有一个图流图,每个节点和边都有一些属性。然后我使用以下代码来获取两个节点之间的最短路径:

AStar astar = new AStar(graph);
astar.setCosts(new DistanceCosts());
astar.setSource(fromNodeIdentifierString);
astar.setTarget(toNodeIdentifierString);
astar.compute();
org.graphstream.graph.Path p = astar.getShortestPath();

然后如何从图中删除不在路径上的所有节点 - 或者以其他方式将路径变成图?

我试过了

graph.clear()
for (Node n : p.getNodeSet())
{
    graph.addNode(n);
}

但显然这不起作用,因为您无法添加 Node 对象,只能创建一个具有给定 ID 的新 Node。我真的必须将整个路径重新创建为节点和边,并重新创建所有属性等吗?还是遍历图中的所有节点并删除那些 ID 与路径中的节点不匹配的节点?肯定有一种更有效的方法可以从路径中获取图形吗?

4

1 回答 1

1
List<org.graphstream.graph.Node> nodes = graph.nodes().collect(Collectors.toList());
nodes.removeAll(p.getNodeSet());
nodes.forEach
(
    node -> graph.removeNode(node)
);
于 2021-03-08T20:27:09.093 回答