Download presentation
Presentation is loading. Please wait.
Published byGervais Dennis Modified over 8 years ago
1
Search Related Algorithms
2
Graph Code Adjacency List Representation:
3
Searches BFS & DFS produce trees – Spanning trees if graph is connected from starting vertex
4
Path Reconstruction Search tree can be stored as parent of each vertex
5
Path Reconstruction Reconstruct path to 6: Got to 6 from 4
6
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0
7
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0 Got to 0 from 1
8
Path Reconstruction Reconstruct path to 6: Got to 6 from 4 Got to 4 from 0 Got to 0 from 1 1 was root
9
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
10
Search Tree Requirements Storage = O(V) Time to build: O(V + E) Time to reconstruct path: O(path length)
11
SearchTree Code Info to reconstruct search tree
12
BFS Search Process Maintain: – Queue of vertices to explore – List of discovered vertices
13
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
14
BFS
16
DFS Search Process Maintain: – Stack of vertices to explore – List of visited vertices
17
DFS Search Process Same vertex may end up on stack multiple times: 1 ParentVisited 0 1 2 3 4 5 6 7
18
DFS Search Process Same vertex may end up on stack multiple times: 6320 ParentVisited 01 1Y 21 31 4 5 61 7
19
DFS Search Process Same vertex may end up on stack multiple times: 63242 ParentVisited 01Y 1Y 20 31 40 5 61 7
20
DFS Search Process Same vertex may end up on stack multiple times: 6324 ParentVisited 01Y 1Y 20Y 31 40 5 61 7
21
DFS Search Process Same vertex may end up on stack multiple times: 63244 ParentVisited 01Y 1Y 20Y 31 42 5 61 7
22
DFS Search Process Same vertex may end up on stack multiple times: 6324 ParentVisited 01Y 1Y 20Y 31 42Y 5 61 7
23
DFS Search Process Same vertex may end up on stack multiple times: 632 ParentVisited 01Y 1Y 20Y 31 42Y 5 61 7
24
DFS Search Process Same vertex may end up on stack multiple times: 63 ParentVisited 01Y 1Y 20Y 31 42Y 5 61 7
25
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 5 61 7
26
DFS Search Process Same vertex may end up on stack multiple times: 67 ParentVisited 01Y 1Y 20Y 31Y 42Y 5 61 73
27
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 5 61 73Y
28
DFS Search Process Same vertex may end up on stack multiple times: 65 ParentVisited 01Y 1Y 20Y 31Y 42Y 57 61 73Y
29
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 61 73Y
30
DFS Search Process Same vertex may end up on stack multiple times: 66 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65 73Y
31
DFS Search Process Same vertex may end up on stack multiple times: 6 ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65Y 73Y
32
DFS Search Process Same vertex may end up on stack multiple times: ParentVisited 01Y 1Y 20Y 31Y 42Y 57Y 65Y 73Y
33
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
34
DFS
36
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.