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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
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.