1
votes

I have a graphstream graph, and each node and edge has some attributes. I then use the following code to get the shortest path between two nodes:

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

How can I then remove from the graph all nodes that are not on the path - or somehow otherwise turn the Path into a Graph?

I tried

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

but obviously that doesn't work because you can't add a Node object, only create a new Node with a given ID. Do I actually have to recreate the whole path as nodes and edges, with all the attributes etc. re-created again; or work through all the nodes in the graph and remove those whose IDs don't match those in the path? Surely there's a more efficient way to get a Graph from a Path?

1

1 Answers

1
votes
List<org.graphstream.graph.Node> nodes = graph.nodes().collect(Collectors.toList());
nodes.removeAll(p.getNodeSet());
nodes.forEach
(
    node -> graph.removeNode(node)
);