I wrote this DFS in Python and I was wondering if it correct. Somehow it seems too simple to me.
Each node is a class with attributes:
visited
: Booleansons
: list of nodes linked to the current node.
This code should work both for directed and undirected graphs.
def DFS(node):
node.visited = True
if node == None:
return
// perform some operation on the node
do_something(node)
to_visit = [son in node.sons if not son.visited]
for son in to_visit:
DFS(node)
[son in node.sons if !son.visited]
\$\endgroup\$