Skip to content
Advertisement

How I can stop depth first search at specific node

Here Is my python code:

JavaScript

its about depth first search algorithm and when I compile it without break statement the result would be: A B E F C G K H D

and when i put break statement the result would be: A B E and my question is how i can here stop this algorithm at specific node like F so the result would be like

A B E F

I have tried to put break after line 23 but it just give me this result A B E but I want F to be included in it

Advertisement

Answer

In order to stop the loop, we would want to return something if the node, for which you are searching for, is found:

JavaScript

In case of dfs(visited, graph, 'A', 'F'), it should print:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement