3

I am curious of knowing of an efficient algorithm for the problem:

Given two vertices s,t in a directed graph, is there a vertex x such that xs and xtare paths?

I can of course do a BFS from each node of the graph resulting in a quadratic time algorithm.

Or I can reverse all the edges of the graph, do a BFS from s store all reachable vertices in a hashtable, do a BFS from t and check for each reachable vertex if it is in the table, ifso, I found x. While the running time is linear, the extra space requirement is ugly.

Is there any elegant O(E + V) time algorithm for this problem?

user695652
  • 783
  • 1
  • 5
  • 14
  • 1
    You don't need a hash table, a simple binary array will do. Your extra space is just |V| bits. Do you really want to go lower than that? :) I'm personally skeptical it's possible. – Mihai Mar 16 '16 at 21:03
  • @Mihai Calancea I think it is possible but I have not a clear idea: You alter BFS so that in each path you initially are allowed to follow forward arcs only and then backward arcs only. – user695652 Mar 16 '16 at 21:36
  • Can you even do a BFS without using extra memory to remember which nodes you've been to (which would amount to those |V| bits?). Also, I was actually wrong, you also need O(|V|) extra integers to store the BFS queue. – Mihai Mar 16 '16 at 22:30
  • @Mihai Calancea I totally get your point, I was just hoping for a one-pass algorithm or just to hear some other approach... – user695652 Mar 17 '16 at 00:47
  • What about this: reverse the edges, construct a disjoint-set data structure for all the vertices, run a BFS from $s$ and call $Union(s, v)$ on all reachable vertices $v$, do the same with $t$, then call $Find(s, t)$ to get your answer? – DylanSp Mar 17 '16 at 14:38
  • What do you mean by "xs is a path"? Do you mean that there is an edge $x \to s$? Do you mean that there exists a path that starts at $x$ and ends at $s$? 2. Your final question mentions only running time. Do you also have a space complexity requirement? If so, what is it exactly? Can you articulate your final question more precisely? Are you asking whether there is any algorithm a $O(V+E)$ running time and $o(V)$ space? Or what?
  • – D.W. Mar 17 '16 at 18:04
  • I mean that that there exists a path that starts at x and ends at s. I meant this as an open question, I am just curious if there is an elegant one pass algorithm which works with minimal book keeping. As @Mihai Calancea pointed out o(V) space is highly unlikely since thats already the space needed by BFS – user695652 Mar 19 '16 at 04:01