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 :)
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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 :)
Name |
---|
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.
You visit every index only once in the visited array.
it doesn't, it traverses every reachable node from the source node
for example:
if we start dfs from 1 it will not visit 3
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:
Hence the existence of such a $$$v$$$ leads to a contradiction, so dfs traverses all vertices that are reachable from the source.
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.