CS 3343 -- Spring 2009 Graphs (review) Definition. A directed graph ( digraph ) G = ( V , E ) is an ordered pair consisting of • a set V of vertices (singular: vertex ), • a set E ⊆ V × V of edges . In an undirected graph G = ( V , E ), the edge set E consists of unordered pairs of vertices. Graphs In either case, we have | E | = O (| V| 2 ). Moreover, if G is connected, then | E | ≥ | V | – 1. Carola Wenk Slides courtesy of Charles Leiserson with (Review CLRS, Appendix B.4 and B.5.) changes and additions by Carola Wenk 3/25/09 CS 3343 Analysis of Algorithms 1 3/25/09 CS 3343 Analysis of Algorithms 2 Adjacency-matrix Adjacency-list representation representation An adjacency list of a vertex v ∈ V is the list Adj [ v ] The adjacency matrix of a graph G = ( V , E ), where of vertices adjacent to v . V = {1, 2, …, n }, is the matrix A [1 . . n , 1 . . n ] Adj [1] = {2, 3} 2 1 given by 2 1 1 if ( i , j ) ∈ E, Adj [2] = {3} A [ i , j ] = Adj [3] = {} 0 if ( i , j ) ∉ E. 3 4 3 4 Adj [4] = {3} A 1 2 3 4 2 1 For undirected graphs, | Adj [ v ] | = degree ( v ). Θ (| V| 2 ) storage 2 1 1 0 1 1 0 ⇒ dense 2 0 0 1 0 For digraphs, | Adj [ v ] | = out-degree ( v ). representation. 3 4 3 0 0 0 0 3 4 4 0 0 1 0 3/25/09 CS 3343 Analysis of Algorithms 3 3/25/09 CS 3343 Analysis of Algorithms 4 1
Adjacency-list representation Graph Traversal Handshaking Lemma: Let G =( V , E ) be a (directed or undirected) Every edge is counted twice graph, given in adjacency list representation. • For undirected graphs: ∑ v ∈ V degree(v) = 2 | E | | V | = n , | E | = m • For digraphs: ∑ v ∈ V in-degree(v) + ∑ v ∈ V out-degree(v) = 2 | E | A graph traversal visits every vertex: • Breadth-first search (BFS) ⇒ adjacency lists use Θ (| V| + |E| ) storage • Depth-first search (DFS) ⇒ a sparse representation ⇒ We usually use this representation, unless stated otherwise 3/25/09 CS 3343 Analysis of Algorithms 5 3/25/09 CS 3343 Analysis of Algorithms 6 Example of breadth-first Breadth-First Search (BFS) search BFS( G= ( V,E )) Mark all vertices in G as “unvisited” // time=0 a f h a f h Initialize empty queue Q for each vertex v ∈ V do d d if v is unvisited visit v // time++ b g BFS_iter( G ) b g Q .enqueue( v ) while Q is non-empty do BFS_iter( G ) e i e i v = Q .dequeue() for each w adjacent to v do c c if w is unvisited visit w // time++ Add edge ( v , w ) to T Q : Q .enqueue( w ) 3/25/09 CS 3343 Analysis of Algorithms 7 3/25/09 CS 3343 Analysis of Algorithms 8 2
Example of breadth-first Example of breadth-first search search 0 a f h 0 a f h a f h a f h 2 d d d d b g b g 1 b g b g e i e i e i e i c c c c 1 2 0 Q: a Q: a b d 3/25/09 CS 3343 Analysis of Algorithms 9 3/25/09 CS 3343 Analysis of Algorithms 10 Example of breadth-first Example of breadth-first search search a f h a f h 0 0 a f h a f h 2 2 d d d d b g b g 1 1 b g b g e i e i e i e i c 4 c 4 3 3 c c 2 3 4 3 4 Q: a b d c e Q: a b d c e 3/25/09 CS 3343 Analysis of Algorithms 11 3/25/09 CS 3343 Analysis of Algorithms 12 3
Example of breadth-first Example of breadth-first search search 0 a f h 0 a f h a f h a f h 2 2 d d d d 5 b g b g 1 1 b g b g e i e i e i e i c 4 c 4 6 3 3 c c 4 5 6 Q: a b d c e Q: a b d c e g i 3/25/09 CS 3343 Analysis of Algorithms 13 3/25/09 CS 3343 Analysis of Algorithms 14 Example of breadth-first Example of breadth-first search search 7 7 8 a f h a f h 0 0 a f h a f h 2 2 d d a d d 5 5 a b g b g 1 1 b g b g e i e i e i e i c 4 6 c 4 6 3 3 c c 6 7 7 8 Q: a b d c e g i f Q: a b d c e g i f h 3/25/09 CS 3343 Analysis of Algorithms 15 3/25/09 CS 3343 Analysis of Algorithms 16 4
Example of breadth-first Example of breadth-first search search 7 8 7 8 0 a f h 0 a f h a f h a f h 2 2 d d d a d a 5 a 5 a b g b g 1 1 b g b g e i e i e i e i c 4 6 c 4 6 3 3 c c 8 Q: a b d c e g i f h Q: a b d c e g i f h 3/25/09 CS 3343 Analysis of Algorithms 17 3/25/09 CS 3343 Analysis of Algorithms 18 Example of breadth-first Breadth-First Search (BFS) search BFS( G= ( V,E )) 7 8 Mark all vertices in G as “unvisited” // time=0 O( n ) a f h 0 a f h Initialize empty queue Q 2 O(1) for each vertex v ∈ V do d a d if v is unvisited 5 a O( n ) visit v // time++ b g 1 b g BFS_iter( G ) without Q .enqueue( v ) while Q is non-empty do BFS_iter BFS_iter( G ) e i e i v = Q .dequeue() for each w adjacent to v do c 4 6 3 c if w is unvisited O( m ) visit w // time++ O( deg ( v )) Add edge ( v , w ) to T Q: a b d c e g i f h Q .enqueue( w ) 3/25/09 CS 3343 Analysis of Algorithms 19 3/25/09 CS 3343 Analysis of Algorithms 20 5
BFS runtime Depth-First Search (DFS) DFS( G= ( V,E )) Mark all vertices in G as “unvisited” // time=0 • Each vertex is marked as unvisited in the beginning ⇒ O( n ) time for each vertex v ∈ V do • Each vertex is marked at most once, enqueued at most once, if v is unvisited and therefore dequeued at most once DFS_rec( G , v ) • The time to process a vertex is proportional to the size of its adjacency list (its degree), since the graph is given in adjacency list DFS_rec( G, v ) representation visit v // d [ v ]=++time ⇒ O( m ) time for each w adjacent to v do • Total runtime is O( n + m ) = O(|V| + |E|) if w is unvisited Add edge ( v , w ) to tree T DFS_rec( G , w ) // f [ v ]=++time 3/25/09 CS 3343 Analysis of Algorithms 21 3/25/09 CS 3343 Analysis of Algorithms 22 Example of depth-first search Example of depth-first search d / f d / f 0/- a a f h 0/- a f h a a f h a f h d d d d b g 1/- b g b g b g e i e i e i e i c c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array - a - a b 3/25/09 CS 3343 Analysis of Algorithms 23 3/25/09 CS 3343 Analysis of Algorithms 24 6
Example of depth-first search Example of depth-first search d / f d / f 0/- 0/- a f h a f h a f h a f h d d d d 1/- b g 1/- b g b g b g e i e i e i e i 2/3 2/- c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array a a - b - b b 3/25/09 CS 3343 Analysis of Algorithms 25 3/25/09 CS 3343 Analysis of Algorithms 26 Example of depth-first search Example of depth-first search d / f d / f 0/- a f h 0/- a f h a f h a f h d d d d 5/- 1/- b g 1/- b g b g b g e i e i e i e i 4/- 4/- 2/3 c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array - a b e - a b e g b b 3/25/09 CS 3343 Analysis of Algorithms 27 3/25/09 CS 3343 Analysis of Algorithms 28 7
Example of depth-first search Example of depth-first search d / f d / f 0/- 0/- a f h a f h 7/- 7/8 a f h a f h d d d d 5/- 5/- 1/- b g 1/- b g b g b g e i e i 6/- 6/- e i e i 4/- 4/- 2/3 c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array a e i g a e i g - b b - b b 3/25/09 CS 3343 Analysis of Algorithms 29 3/25/09 CS 3343 Analysis of Algorithms 30 Example of depth-first search Example of depth-first search d / f d / f 0/- a f h 0/- a f h 7/8 7/8 a f h a f h d d d d 5/- 5/- 1/- b g 1/- b g b g b g e i e i 6/- 6/9 6/9 e i e i 4/- 4/- 2/3 c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array - a b e i g - a b g e i g b b 3/25/09 CS 3343 Analysis of Algorithms 31 3/25/09 CS 3343 Analysis of Algorithms 32 8
Example of depth-first search Example of depth-first search d / f d / f 10/- 10/- 0/- 0/- a f h a f h 7/8 7/8 a f h a f h 11/12 11/- d d d d 5/- 5/- 1/- b g 1/- b g b g b g e i e i 6/9 6/9 e i e i 4/- 4/- 2/3 c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array a g e i g a g e i g - b f b - b f b 3/25/09 CS 3343 Analysis of Algorithms 33 3/25/09 CS 3343 Analysis of Algorithms 34 Example of depth-first search Example of depth-first search d / f d / f 10/- 10/13 10/13 0/- a f h 0/- a f h 7/8 7/8 a f h a f h 11/12 11/12 d d d d 5/- 5/- 5/14 1/- b g 1/- b g b g b g e i e i 6/9 6/9 e i e i 4/- 4/- 2/3 c 2/3 c c c Store edges in Store edges in π : a b c d e f g h i π : a b c d e f g h i predecessor array predecessor array - a b g e i g - a b g e i g f b f b 3/25/09 CS 3343 Analysis of Algorithms 35 3/25/09 CS 3343 Analysis of Algorithms 36 9
Recommend
More recommend