400 likes | 424 Views
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 .
E N D
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. • The distance from s to v is 2. r s t u v w x y
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.
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.
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.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
Content • Breadth-first search • Depth-first search
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
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
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
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)
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
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)
Depth-first search • The predecessor subgraph is a depth-first forest. u w z v y x
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)
Depth-first search • Classification of edges • Tree edges • Back edges • Forward edges • Cross edges u w C z v F B y x
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.
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.
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)
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)
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
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
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?