Dontugiveup's blog

By Dontugiveup, history, 3 years ago, In English

I kind of feel that it does, but I've not been able to convince myself that it does. How can I make myself sure about dfs traversing every node?

help me having the view to look at it.

Surely could be a naive question, but any help is appreciated :)

  • Vote: I like it
  • +11
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

As soon as dfs is called from a source vertex, the source gets visited in the first step. In standard dfs algorithm, we iterate through each node and check if its visited or not. If visited we call dfs function and this ensures that the node is visited after it. If it's already visited then you move ahead without doing anything. This ensures that each node is visited for sure.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You visit every index only once in the visited array.

»
3 years ago, # |
  Vote: I like it +28 Vote: I do not like it

it doesn't, it traverses every reachable node from the source node

for example:

Spoiler

if we start dfs from 1 it will not visit 3

»
3 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

Suppose that it is not the case that all vertices reachable from the source of the dfs are visited by the dfs. Then there is at least one vertex such that it is reachable from the source $$$s$$$ of the dfs, but it is not visited by the DFS. Let $$$v$$$ be such a vertex with the minimum distance to the source.

Clearly, $$$v$$$ is not $$$s$$$, since when you call dfs on $$$s$$$, the first thing you do is to mark it as visited. So the distance from $$$s$$$ to $$$v$$$ is $$$\ge 1$$$.

Since $$$v$$$ is reachable from $$$s$$$ and the distance from $$$s$$$ to $$$v$$$ must be $$$\ge 1$$$, there must be a vertex before $$$v$$$ in the path from $$$s$$$ to $$$v$$$, say $$$u$$$. By definition, $$$u$$$ is reachable from $$$s$$$, and since $$$\text{dist}(s, u) = \text{dist}(s, v) - 1 < \text{dist}(s, v)$$$, $$$u$$$ must be visited by the dfs at some point (otherwise it would contradict the choice of $$$v$$$).

Since $$$u$$$ is visited, there must have been a call to dfs with argument as $$$u$$$. There are two cases when $$$v$$$ is encountered in the adjacency list of $$$u$$$ in such a call:

  • $$$v$$$ is marked as visited: this implies that $$$v$$$ has been visited by the dfs at this point, which is a contradiction to the choice of $$$v$$$.
  • $$$v$$$ is not marked as visited: this implies that dfs will be called with argument $$$v$$$, so $$$v$$$ will be marked as visited immediately, which is a contradiction to the choice of $$$v$$$.

Hence the existence of such a $$$v$$$ leads to a contradiction, so dfs traverses all vertices that are reachable from the source.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Really quite well explained...just before looking for answers now...i thought the same way...like since the graph is connected...there has got to be a way from s to v. Now if we start from s...then each and every child of it is going to be visited, it may be through the parent (s) itself or could be through another vertex...so it goes closer and closer and will mark the concerned vertex as visited.

    Glad to see something in the same direction in the comments. Really helped :) feels nice.