4

Is it possible in an undirected graph for an edge that is leasing to an already visited node to lead towards a vertice that is not an ascendant of the current node?

To be more clear, I want to implement a depth-first search on an undirected graph. If I come across an edge that connects my current vertice with an already-visited one, am I guaranteed to have a path from one to another by iterating through the parent array?

The most natural answer seems to be affirmative. I have yet to find a counter-example. What do you think?

In DFS terminology:
Can an edge be a cross-edge in DFS - an edge that leads to an already discovered node, which is not an ancestor of the origin, in an undirected graph?

amit
  • 175,853
  • 27
  • 231
  • 333
lbicsi
  • 63
  • 1
  • 9

1 Answers1

6

An edge discovered by DFS cannot be a cross edge, if its destination is an already discovered node, it must be a back-edge - so it is leading to an ancestor (in the DFS tree) of the source node.

Proof:

Assume it was not the case, and while in some node vyou encounter an already discovered node (u) that is not one of your 'parents' (in the DFS tree).
Since you discovered the node, there is an edge (v,u).
But the graph is undirected, so the edge is symetrical.

Since u was already discovered, you have these options:

  1. u was discovered and not "closed" yet, in this case, you are basically traversing a subtree which has u as a root, and u is indeed a parent of v.
  2. u was discovered and closed already. But that's impossible considering you just discovered v, and there is an edge (u,v).

Thus an edge that leads to an already discovered edge in an undirected graph, must be a back edge, and cannot be a cross-edge.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Well, what about the case when v is not 'just' discovered? In other words, what if my search has finished finding certain sub-trees, and then I find an edge that links to a node that was not visited initially, but then gets visited in the mean time? Then, I suppose the neighbor should be part of the node's already-processed subtree, am I right? – lbicsi Mar 09 '15 at 12:58
  • @lbicsi I am not following your question, as I see it - doesn't it fit exactly case 2? – amit Mar 09 '15 at 13:04
  • So your answer to the original question "Can an edge be a cross-edge in DFS..." is "Yes"? – beaker Mar 09 '15 at 16:13
  • @beaker No, I edited the question to make it the other-way around without editing my question, let me fix it. Thank you for your comment. – amit Mar 09 '15 at 16:26
  • Excellent, that makes much more sense. :) – beaker Mar 09 '15 at 16:32