80 likes | 243 Views
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
E N D
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
2 4 1 6 1 3 5 Depth-First Search
2 4 1 6 1 3 5 Depth-First Search 5 2 4 List = {} List = {1} List = {1} List = {1,2,5,6} List = {1,2} List = {1,2} List = {1,2} 6 3 List = {1,2,5} List = {1,2,5} List = {1,2,4} List = {1,2,4} List = {1,2,4,3}
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.
2 2 4 1 6 1 4 3 5 6 A Depth-First Search Tree 5 3 Order visited = 1 2 5 6 4 3
Not A Depth-First Search Tree 4 2 2 4 1 6 1 6 5 3 3 5 Order visited = 1 2 3 5 4 6