I’m working on better understanding the application of a depth-first search algorithm. I understand how to use it to traverse a binary search tree to produce a sorted list. My Python implementation looks like this:
class bst_node:
def __init__(self, x):
self.x = x
self.left = None
self.right = None
def get_dfs_path(bst_node):
""" Returns a depth-first search path for a BST """
left = [] if bst_node.left == None else get_dfs_path(bst_node.left)
right = [] if bst_node.right == None else get_dfs_path(bst_node.right)
return left + [bst_node] + right
Which works quite nicely. I’m struggling to understand, however, whether this algorithm can be meaningfully applied to a digraph in general, rather than the more strict BST. Consider the following digraph node implementation:
class di_node:
def __init__(self, x):
self.x = x
self.visited = False
self.children = []
Since a node in a digraph can have an arbitrary number of children, the dfs logic can’t simply construct the path as dfs_path(left) + parent_node + dfs_path(right). Can someone help me understand if/how dfs applies to a digraph?
EDIT
Ok, based on the responses let me attempt a dfs traversal for a di_node. Please let me know if I’m anywhere close to the mark:
def get_dfs_path(di_node):
""" Returns a depth-first search path for a digraph """
if di_node.visited:
return []
else:
di_node.visited = True
return [di_node] + [ get_dfs_path(child) for child in di_node.children) ]
As you noticed in-order traversal (left-subtree, current node, right subtree) doesn’t make much sense for general graphs since a node can have more than two subtrees. However a depth first search can also use pre-order (process the current node first, then the subtrees) or post-order (first process the subtrees, then the current node) traversal. Those two work just fine with graphs.
One thing you have to keep track of when performing DFS on graphs is which nodes you already visited. Otherwise you’d get infinite loops when traversing cyclic graphs.