I'm currently trying to figure out an efficient way to find all nodes on some path between two nodes (say, X
and Y
) in a directed graph.
My first thought was to run a BFS from X, run a BFS from Y, and take the intersection of visited nodes. Note that I don't need to enumerate all paths between X and Y, just find all nodes that lie on a path from X to Y.
My question is, is there a more optimized way to do this?