To get an overall result I'll report what I did.
I implemented the in the question mentioned algorithm. (Lets call it thigg's algorithm, because noone mentioned a name)
Thigg's algorithm:
for each node in the graph:
if there is a path from src to this node
and a path from this node to dest,
push node to the result list.
As mentioned by Edward Doolittle in his answer the graph can be optimized before by identifying Strongly-Connected-Components and reducing them to one node. This would reduce the effort of the algorithm on each run.
I implemented thiggs algorithm with the Boost-Graph-Library (without SCC optimization) where a
is the source and b
the destination vertex:
I used a breadth first search to get a list of all vertexes which are reachable by a
:
boost::breadth_first_search(graph, a, visitor(vis));
Where vis is a custom visitor which puts all the visited vertexes to a list.
Then it reverts the graph to get all vertexes which can reach b
boost::breadth_first_search(boost::make_reverse_graph(graph), b, visitor(vis));
And finally computes the intersection:
std::set_intersection(froma.begin(),froma.end(),fromb.begin(),fromb.end(),back_inserter(inters));
Note that your graph has to be bidirectional in order to use make_reverse_graph
.
The algorithm also works for edges as mentioned in the question.