1 / 39

Elementary Graph Algorithms

Elementary Graph Algorithms. Heejin Park College of Information and Communications Hanyang University. Content. Breadth-first search Depth-first search. Breadth-first search. Distance Distance from u to v The number of edges in the shortest path from u to v .

pedrotaylor
Download Presentation

Elementary Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elementary Graph Algorithms Heejin Park College of Information and Communications Hanyang University

  2. Content • Breadth-first search • Depth-first search

  3. Breadth-first search • Distance • Distance from u to v • The number of edges in the shortest path from u to v. • The distance from s to v is 2. r s t u v w x y

  4. r s t u ∽ 0 ∽ ∽ ∽ ∽ ∽ ∽ v w x y Breadth-first search • Breadth-first search • Given a graph G = (V, E)and asource vertex s, itexplores the edges of G to "discover" every reachable vertex from s. • It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.

  5. r s t u 1 0 ∽ ∽ ∽ 1 ∽ ∽ v w x y Breadth-first search • Breadth-first search • Given a graph G = (V, E)and asource vertex s, itexplores the edges of G to "discover" every reachable vertex from s. • It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.

  6. r s t u 1 0 2 ∽ 2 1 2 ∽ v w x y Breadth-first search • Breadth-first search • Given a graph G = (V, E)and asource vertex s, itexplores the edges of G to "discover" every reachable vertex from s. • It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.

  7. r s t u 1 0 2 3 2 1 2 3 v w x y Breadth-first search • Breadth-first search • Given a graph G = (V, E)and asource vertex s, itexplores the edges of G to "discover" every reachable vertex from s. • It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.

  8. Breadth-first search • Breadth-first search • It also computes • the distance of vertices from the source: d[u] = 3 • the predecessor of vertices: π[u] = t r s t u 1 0 2 3 2 1 2 3 v w x y

  9. Breadth-first search • The predecessor subgraph of G as Gπ= (Vπ, Eπ), • Vπ= {v ∈V: π[v] ≠ NIL} U { s} • Eπ= {(π[v], v) : v ∈ Vπ- {s}}. r s t u 1 0 2 3 2 1 2 3 v w x y

  10. Breadth-first search • The predecessor subgraph Gπ is a breadth-first tree. • since it is connected and |Eπ| = |Vπ| -1. • The edges in Eπare called tree edges. r s t u 1 0 2 3 2 1 2 3 v w x y

  11. Breadth-first search BFS(G, s) 1 for each vertex u V [G] - {s} 2 do color[u] ← WHITE 3 d[u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d[s] ← 0 7 π[s] ← NIL 8 Q ← Ø 9 ENQUEUE(Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE(Q) 12 for each v ∈ Adj[u] 13 do if color[v] = WHITE 14 then color[v] ← GRAY 15 d[v] ← d[u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color[u] ← BLACK

  12. r s t u ∽ 0 ∽ ∽ ∽ ∽ ∽ ∽ v w x y Breadth-first search r s v r w s t u w x t x y u r v s Q s t x w 0 t u x y x x y u

  13. r s t u 1 0 ∽ ∽ ∽ 1 ∽ ∽ v w x y Breadth-first search • white: not discovered (not entered the Q) • gray: discovered (in the Q) • black: finished (out of the Q) r s v r w s t u w x t x y u r v s t x w w r Q t u x y x 1 1 x y u

  14. r s t u 1 0 2 ∽ ∽ 1 2 ∽ v w x y Breadth-first search r s v r w s t u w x t x y u r v r t x Q s t x w 1 2 2 t u x y x x y u

  15. r s t u 1 0 2 ∽ 2 1 2 ∽ v w x y Breadth-first search r s v r w s t u w x t x y u r v t x v Q s t x w 2 2 2 t u x y x x y u

  16. r s t u 1 0 2 3 2 1 2 ∽ v w x y Breadth-first search r s v r w s t u w x t x y u r v x v u Q s t x w 2 2 3 t u x y x x y u

  17. r s t u 1 0 2 3 2 1 2 3 v w x y Breadth-first search r s v r w s t u w x t x y u r v v u y Q s t x w 2 3 3 t u x y x x y u

  18. r s t u 1 0 2 3 2 1 2 3 v w x y Breadth-first search r s v r w s t u w x t x y u r v u y Q s t x w 3 3 t u x y x x y u

  19. r s t u 1 0 2 3 2 1 2 3 v w x y Breadth-first search r s v r w s t u w x t x y u r v y Q s t x w 3 t u x y x x y u

  20. r s t u 1 0 2 3 2 1 2 3 v w x y Breadth-first search r s v r w s t u w x t x y u r v ¢ Q s t x w 3 t u x y x x y u

  21. Breadth-first search BFS(G, s) 1 for each vertex u V [G] - {s} 2 do color[u] ← WHITE 3 d[u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d[s] ← 0 7 π[s] ← NIL 8 Q ← Ø 9 ENQUEUE(Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE(Q) 12 for each v ∈ Adj[u] 13 do if color[v] = WHITE 14 then color[v] ← GRAY 15 d[v] ← d[u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color[u] ← BLACK

  22. Analysis • Running time • Initialization: Θ(V) • Exploring the graph: O(E) • An enque operation for an edge exploration. • #deque operations = #enque operations • An edge is explored at most once. • Overall:O(V + E)

  23. Content • Breadth-first search • Depth-first search

  24. Depth-first search • Colors of vertices • Each vertex is initially white. (not discovered) • The vertex is grayed when it is discovered. • The vertex is blackened when it is finished, that is, when its adjacency list has been examined completely. u v w y z x

  25. Depth-first search • Timestamps • Each vertex v has two timestamps. • d[v]: discovery time (when v is grayed) • f [v]: finishing time (when v is blacken) u v w 1/ 2/ 4/5 3/ y z x

  26. u v w u v w 1/ 1/ 2/ 3/ y z x (c) u v w u v w y z x 1/ 2/ 1/ 2/ (a) 4/ 3/ y z x y z x (b) (d) Depth-first search

  27. u u v v w w 1/ 1/ 2/ 2/ 4/ 4/5 3/ 3/ y y z z x x (e) (f) u v w 1/ 2/7 4/5 3/6 y z x (h) Depth-first search u v w 1/ 2/ 4/5 3/6 y z x (g)

  28. u v w 1/ 2/7 4/5 3/6 y z x (i) u v w u v w u v w 1/8 2/7 9/ 1/8 2/7 1/8 2/7 9/ 4/5 3/6 4/5 3/6 4/5 3/6 y z y z x x y z x (k) (j) (l) Depth-first search

  29. u u v v w w 1/8 1/8 2/7 2/7 9/ 9/12 4/5 4/5 3/6 3/6 10/11 10/11 y y z z x x (o) (p) Depth-first search u v w u v w 1/8 2/7 9/ 1/8 2/7 9/ 4/5 3/6 10/ 4/5 3/6 10/ y z x y z x (m) (n)

  30. Depth-first search • The predecessor subgraph is a depth-first forest. u w z v y x

  31. Depth-first search • Parenthesis theorem (forgray time) • Inclusion: The ancestor includes the descendants. • Disjoint: Otherwise. u w z v y x 1 2 3 4 5 6 7 8 9 10 11 12 (u (v (y (x x) y) v) u) (w (z z) w)

  32. Depth-first search • Classification of edges • Tree edges • Back edges • Forward edges • Cross edges u w C z v F B y x

  33. Depth-first search • Tree edges:Edges in the depth-first forest. • Back edges: Those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. Self-loops are considered to be back edges. • Forward edges: Those edges (u, v) connecting a vertex u to a descendant v in a depth-first tree. • Cross edges: All other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees.

  34. Depth-first search • Classification by the DFS algorithm • Each edge (u, v) can be classified by the color of the vertex v that is reached when the edge is first explored: • WHITE indicates a tree edge, • GRAY indicates a back edge, and • BLACK indicates a forward or cross edge.

  35. Depth-first search u w C z v F B y x 1 2 3 4 5 6 7 8 9 10 11 12 (u (v (y (x x) y) v) u) (w (z z) w)

  36. u u v v w w 1/ 1/ 2/ 2/ B B 4/5 4/ 3/ 3/ y y z z x x (f) (e) u v w 1/ 2/7 B 4/5 3/6 y z x (h) Depth-first search u v w 1/ 2/ B 4/5 3/6 y z x (g)

  37. u v w 1/ 2/7 B F 4/5 3/6 y z x (i) u v w u v w u v w 1/8 2/7 9/ 1/8 2/7 1/8 2/7 9/ B B C F B F F 4/5 3/6 4/5 3/6 4/5 3/6 y z y z x x y z x (k) (j) (l) Depth-first search

  38. u u v v w w 1/8 1/8 2/7 2/7 9/12 9/ C C B B B B 4/5 4/5 3/6 3/6 10/11 10/11 y y z z x x (o) (p) Depth-first search u v w u v w 1/8 2/7 9/ 1/8 2/7 9/ B C F C B F B 4/5 3/6 10/ 4/5 3/6 10/ y z x y z x (m) (n) F F

  39. Depth-first search • In a depth-first search of an undirected graph, every edge of G is either a tree edge or a back edge. • Forward edge? • Cross edge?

More Related