4

To my knowledge, it is possible that if a path from vertex "a" to vertex "b" exists on some arbitrary directed graph, that there can be some circumstance where, using a depth first search on the graph, that vertex "b" can be discovered in the search after vertex "a" is done being processed. However, this doesn't seem possible to me (after drawing out many graphs). Any ideas?

user2789945
  • 527
  • 2
  • 6
  • 23

2 Answers2

4

No, your assumption is wrong. It is impossible.

It is fairly easy to prove using induction that when processing of vertex "a" is done, all vertices reachable from "a" (e.g. "b") has already been discovered.

shx2
  • 61,779
  • 13
  • 130
  • 153
1
 (a) ---> (a1) ---->(b)
 |                   >
 |                   | 
 >                   |  
(a2)--------------->(a3)   

Consider this graph, vertex (a) has path to vertex(b).

When we run dfs on starting from vertex (a), the output is (a),(a1),(b),(a2), (a3)

Vertex (b) is visited after (a) is visited.

Vikas
  • 4,263
  • 1
  • 34
  • 39
  • Yes, in this case, vertex (b) is indeed visited after (a), but (b) will still be discovered before (a) is done being processed in the dfs. – user2789945 Apr 28 '15 at 06:15
  • DFS is about discovering/visiting/searching vertexes of a graph. Here vertex (a) is discovered before (b). About (a) being completely processed before (b) gets discovered is a different thing. – Vikas Apr 28 '15 at 06:25