views:

502

answers:

3
+1  Q: 

DFS Tree question

In an undirected graph, can two nodes at an identical distance n from the root of a DFS tree be neighbors in the original graph? I'm thinking no, but I'm not sure (because of back edges)

+1  A: 

I think you are correct:

All neighbors of a node will be of rank one less (the node we got here from) or one more (or two more, etc) because we will rank them before we back out of the node.

BCS
A: 

Your question does not stipulate how the DFS tree is generated. Naive implementations simply list nodes in the order they are visited, but most practical implementations rebuild the output tree as the search progresses and the ranks of the nodes are adjusted.

Consider three nodes, A B C, connected to each other. A naive DFS will visit A B C, with ranks of 0 1 2, and the tree will have a back edge from C to A. A more useful implementation will backtrack from C to B to A, and then from A will travel to C again and assign it a rank of 1, then decline to travel to B at rank 2 because B is already rank 1, this tree will have both B and C at rank 1 below A, with a cross edge between B and C.

Sparr
+2  A: 

The wikipedia article states that "it can be shown that if the graph is undirected then all of its edges are tree edges or back edges."

If you find out how this can be shown, you should be quite close to your solution.

Svante
If the edge is not a back edge (an edge you walk back in on) than it will be a tree edge b/c you will walk out it.
BCS