EMIS 8374 Search Algorithms: DFS Updated 12 February 2004
Depth-First Search Always pick the last (most recently added) node in the LIST Pick marked nodes in a last-in, first-out order (LIFO) Breadth-First Search uses a FIFO order Makes a “deep” probe, creating as long a path as possible Backs up when it can’t mark a new node
Depth-First Search 1 2 4 3 5 6
Depth-First Search 5 2 1 2 4 3 5 6 4 6 3 List = {} List = {1}
Depth-First Search Tree 5 2 2 4 1 6 1 4 3 5 6 3
Properties of a DFS Tree If node j is a descendant of node i, then order(j) > order(i) All descendants of any node are ordered consecutively in sequence. That is, suppose that order[i]= p and i has q descendants. Then, any node j such p+1 ≤ order[j] ≤ p+q is a descendant of node i.
A Depth-First Search Tree 5 1 2 4 3 5 6 3 Order visited = 1 2 5 6 4 3
Not A Depth-First Search Tree 4 2 2 4 1 6 1 6 3 5 3 5 Order visited = 1 2 3 5 4 6