Graph Searching Algorithms

Click here to load reader

download Graph Searching Algorithms

of 75

  • date post

    22-Feb-2016
  • Category

    Documents

  • view

    39
  • download

    2

Embed Size (px)

description

Graph Searching Algorithms. Tree. Breadth-First Search (BFS). Breadth-First Search (BFS). Not discovered. white. u. ∞. 0. x. Discovered, adjacent white nodes. gray. v. y. ∞. ∞. Discovered, no adjacent white nodes. black. w. ∞. ∞. z. Breadth-First Search (BFS). u. ∞. 0. - PowerPoint PPT Presentation

Transcript of Graph Searching Algorithms

PowerPoint Presentation - Tree Searching Algorithms

Graph Searching AlgorithmsSTreeBreadth-First Search (BFS)Breadth-First Search (BFS)0uxvyzwwhitegrayblackNot discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodesBreadth-First Search (BFS)0uxvyzwBFS(G, u): 1. Initialize the graph color[u] gray [u] Nil d[u] 0 for each other vertex color[u] whiteBreadth-First Search (BFS)0uxvyzwQuBFS(G, u): 2. Initialize the queue Q Enqueue(Q, u)Breadth-First Search (BFS)QBFS(G, u): 3. While Q 1) t Dequeue(Q)0uxvyzwt = uBreadth-First Search (BFS)0u1x1vyzwQvBFS(G, u): 3. While Q 2) for each r adj to t if color[r] = white color[r] gray [r] t d[r] d[t] + 1 Enqueue(Q, r)t = ur = x, vxBreadth-First Search (BFS)0u1x1vyzwQvBFS(G, u): 3. While Q 3) color[t] blackt = ur = x, vxBreadth-First Search (BFS)0u1x1vyzwQxBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = vBreadth-First Search (BFS)0u1x1v2yzwQxBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = vr = yyBreadth-First Search (BFS)0u1x1v2yzwQxBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = vr = yyBreadth-First Search (BFS)0u1x1v2yzwQyBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = xr = Breadth-First Search (BFS)0u1x1v2yz3wQwBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = yr = wBreadth-First Search (BFS)0u1x1v2y4z3wQzBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = wr = zBreadth-First Search (BFS)0u1x1v2y4z3wQBFS(G, u): 3. While Q 1) t Dequeue(Q) 2) for each r adj to t 3) color[t] blackt = zr = Breadth-First Search (BFS)0u1x1v2y4z3wBFS(G, u): - the shortest-path distance from uBreadth-First Search (BFS)0u1x1v2y4z3wBFS(G, u): - the shortest-path distance from u - construct a treeBreadth-First Search (BFS)0u1x1v2y4z3wBFS(G, u): - Initialization: |V| - Enqueuing/dequeuing: |V| - Scanning adj vertices: |E|Breadth-First Search (BFS)0u1x1v2y4z3wBFS(G, u): - Initialization: O(|V|) - Enqueuing/dequeuing: O(|V|) - Scanning adjacent vertices: O(|E|) => total running time: O(|V| + |E|)Depth-First Search (DFS)Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished vwDepth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: td[u] = tvwDepth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: t+1d[u] = tvwd[v] = t+1Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: t+2d[u] = tvwd[v] = t+1f[v] = t+2Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: t+3d[u] = tvwd[v] = t+1f[v] = t+2d[w] = t+3Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: t+4d[u] = tvwd[v] = t+1f[v] = t+2d[w] = t+3f[v] = t+4Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished timestamp: t+5d[u] = tf[u] = t+5vwd[v] = t+1f[v] = t+2d[w] = t+3f[w] = t+4Depth-First Search (DFS)ud[u]: when u is discoveredf[u]: when searching adj of u is finished d[u] = tf[u] = t+5vwd[v] = t+1f[v] = t+2d[w] = t+3f[w] = t+4d[u] < f[u][ d[u], f[u] ] entirely contains [ d[v], f[v] ][ d[v], f[v] ] and [ d[w], f[w] ] are entirely disjointDepth-First Search (DFS)uxvyzwwhitegrayblackNot discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodesDepth-First Search (DFS)uxvyzwd /d / fNot discoveredDiscovered,adjacent white nodesDiscovered,no adjacent white nodesDepth-First Search (DFS)uxvyzwDFS(G): 1. Initialization for each u V[G], color[u] white [u] Nil time 0Depth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting color[u] gray d[u] time time + 11/uxvyzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white [v] u DFS-Visit[v]1/ux2/vyzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white [v] u DFS-Visit[v]1/ux2/v3/yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. for each adj v of white [v] u DFS-Visit[v]1/u4/x2/v3/yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/u4/5x2/v3/yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/u4/5x2/v3/6yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/u4/5x2/7v3/6yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/8u4/5x2/7v3/6yzwDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/8u4/5x2/7v3/6yz9/wDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/8u4/5x2/7v3/6y10/z9/wDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/8u4/5x2/7v3/6y10/11z9/wDepth-First Search (DFS)DFS(G): 1. Initialization 2. For each u V[G] if color[u] = white DFS-Visit(u)DFS-Visit(u): 1. Initial Setting 2. Handling adj vertices 3. color[u] black f[u] time time + 11/8u4/5x2/7v3/6y10/11z9/12wDepth-First Search (DFS)DFS(G): - construct a forest1/8u4/5x2/7v3/6y10/11z9/12wDepth-First Search (DFS)1/8u4/5x2/7v3/6y10/11z9/12wDFS(G): - Initialization: O(|V|) - Traversing vertices: O(|V|) - Scanning adjacent vertices: O(|E|) => total running time: O(|V| + |E|)Topological SortingmnoqtursmsquontrTopological SortingBrute-Force way 1. Find a vertex without edges. 2. Put it onto the front of the list, and remove it from G. 3. Remove all edges to the removed edge. 4. Repeat 1~3.noqtursmO(|V|2 + |V||E|)OrO(|V|2)Topological SortingUsing DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.noqtursmO(|V| + |E|)Topological SortingUsing DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.uv1) v is white: d[u] < d[v] < f[u]2) v is black: f[v] < d[u]3) v is gray: d[v] < d[u] < f[v]At d[u]:v enters the list before u?Topological SortingUsing DFS 1. Call DFS(G) 2. When a vertex is finished, put it onto the front of the list.uv1) v is white: d[u] < d[v] At d[u]:v enters the list before u?tt is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]Topological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor, d[v] < d[u] < f[u] < f[v] Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] < d[v] < f[v]uvwDepth-First Search (DFS)uvwd[u] < f[u][ d[u], f[u] ] entirely contains [ d[v], f[v] ][ d[v], f[v] ] and [ d[w], f[w] ] are entirely disjointIn a depth-first forest, v is a descendant of u if and only if d[u] < d[v] < f[v] < f[u]Topological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor, d[v] < d[u] < f[u] < f[v] Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] < d[v] < f[v]uvAt d[u]:1) v is white: d[u] < d[v] t is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]tContradiction:G is not acyclic.Topological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor, d[v] < d[u] < f[u] < f[v] Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] < d[v] < f[v]uvAt d[u]:1) v is white: d[u] < d[v] t is gray: d[t] < d[u] < f[t]t is black: f[t] < d[u]tContradiction:v should be black.Topological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor, d[v] < d[u] < f[u] < f[v] Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] < d[v] < f[v]uvAt d[u]:1) v is white: d[u] < d[v] t is white.tTopological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor, d[v] < d[u] < f[u] < f[v] Otherwise, d[v] < f[v] < d[u] < f[u] or d[u] < f[u] < d[v] < f[v]uvAt d[u]:1) v is white: d[u] < d[v] < f[u]t is white.tTopological SortingIf v is a descendant, d[u] < d[v] < f[v] < f[u]If v is an anscestor,