Graph -11: Check if Source to Destination Path exists in Directed Graph

preview_player
Показать описание
Solution:
- We'll achieve this via DFS approach.
- Take stack & boolean array
- Now when you start dfs from source index, it mark visited to all components to which it's connected to
- So via visted boolean array, we can identify if element is visited or not
- So at last return visited[destination_index]

Time Complexity: O(V + E)

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

I really admire this solution, i got confused while searching on net. Neat and concise

rahulsinghai
Автор

Hi , the code you mentioned for dfs using Stack is a bit wrong, though it will work.
You are marking each node as visited, when you are in the for loop itself, so, in this way all the neighbours get visited all together for a particular node, and this way it do not go to the depth first.
You must be marking them visited as true, when you pop them out of stack, in the while loop.
I have seen this issue in many of your programs.
Correct me if i am wrong, since I also need to get this cleared, if I am wrong
Thanks

akashgarg
Автор

Thank you sir. very much informative...clear explanation.

pinkyg
Автор

If we do dfs in directed to undirected graph it will cover each and every node
Then how, by traversing into the directed graph our one element is not visited

adarshgoyal