Graph Traversal
Total Page:16
File Type:pdf, Size:1020Kb
GRAPH TRAVERSAL Lecture 18 CS 2110 — Spring 2019 JavaHyperText Topics 2 “Graphs”, topic 8: DFS, BFS Graph Representations 3 1 2 Adjacency list 3 4 Adjacency matrix 1 2 3 0 1 2 3 4 0 F F F F F 2 4 1 F F T T F 2 F F F F T 3 2 4 3 F F T F T 4 4 F F F F F 4 Review: Sparse vs. Dense (improved definitions in Lec 17) Graph Search 5 Graph Traversal 6 Goal: visit each vertex 2 3 that is reachable from 1 4 some starting vertex 7 5 6 8 And: even if there are 2 3 many paths to a node, 1 visit only once 4 7 5 6 Two algorithms: DFS, BFS 8 7 Depth-First Search (DFS) Intuition: one person exploring a maze Depth-First Search 8 Idea: Recursively visit each unvisited neighbor /** Visit every node reachable along a path of unvisited nodes from node v. Precondition: v is unvisited. */ void dfs(Vertex v) { 3 1 2 mark v visited; for all edges (v,u): 4 if u is unmarked: 7 5 dfs(u); 6 8 } 8 dfs(1) visits the nodes in this order: 1, 2, 3, 5, 7, 8 9 Poll #1 Depth-First Search 10 /** Visit every node reachable along a path of unvisited nodes from node v. Precondition: v is unvisited. */ void dfs(Vertex v) { mark v visited; for all edges (v,u): if u is unmarked: dfs(u); } Demo DFS Space Efficiency 11 void dfs(Vertex v) { mark v visited; Suppose graph has for all edges (v,u): V vertices and E edges if u is unmarked: dfs(u); } 1 2 3 4 Space required? • Mark for each vertex: O(V) • Frame for each recursive call: O(V) Worst case: O(V) 8 7 6 5 DFS Time Efficiency 12 void dfs(Vertex v) { mark v visited; Suppose graph has for all edges (v,u): V vertices and E edges if u is unmarked: dfs(u); } Time required? 1 2 • Mark each vertex: O(V) • Recursive call for on each unvisited vertex: O(V) • Find each edge 4 3 • in adj list: O(E): Worst case: O(V+E) • In adj matrix: O(V2): Worst case: O(V2) Variant: Iterative DFS 13 Same algorithm; non-recursive implementation void dfs(Vertex u) { Stack s= new Stack(); 2 3 s.push(u); 1 while (s is not empty) { 4 u= s.pop(); if (u not visited) { 77 5 visit u; 6 for each edge (u, v): 88 s.push(v); } 8 } 57 } 5 u: 352178 Stack: 5312 Demo Visit order was 1, 7, 8, 5, 2, 3: differs from before because of order edges processed 14 Breadth-First Search (BFS) Intuition: Search party fanning out in all directions Breadth-First Search 15 Idea: Iteratively process the graph in "layers" moving further away from the source vertex. 3 1 2 4 77 55 6 88 Breadth-First Search 16 Idea: Iteratively process the graph in "layers" moving further away from the source vertex. /** Visit all vertices reachable on unvisited paths from u. */ 3 void dfsbfs(int u) { 1 2 StackQueue s=q= new Stack()Queue() 4 s.pushq.add(u);(u); while ( q is not empty ) {) { 77 55 u= s.popq.remove(); (); if (u not visited) { 6 visit u; 8 for each edge(u, v):(u, v): 8 s.pushq.add(v);(v); } } } } Queue: 21 5 7 3 5 8 5 } Visit order was 1, 2, 5, 7, 3, 8 Demo 17 Poll #2 Improved BFS 18 Idea: Don’t put vertex in queue if already encountered /** Visit all vertices reachable on unvisited paths from u. */ void bfs(int u) { Queue q= new Queue q.add(u); while ( q is not empty ) { u= q.remove(); if (u not visited) { visit u; for each (u, v): if (v not encountered) { mark v as encountered; q.add(v); } } } } Analyzing BFS 19 /** Visit all vertices reachable on unvisited paths from u. */ void bfs(int u) { Same as DFS. Queue q= new Queue Space: O(V) q.add(u); Time: while ( q is not empty ) { • Adj list: O(V+E) u= q.remove(); • Adj matrix: O(V2) if (u not visited) { visit u; for each (u, v): if (v not encountered) { mark v as encountered; q.add(v); } } } } Comparing Traversal Algorithms 20 DFS (recursive) DFS & BFS (iterative, improved*) 2 2 ¨ Time: O(V+E) or O(V ) ¨ Time: O(V+E) or O(V ) ¨ Space: O(V) ¨ Space: O(V) *Without improvement, space becomes O(E).