Given a directed unweighted graph G, a set B of blue vertices in V and a yellow vertex y in (V-B), I need to find the shortest path from a given source s to all other vertices in the graph, without visiting the yellow vertex after visiting a blue vertex. In other words, it is not allowed to visit the yellow vertex if a blue vertex has already been visited (it is possible that some vertices will not be reachable).
Here are my thoughts:
Run BFS(s) a little differently - If we pass at a vertex b which is in B, don't continue the search on b's neighbors. When BFS finishes, if all vertices have been labeled, we are done. Otherwise:
Run BFS(b) on every b in B. For every path from b to y, delete the last edge in the path. For example, the BFS(b) found a path b,s,y then delete (s,y).
Run BFS(s).
Complexity would be O(|B||E|) because we would run BFS(b) |B| times.
I have a feeling this solution is not the best but this is what I came up with. Did I miss anything ?
sthat passes throughy, so this does not work correctly. Even if you removeyentirely, your algorithm will still fail for graphs like this. You need a completely different approach. - BlueRaja - Danny Pflughoeft