Could you please tell me when to use BFS or DFS if the problem can be solved both by BFS and DFS ?
Cycle Detection Using DFS
If problem can be solved by both BFS and DFS you can use any one of them as, both have same time complexity so, no difference.
In general
BFS we use when we have to reach nodes in increasing order of distance from the source node.
DFS we use when you don’t use BFS! ( when it’s not needed to move in bfs criteria)
Hit like if you get it!
1 Like
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.