DFS reference doubt

why its imp to pass the visited map as reference ?
i tried using without reference and didnt get the ans…please help me to clarify this part.

@sharmalakshyarocks The visited map is the most essential thing while running your dfs and keeps the runtime of algorithm in check, otherwise it would keep looping.
Lets say you mark node 1 as visited, now you run dfs for nodes which are neighbors for 1 and before running dfs for them you mark them as visited, this is done so as to avoid exploring them again via some other path.
For example if you are inserting bi-directional edges, you have to make sure that 1 is marked as visited, otherwise its neighbors will once again call 1 and you repeat the same algorithm again and again.
So marking them as visited just confirms that you have run dfs for these elements at least once.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.