Search Related Algorithms
BFS Finds shortest path to all connected vertices Space: Exponential in search depth Degree of 100 for each vertex First hop – 100 vertices on queue Second hop - ~10,000 vertices on queue Third hop - ~1,000,000 vertices to track But limited by number of vertices : O(V)
DFS Finds all connected vertices Space Proportional to length of current path Recursive version implicitly stores on call stack Iterative version explicitly stores on stack May have duplicates
DFS vs BFS Both worst case O(V) space Time : Pick based on job… BFS gets there sooner Time : Adjacency list O(V + E) Build visited list : V Check every edge : V + E Adjacency Matrix O(V2) For each vertex, check every other Pick based on job…
For Search Search Problems BFS : Large memory requirement DFS : Finds suboptimal path May miss answer
IDS Iterative Deepening Search DFS to limited depth Increase depth until solution found Memory bound by search depth Time similar to BFS
BFS Application Bipartite : can divide graph into two sets of vertices A, B; every edge connects an element of A to element of B
BFS Application Storage array for color Mark start vertex blue When visiting a node, color neighbors opposite color Conflict = not bipartite Vertex Color 1 2 3 4 5 6 7
BFS Application Storage array for color Mark start vertex blue When visiting a node, color neighbors opposite color Conflict = not bipartite Vertex Color 1 2 Blue 3 4 5 6 7
BFS Application Storage array for color Mark start vertex blue When visiting a node, color neighbors opposite color Conflict = not bipartite Vertex Color 1 Red 2 Blue 3 4 5 6 7
BFS Application Storage array for color Mark start vertex blue When visiting a node, color neighbors opposite color Conflict = not bipartite Vertex Color 1 Red 2 Blue 3 4 5 6 7
DAG DAGs : Directed Acyclical Graphs Common in scheduling type problems
Cycle Detection Detect Cycle: Do DFS from each node until all are visited If a neighbor to current is already on stack we have a cycle
Detect Cycles For each vertex: Vertex Visited In Stack F 1 2 3 4 5
Detect Cycles For each vertex: Check 0 Vertex Visited In Stack T 1 F 2 T 1 F 2 3 4 5
Detect Cycles For each vertex: Check 0 Check 1 Vertex Visited In Stack T 1 2 F 3 4 5
Detect Cycles For each vertex: Check 0 Check 1 Check 2 Vertex Visited In Stack T 1 2 3 F 4 5
Detect Cycles For each vertex: Check 0 Check 1 Check 2…done Vertex Visited In Stack T 1 2 F 3 4 5
Detect Cycles For each vertex: Check 0 Check 1…done Check 2…done Visited In Stack T 1 F 2 3 4 5
Detect Cycles For each vertex: Check 0 Check 1…done Check 2…done Check2… not needed Vertex Visited In Stack T 1 F 2 3 4 5
Detect Cycles For each vertex: Check 0…done Check 1…done Check 2…done Check2… not needed Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 1…not needed Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 2…not needed Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 3 Vertex Visited In Stack T F 1 2 T F 1 2 3 4 5
Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Check 5 Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Check 5 Check 3… already on stack! Vertex Visited In Stack T F 1 2 3 4 5
Detect Cycles bool checkCycle() Make bool isVisited[] – every vertex to false Make bool inStack[] – every vertex to false For each vertex If not already visited if cycleDFS(vertex, isVisited, inStack) return true return false bool cycleDFS(vertex, &isVisited, &inStack) mark vertex inStack mark vertex visited for each neighbor if in stack return true //cycle!! if not visited if cycleDFS(neighbor, isVisited, inStack) return true //found a cycle down stream mark vertex not inStack return false //guess we were OK along this path
Topological Sort Topological Sort : turn DAG into ordered list such that all edges point one way Maybe multiple orderings
Topological Sort Do DFS Add node to list as you LEAVE Make list ordering Make bool isVisited[] – every vertex to false For each vertex If not visited, do topoDFS(vertex, isVisited, ordering) topoDFS(vertex, isVisited, ordering) mark vertex visited for each neighbor if not visited topoDFS(neighbor, isVisited, ordering) add vertex to front of ordering
Connected Components Connected components : Break graph up intro strongly connected groups
Connected Components Connected components : Phase 1 : Identify chains / "roots" Do DFSs until each vertex visited Record time each vertex left the stack (last visited) Phase 2 : Find connections that lead to "roots" Sort list of vertexes based on last visit time (large to small) Reverse all the edges Do DFS from each node based on sorted list until every node has a group
Connected Components Connected components – part 1 Make int lastVisitTime[] int time = 0 Make bool isVisited[] – every vertex to false For each vertex If not visited, do connectDFS(vertex, isVisited, lastVisit, time) connectDFS(vertex, isVisited, lastVisit, &time) mark vertex visited time++ for each neighbor if not visited connectDFS(neighbor, isVisited, lastVisit, time) lastVisit[vertex] = ++time
Connected Components Connected components – part 2 Make masterlist of vertices – sorted based on lastVisitTime Reverse all edges Make bool isVisited[] – every vertex to false For each vertex in list If not visited, Make curConnected list do connect2DFS(vertex, isVisited, curConnected) print curConnected connect2DFS(vertex, isVisited, curConnected) mark vertex visited add to curConnected for each neighbor if not visited connect2DFS(neighbor, isVisited, curConnected)