Search code examples
algorithmdata-structuresgraphpathpath-finding

is there a path from s-t that passes through special node?


I've seen some variations of this problem described og this website, but not this one exactly.

The problem

I have a unweighted graph G=(V,E), and preferably I need to make a solution that works for both directed and non-directed graphs.

A subset of V is W, and these are special nodes. I need to find out whether it is possible to find a path from a start node s to a end node t, that passes through one or more of the special nodes in W. This is a simple path where nodes are not repeated, and it has to run in polynomial time

So I simply need to output 'true' or 'false'.

My attempt so far

First, I thought that for every special node i W, I would run a bfs, that could find that node w, and then run a new bfs from node w to t. Roughly like this:

for w in W:
    firstpath = bfs from s to w
    secondPath = bfs from w to t (that does not touch nodes in firstpath)
    if firstpath + second == path from s to t:
         return True
    else:
        continue

But here, a problem could be that the "firstpath" could block a possible path from w to t.

Any ideas for polynomial algorithms for this problem that could guarantee no "node collisions"


Solution

  • For undirected graphs we can try all w in W, looking for two node-disjoint paths from w to s and t using max flow. I'm not sure that this generalizes to directed graphs, unfortunately.