graphs
play

Graphs Carola Wenk Slides courtesy of Charles Leiserson with - PowerPoint PPT Presentation

CS 5633 -- Spring 2010 Graphs Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 3/25/10 CS 5633 Analysis of Algorithms 1 Graphs (review) Definition. A directed graph ( digraph ) G = ( V , E ) is an


  1. CS 5633 -- Spring 2010 Graphs Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 3/25/10 CS 5633 Analysis of Algorithms 1

  2. 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. In either case, we have | E | = O (| V| 2 ). Moreover, if G is connected, then | E | ≥ | V | – 1. (Review CLRS, Appendix B.4 and B.5.) 3/25/10 CS 5633 Analysis of Algorithms 2

  3. Adjacency-matrix representation The adjacency matrix of a graph G = ( V , E ), where V = {1, 2, …, n }, is the matrix A [1 . . n , 1 . . n ] given by 1 if ( i , j ) ∈ E, A [ i , j ] = 0 if ( i , j ) ∉ E. A 1 2 3 4 2 1 Θ (| V| 2 ) storage 2 1 1 0 1 1 0 ⇒ dense 2 0 0 1 0 representation. 3 4 3 0 0 0 0 3 4 4 0 0 1 0 3/25/10 CS 5633 Analysis of Algorithms 3

  4. Adjacency-list representation An adjacency list of a vertex v ∈ V is the list Adj [ v ] of vertices adjacent to v . Adj [1] = {2, 3} 2 1 2 1 Adj [2] = {3} Adj [3] = {} 3 4 3 4 Adj [4] = {3} For undirected graphs, | Adj [ v ] | = degree ( v ). For digraphs, | Adj [ v ] | = out-degree ( v ). 3/25/10 CS 5633 Analysis of Algorithms 4

  5. Adjacency-list representation Handshaking Lemma: Every edge is counted twice • For undirected graphs: ∑ v ∈ V degree(v) = 2 | E | • For digraphs: ∑ v ∈ V in-degree(v) + ∑ v ∈ V out-degree(v) = 2 | E | ⇒ adjacency lists use Θ (| V| + |E| ) storage ⇒ a sparse representation ⇒ We usually use this representation, unless stated otherwise 3/25/10 CS 5633 Analysis of Algorithms 5

  6. Graph Traversal 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) 3/25/10 CS 5633 Analysis of Algorithms 6

  7. Breadth-First Search (BFS) BFS( G= ( V,E )) Mark all vertices in G as “unvisited” // time=0 Initialize empty queue Q for each vertex v ∈ V do if v is unvisited visit v // time++ BFS_iter( G ) Q .enqueue( v ) while Q is non-empty do BFS_iter( G ) v = Q .dequeue() for each w adjacent to v do if w is unvisited visit w // time++ Add edge ( v , w ) to T Q .enqueue( w ) 3/25/10 CS 5633 Analysis of Algorithms 7

  8. Example of breadth-first search a f h a f h d d b g b g e i e i c c Q : 3/25/10 CS 5633 Analysis of Algorithms 8

  9. Example of breadth-first search a f h 0 a f h d d b g b g e i e i c c 0 Q: a 3/25/10 CS 5633 Analysis of Algorithms 9

  10. Example of breadth-first search a f h 0 a f h 2 d d b g 1 b g e i e i c c 1 2 Q: a b d 3/25/10 CS 5633 Analysis of Algorithms 10

  11. Example of breadth-first search a f h 0 a f h 2 d d b g 1 b g e i e i c 4 3 c 2 3 4 Q: a b d c e 3/25/10 CS 5633 Analysis of Algorithms 11

  12. Example of breadth-first search a f h 0 a f h 2 d d b g 1 b g e i e i c 4 3 c 3 4 Q: a b d c e 3/25/10 CS 5633 Analysis of Algorithms 12

  13. Example of breadth-first search a f h 0 a f h 2 d d b g 1 b g e i e i c 4 3 c 4 Q: a b d c e 3/25/10 CS 5633 Analysis of Algorithms 13

  14. Example of breadth-first search a f h 0 a f h 2 d d 5 b g 1 b g e i e i c 4 6 3 c 5 6 Q: a b d c e g i 3/25/10 CS 5633 Analysis of Algorithms 14

  15. Example of breadth-first search 7 a f h 0 a f h 2 d d 5 b g 1 b g e i e i c 4 6 3 c 6 7 Q: a b d c e g i f 3/25/10 CS 5633 Analysis of Algorithms 15

  16. Example of breadth-first search 7 8 a f h 0 a f h 2 d a d 5 a b g 1 b g e i e i c 4 6 3 c 7 8 Q: a b d c e g i f h 3/25/10 CS 5633 Analysis of Algorithms 16

  17. Example of breadth-first search 7 8 a f h 0 a f h 2 d a d 5 a b g 1 b g e i e i c 4 6 3 c 8 Q: a b d c e g i f h 3/25/10 CS 5633 Analysis of Algorithms 17

  18. Example of breadth-first search 7 8 a f h 0 a f h 2 d a d 5 a b g 1 b g e i e i c 4 6 3 c Q: a b d c e g i f h 3/25/10 CS 5633 Analysis of Algorithms 18

  19. Example of breadth-first search 7 8 a f h 0 a f h 2 d a d 5 a b g 1 b g e i e i c 4 6 3 c Q: a b d c e g i f h 3/25/10 CS 5633 Analysis of Algorithms 19

  20. Breadth-First Search (BFS) BFS( G= ( V,E )) Mark all vertices in G as “unvisited” // time=0 O( n ) Initialize empty queue Q O(1) for each vertex v ∈ V do if v is unvisited O( n ) visit v // time++ BFS_iter( G ) without Q .enqueue( v ) while Q is non-empty do BFS_iter BFS_iter( G ) v = Q .dequeue() for each w adjacent to v do if w is unvisited O( m ) visit w // time++ O( deg ( v )) Add edge ( v , w ) to T Q .enqueue( w ) 3/25/10 CS 5633 Analysis of Algorithms 20

  21. 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 adjacency list (its degree), since the graph is given in adjacency list representation ⇒ O( m ) time • Total runtime is O( n + m ) = O(|V| + |E|) 3/25/10 CS 5633 Analysis of Algorithms 21

  22. Depth-First Search (DFS) DFS( G= ( V,E )) Mark all vertices in G as “unvisited” // time=0 for each vertex v ∈ V do if v is unvisited DFS_rec( G , v ) DFS_rec( G, v ) visit v // d [ v ]=++time for each w adjacent to v do if w is unvisited Add edge ( v , w ) to tree T DFS_rec( G , w ) // f [ v ]=++time 3/25/10 CS 5633 Analysis of Algorithms 22

  23. Example of depth-first search d / f 0/- a a f h a a f h d d b g b g e i e i c c Store edges in π : a b c d e f g h i predecessor array a - 3/25/10 CS 5633 Analysis of Algorithms 23

  24. Example of depth-first search d / f 0/- a f h a f h d d 1/- b g b g e i e i c c Store edges in π : a b c d e f g h i predecessor array a - b 3/25/10 CS 5633 Analysis of Algorithms 24

  25. Example of depth-first search d / f 0/- a f h a f h d d 1/- b g b g e i e i 2/3 2/- c c Store edges in π : a b c d e f g h i predecessor array a - b 3/25/10 CS 5633 Analysis of Algorithms 25

  26. Example of depth-first search d / f 0/- a f h a f h d d 1/- b g b g e i e i 2/3 c c Store edges in π : a b c d e f g h i predecessor array a - b b 3/25/10 CS 5633 Analysis of Algorithms 26

  27. Example of depth-first search d / f 0/- a f h a f h d d 1/- b g b g e i e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array a e - b b 3/25/10 CS 5633 Analysis of Algorithms 27

  28. Example of depth-first search d / f 0/- a f h a f h d d 5/- 1/- b g b g e i e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array a e g - b b 3/25/10 CS 5633 Analysis of Algorithms 28

  29. Example of depth-first search d / f 0/- a f h a f h d d 5/- 1/- b g b g e i 6/- e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array a e i g - b b 3/25/10 CS 5633 Analysis of Algorithms 29

  30. Example of depth-first search d / f 0/- a f h 7/- 7/8 a f h d d 5/- 1/- b g b g e i 6/- e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array a e i g - b b 3/25/10 CS 5633 Analysis of Algorithms 30

  31. Example of depth-first search d / f 0/- a f h 7/8 a f h d d 5/- 1/- b g b g e i 6/- 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array a e i g - b b 3/25/10 CS 5633 Analysis of Algorithms 31

  32. Example of depth-first search d / f 0/- a f h 7/8 a f h d d 5/- 1/- b g b g e i 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array g a e i g - b b 3/25/10 CS 5633 Analysis of Algorithms 32

  33. Example of depth-first search d / f 10/- 0/- a f h 7/8 a f h d d 5/- 1/- b g b g e i 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array g a e i g - b f b 3/25/10 CS 5633 Analysis of Algorithms 33

  34. Example of depth-first search d / f 10/- 0/- a f h 7/8 a f h 11/- 11/12 d d 5/- 1/- b g b g e i 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array g a e i g - b f b 3/25/10 CS 5633 Analysis of Algorithms 34

  35. Example of depth-first search d / f 10/- 10/13 0/- a f h 7/8 a f h 11/12 d d 5/- 1/- b g b g e i 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array g a e i g - b f b 3/25/10 CS 5633 Analysis of Algorithms 35

  36. Example of depth-first search d / f 10/13 0/- a f h 7/8 a f h 11/12 d d 5/- 5/14 1/- b g b g e i 6/9 e i 4/- 2/3 c c Store edges in π : a b c d e f g h i predecessor array g a e i g - b f b 3/25/10 CS 5633 Analysis of Algorithms 36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend