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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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.