Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion #31 1/11 Discussion #31 Adjacency Lists; Breadth-First Search & Depth-First Search.

Similar presentations


Presentation on theme: "Discussion #31 1/11 Discussion #31 Adjacency Lists; Breadth-First Search & Depth-First Search."— Presentation transcript:

1 Discussion #31 1/11 Discussion #31 Adjacency Lists; Breadth-First Search & Depth-First Search

2 Discussion #31 2/11 Topics Adjacency list representation Breadth-First Search (BFS) Depth-First Search (DFS)

3 Discussion #31 3/11 Adjacency Lists For directed graphs: b a c 1 a123 2 b3 3 c2 bc cb cbaa Simple Notation { (a,a), (a,b), (a,c), (b,c), (c,b) } 010 c 100 b 111 a cba

4 Discussion #31 4/11 Adjacency Lists for Undirected and Weighted Graphs Undirected Graphs: aabc bac cab Make each edge (except loops) go both ways. b a c Weighted Graphs: a(a,1)(b,5)(c,3) b(a,5)(c,7) c(a,3)(b,7) - add additional field to node - node-weight pairs

5 Discussion #31 5/11 Breadth-First Search (BFS)

6 Discussion #31 6/11 BFS Algorithm aabc bac cabd dc queue: b c d a     a b c d n nodes O(n) ~ 2m edges O(m) = O(n+2m) = O(m) if m>>n Undirected edges: each edge twice

7 Discussion #31 7/11 Depth-First Search (DFS)

8 Discussion #31 8/11 DFS Algorithm aabc bac cabd dc n nodes O(n) ~ 2m edges O(m) = O(n+2m) = O(m) if m>>n stack: b c d a a b c d    

9 Discussion #31 9/11 BFS vs. DFS ab ec f d BFS Queue aabce bacd cabf db ea fc b c e      d     f    Done! a BFS Order: a b c e d f

10 Discussion #31 10/11 BFS vs. DFS aabce bacd cabf db ea fc  BFS Queue b c e df Done! BFS Order: a b c e d f a DFS Stack a b   c  f  d  e  pop ab ec f d DFS Order: a b c f d e Done!

11 Discussion #31 11/11 a bcd efghi jk l mn BFS & DFS with Directed Graphs BFS DFS a bcd efghi jk l mn a,b,c,d,e,f,g,h,i,j,k,l,m,n Same as before, by chance a,b,e,j,f,k,l,h,c,g,d,i,m,n Not same as before


Download ppt "Discussion #31 1/11 Discussion #31 Adjacency Lists; Breadth-First Search & Depth-First Search."

Similar presentations


Ads by Google