I solved this by building an induced subgraph based on all the traversal endpoints.
Building a subgraph from the set of last nodes and edges of each traversal does not work, because edges that are not part of the shortest paths will not be included.
The code snippet is as follows:
Set<Node> nodes = new HashSet<Node>();
Set<Relationship> edges = new HashSet<Relationship>();
for (Node n : traverser.nodes())
{
nodes.add(n);
}
for (Node node : nodes)
{
for (Relationship rel : node.getRelationships())
{
if (nodes.contains(rel.getOtherNode(node)))
edges.add(rel);
}
}
Each edge is added twice. Once for an outgoing node and once for an incoming node. Using Set, I can provide it in the collection only once.
It is only possible to iterate over incoming / outgoing edges, but it is unclear how the loops are processed (edge from node to itself). What category do they belong to? This snippet does not have this problem.