Search Related Algorithms
Graph Code Adjacency List Representation:
Searches BFS & DFS produce trees – Spanning trees if graph is connected from starting vertex
Path Reconstruction Search tree can be stored as parent of each vertex
Path Reconstruction Reconstruct path to 6: Got to 6 from 4
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0 Got to 0 from 1
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0 Got to 0 from 1 1 was root
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0 Got to 0 from 1 1 was root 1 0 4 6
Search Tree Requirements Storage = O(V) Time to build: O(V + E) Time to reconstruct path: O(path length)
SearchTree Code Info to reconstruct search tree
BFS Search Process Maintain: – Queue of vertices to explore – List of discovered vertices
BFS BFS (startVertex) Make queue of vertices Put starting vertex on queue Build discovered array – mark start discovered Make parentList While queue is not empty current = queue.dequeue() For each neighbor index from current If( !discovered[neighbor] ) discovered[neighbor] = true parentList[neighbor] = current enqueue neighbor bool array 1 = visited, 0 = not visited List of ints vertices to visit int array
BFS
DFS Search Process Maintain: – Stack of vertices to explore – List of visited vertices
DFS Search Process Same vertex may end up on stack multiple times: 1 ParentVisited
DFS Search Process Same vertex may end up on stack multiple times: 6320 ParentVisited 01 1Y
DFS Search Process Same vertex may end up on stack multiple times: ParentVisited 01Y 1Y
DFS Search Process Same vertex may end up on stack multiple times: 6324 ParentVisited 01Y 1Y 20Y
DFS Search Process Same vertex may end up on stack multiple times: ParentVisited 01Y 1Y 20Y
DFS Search Process Same vertex may end up on stack multiple times: 6324 ParentVisited 01Y 1Y 20Y 31 42Y
DFS Search Process Same vertex may end up on stack multiple times: 632 ParentVisited 01Y 1Y 20Y 31 42Y
DFS Search Process Same vertex may end up on stack multiple times: 63 ParentVisited 01Y 1Y 20Y 31 42Y
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y
DFS Search Process Same vertex may end up on stack multiple times: 67 ParentVisited 01Y 1Y 20Y 31Y 42Y
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y Y
DFS Search Process Same vertex may end up on stack multiple times: 65 ParentVisited 01Y 1Y 20Y 31Y 42Y Y
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 61 73Y
DFS Search Process Same vertex may end up on stack multiple times: 66 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65 73Y
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65Y 73Y
DFS Search Process Same vertex may end up on stack multiple times: ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65Y 73Y
DFS DFS (startVertex) Make stack of vertices Put starting vertex on stack Build visited array Make parentList While stack is not empty current = stack.pop() if( !visited[current] ) visited[current] = true For each neighbor index from current if( !visited[neighbor] ) parent[neighbor] = current push neighbor onto stack bool array 1 = visited, 0 = not visited List of ints vertices to visit int array
DFS
DFS Recursive DFSRecursive (startVertex) Build visited array Make parentList BFSHelper(startVertex, parentList, visited) DFSHelper (currentVertex, &parentList, &visited) visited[currentVertex] = true For each edge of currentVertex newVertex = edge destination if( !isVisited[newVertex] parentList[newVertex] = currentVertex DFSHelper(newVertex, parentList, visited)