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