/
Graph Traversal Lecture 18 Graph Traversal Lecture 18

Graph Traversal Lecture 18 - PowerPoint Presentation

lois-ondreau
lois-ondreau . @lois-ondreau
Follow
342 views
Uploaded On 2019-12-08

Graph Traversal Lecture 18 - PPT Presentation

Graph Traversal Lecture 18 CS 2110 Spring 2019 JavaHyperText Topics Graphs topic 8 DFS BFS 2 Graph Representations 3 0 1 2 3 4 0 F F F F F 1 F F T T F 2 F F F F T 3 F F T F T 4 F F F F F ID: 769627

visit dfs unvisited vertex dfs visit vertex unvisited visited void mark search bfs graph queue reachable vertices add paths

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Graph Traversal Lecture 18" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Graph Traversal Lecture 18 CS 2110 — Spring 2019

JavaHyperText Topics “Graphs”, topic 8: DFS, BFS 2

Graph Representations 3 0 1 2 3 4 0FFFFF1FFTTF2FFFFT3FFTFT4FFFFF 1 2 3 4 1 2 3 4 3 4 4 2 2 Adjacency list Adjacency matrix

(improved definitions in Lec 17) Review: Sparse vs. Dense 4

Graph Search 5

Graph Traversal Goal: visit each vertex that is reachable from some starting vertex And: even if there are many paths to a node, visit only once Two algorithms: DFS, BFS61725346 8 1 7 2 5 3 4 6 8

Intuition: one person exploring a maze Depth-First Search (DFS) 7

Depth-First Search /** 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);}8Idea: Recursively visit each unvisited neighbor172 5 3 4 6 8 1 2 3 5 7 8 dfs (1) visits the nodes in this order: 1, 2, 3, 5, 7, 8

Poll #1 9

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 void dfs (Vertex v) { mark v visited; for all edges ( v,u): if u is unmarked: dfs(u);}11Suppose graph has V vertices and E edgesSpace required?Mark for each vertex: O(V)Frame for each recursive call: O(V)Worst case: O(V) 1 7 2 5 3 4 6 8

DFS Time Efficiency void dfs (Vertex v) { mark v visited; for all edges (v,u): if u is unmarked: dfs(u);}12Suppose graph has V vertices and E edgesTime required?Mark each vertex: O(V)Recursive call for on each unvisited vertex: O(V)Find each edgein adj list: O(E): Worst case: O(V+E)In adj matrix: O(V2 ): Worst case: O(V2) 1 2 3 4

void dfs (Vertex u) { Stack s= new Stack(); s.push (u); while (s is not empty) { u= s.pop(); if (u not visited) { visit u; for each edge (u, v): s.push(v); } }}Variant: Iterative DFS1317 2 5 3 4 6 8 1 Stack: 1 2 5 7 3 8 2 3 5 7 8 Same algorithm; non-recursive implementation u: 1 7 5 8 5 2 5 5 3 5 5 Visit order was 1, 7, 8, 5, 2, 3: differs from before because of order edges processed Demo

Intuition: Search party fanning out in all directions Breadth-First Search (BFS) 14

Breadth-First Search 15 Idea: Iteratively process the graph in "layers" moving further away from the source vertex. 1 7 2 5 346 8 1 2 3 5 7 8

Breadth-First Search 16 1 7 2 5 3 46 8 1 1 7 5 2 5 3 5 8 2 3 5 7 8 /** Visit all vertices reachable on unvisited paths from u. */ void dfs (int u) { Stack s= new Stack() s.push (u); while ( ) { u= s.pop (); if (u not visited) { visit u; for each edge (u, v): s.push (v); } } } /** Visit all vertices reachable 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): q.add (v); } } } Idea: Iteratively process the graph in "layers" moving further away from the source vertex. Queue: Visit order was 1, 2, 5, 7, 3, 8 Demo

Poll #2 17

Improved BFS 18 /** 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); } } } } Idea: Don’t put vertex in queue if already encountered

Analyzing BFS 19 /** 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); } } } } Same as DFS.Space: O(V)Time:Adj list: O(V+E) Adj matrix: O(V2)

Comparing Traversal Algorithms Time: O(V+E) or O(V 2 )Space: O(V)Time: O(V+E) or O(V2 ) Space: O(V) DFS (recursive) DFS & BFS (iterative, improved*)20*Without improvement, space becomes O(E)