Depth First Search (DFS) is a bold search. We go as deep as we can to look for a value, and when there is nothing new to discover, we retrace our steps to find something new.
By the way, DFS always comes with Backtracing and Divide and Conquer.
Backtracing: backtracking is the concept of retracing and DFS is the algorithm that implements it.
Divide and Conquer: splitting into subproblems of the same type (search in left and right children) until they are simple enough to be solved directly (null nodes or found target) and combine the results from these subproblems (return non-null node).
classNode: def__init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right
# usually using DFS to structure a tree defbuild_tree(nodes): val = next(nodes) ifnot val or val == 'x': return cur = Node(val) cur.left = build_tree(nodes) cur.right = build_tree(nodes) return cur
# yield makes print_tree() as a generator object which is an iterater defprint_tree(root): ifnot root: yield"x" return yieldstr(root.val) yieldfrom print_tree(root.left) yieldfrom print_tree(root.right)
if __name__ == '__main__': root = build_tree(iter(input().split())) print(' '.join(print_tree(new_root)))
DFS in Graph
Traversal
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defdfs(myGraph, start): visited = set() defDFSUtil(start, visited): # Mark the current node as visited # and print it visited.add(start) print(start, end=' ')
# Recur for all the vertices # adjacent to this vertex for neighbour in myGraph.graph[start]: if neighbour notin visited: DFSUtil(neighbour, visited) DFSUtil(start, visited)
from collections import defaultdict # This class represents a directed graph using # adjacency list representation classGraph: # Constructor def__init__(self): # Default dictionary to store graph self.graph = defaultdict(list) # Function to add an edge to graph defaddEdge(self, u, v): self.graph[u].append(v)
if __name__ == "__main__": g = Graph() g.addEdge(0, 1) g.addEdge(0, 2) g.addEdge(1, 2) g.addEdge(2, 0) g.addEdge(2, 3) g.addEdge(3, 3)