Slide 1

Slide 1 text

Chapter 22 Elementary Graph Algorithms Sun-Yuan Hsieh 謝孫源 教授 成功大學資訊工程學系

Slide 2

Slide 2 text

Outline Graph representation Breadth-first search Depth-first search Topological sort Strongly connected components 2

Slide 3

Slide 3 text

Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: 1. Adjacency lists. 2. Adjacency matrix. 3

Slide 4

Slide 4 text

When expressing the running time of an algorithm, it’s often in terms of both |V| and |E|. In asymptotic notation  and only in asymptotic notation  we’ll drop the cardinality. Example: (V+E). 4

Slide 5

Slide 5 text

Adjacency lists Array Adj of |V| lists, one per vertex. Vertex u’s list has all vertices v such that (u, v)  E. (Works for both directed and undirected graphs.) Example: For an undirected graph: 1 2 5 4 3 1 2 3 4 5 2 1 2 2 4 5 5 4 5 1 / / / / / 4 3 2 3 Adj 5

Slide 6

Slide 6 text

Adjacency lists If edges have weights, can put the weights in the lists. Weight: w : E  R Space: (V+E) Time: to list all vertices adjacent to u: (degree(u)) Time: to determine if (u, v)  E: (degree(u)) 6

Slide 7

Slide 7 text

Example: For a directed graph: Same asymptotic space and time Adj 1 2 3 4 2 4 1 4 3 / / / / 7 1 2 3 4

Slide 8

Slide 8 text

Adjacency matrix Space: (V2) Time: to list all vertices adjacent to u: (V) Time: to determine if (u, v)  E: (1) Can store weights instead of bits for weighted graph 1 2 3 4 5 1 2 3 4 5 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 2 3 4 4 3 2 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 8

Slide 9

Slide 9 text

Breadth-first search Input: Graph G = (V,E), either directed or undirected, and source vertex s  V Output: d[v] = distance (smallest # of edges) from s to v, for all v  V Also [v] = u such that (u, v) is last edge on a shortest path s → v • u is v’s predecessor • set of edges {([v], v) : v ≠ s} forms a tree 9

Slide 10

Slide 10 text

Breadth-first search Idea: Send a wave out from s. • First hits all vertices 1 edge from s. • From there, hits all vertices 2 edges from s. • Etc. Use FIFO queue Q to maintain wavefront. • v  Q if and only if wave has hit v but has not come out of v yet. 10

Slide 11

Slide 11 text

(a) 0 t v  w  r s    y u   x s 0 Q (b) 0 t v w  r s   y u   x w 1 Q 1 1 r 1 (c) 0 t v w  r s y u   x r 1 Q 1 1 t 2 2 2 x (d) 0 t v w r s y u   x t 2 2 Q 1 1 x 2 2 v 2 2 2 11

Slide 12

Slide 12 text

(e) 0 t v w r s y u  x x Q 1 1 v 2 2 u 2 3 (f) 0 t v w r s y u x v Q 1 1 u 2 2 y 2 3 3 (g) 0 t w r s y u x u 3 Q 1 1 y 3 2 2 v 2 3 (h) 0 t w r s y u x y 3 1 1 2 2 2 3 3 3 v 2 3 2 3 3 2 2 12

Slide 13

Slide 13 text

(i) 0 t v w r s y u x Q 1 1 2 2 2 3 3  v 2 13

Slide 14

Slide 14 text

BFS(V, E, s) for each u  V – {s} do d[u] ← ∞ d[s] ← 0 Q ←φ ENQUEUE(Q, s) While Q ≠ φ do u ← DEQUEUE(Q) for each v  Adj[u] do if d[v] = ∞ then d[v] ← d[u] + 1 ENQUEUE(Q, v) 14

Slide 15

Slide 15 text

Example: directed graph a b e c g h f i s 0 1 3 2 1 3 2 3 3 Can show that Q consists of vertices with d values. i i i …. i i+1 i+1 … i+1 • Only 1 or 2 values. • If 2, differ by 1 and all smallest are first. 15

Slide 16

Slide 16 text

Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. BFS may not reach all vertices. Time = O(V + E). • O(V) because every vertex enqueued at most once. • O(E) because every vertex dequeued at most once and we examine (u, v) only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected. 16

Slide 17

Slide 17 text

Depth-first search Input: G = (V, E), directed or undirected. No source vertex given! Output: 2 timestamps on each vertex: d[v] = discovery time f [v] = finishing time These will be useful for other algorithms later on. Can also compute [v]. 17

Slide 18

Slide 18 text

Will methodically explore every edge. Start over from different vertices as necessary. As soon as we discover a vertex, explore from it. Unlike BFS, which puts a vertex on a queue so that we explore from it later. 18

Slide 19

Slide 19 text

As DFS progresses, every vertex has a color: WHITE = undiscovered GRAY = discovered, but not finished (not done exploring from it) BLACK = finished (have found everything reachable from it) Discovery and finish times: Unique integers from 1 to 2|V|. For all v, d[v] < f[v]. In other words, 1 ≤ d[v] < f[v] ≤ 2|V|. 19

Slide 20

Slide 20 text

1/ (a) u v w x y z 1/ 2/ (b) u v w x y z 1/ 2/ 3/ (c) u v w x y z 1/ 2/ 4/ 3/ (d) u v w x y z Discovery time 20

Slide 21

Slide 21 text

1/ 2/ 4/ 3/ (e) u v w x y z B 1/ 2/ 4/5 3/ (f) u v w x y z B 1/ 2/ 4/5 3/6 (g) u v w x y z B 1/ 2/7 4/5 3/6 (h) u v w x y z B 21

Slide 22

Slide 22 text

1/ 2/7 4/5 3/6 (i) u v w x y z B F 1/8 2/7 4/5 3/6 (j) u v w x y z B F 1/8 2/7 9/ 4/5 3/6 (k) u v w x y z B F 1/8 2/7 9/ 4/5 3/6 (l) u v w x y z B F C 22

Slide 23

Slide 23 text

1/8 2/7 9/ 4/5 3/6 10/ (m) u v w x y z B F C 1/8 2/7 9/ 4/5 3/6 10/ (n) u v w x y z B F C B 1/8 2/7 9/ 4/5 3/6 10/11 (o) u v w x y z B F C B 1/8 2/7 9/12 4/5 3/6 10/11 (o) u v w x y z B F C B 23

Slide 24

Slide 24 text

Pseudocode: Uses a global timestamp time. DFS(V,E) for each u  V do color[u] ← WHITE time ← 0 for each u  V do if color[u] = WHITE then DFS - Visit(u) 24

Slide 25

Slide 25 text

DFS - Visit(u) color[u] ← GRAY  discover u time ← time +1 d[u] ← time for each v  Adj[u]  explore (u, v) do if color[v] = WHITE then DFS - Visit(v) color[u] ← BLACK time ← time +1 f[u] ← time  finish u 25

Slide 26

Slide 26 text

Example: 2 7 1 12 3 4 5 6 1415 9 10 13 16 8 11 T T T T B F C C C T C T C C d f Time = (V + E). • Similar to BFS analysis. • , not just O, since guaranteed to examine every vertex and edge. 26

Slide 27

Slide 27 text

DFS forms a depth-first forest comprised of  1 depth-first trees. Each tree is made of edges (u, v) such that u is gray and v is white when (u, v) is explored. 27

Slide 28

Slide 28 text

Theorem (Parenthesis theorem) [Proof omitted.] For all u, v, exactly one of the following holds: 1. d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u] and neither of u and v is a descendant of the other. 2. d[u] < d[v] < f[v] < f[u] and v is a descendant of u. 3. d[v] < d[u] < f[u] < f[v] and u is a descendant of v. So d[u] < d[v] < f[u] < f[v] cannot happen. Like parentheses: OK: ( ) [ ] ( [ ] ) [ ( ) ] Not OK: ( [ ) ] [ ( ] ) Corollary v is a proper descendant of u if and only if d[u] < d[v] < f[v] < f[u]. 28

Slide 29

Slide 29 text

3/6 2/9 1/10 4/5 7/8 12/13 (a) y z s x w v B C C 14/15 u 11/16 t F C C B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ( s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t) s z y w x t v u (b) 29

Slide 30

Slide 30 text

Theorem (White-path theorem) [Proof omitted.] v is a descendant of u if and only if at time d[u], there is a path u → v consisting of only white vertices. (Except for u, which was just colored gray.) Classification of edges Tree edge: in the depth-first forest. Found by exploring (u, v). Back edge: (u, v), where u is a descendant of v. Forward edge: (u, v), where v is a descendant of u, but not a tree edge. Cross edge: any other edge. Can go between vertices in the same depth-first tree or in different depth-first trees. 30

Slide 31

Slide 31 text

In an undirected graph, there may be some ambiguity since (u, v) and (v, u) are the same edge. Classify by the first type above that matches. Theorem [Proof omitted.] In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges. 31

Slide 32

Slide 32 text

Topological sort Directed acyclic graph (dag) A directed graph with no cycles. Good for modeling processes and structures that have a partial order: a > b and b > c a > c. But may have a and b such that neither a > b nor b > a. Can always make a total order (either a > b or b > a for all a ≠ b) from a partial order. In fact, that’s what a topological sort will do.  32

Slide 33

Slide 33 text

Example: dag of dependencies for putting on goalie equipment: socks shorts hose pants skates leg pads T-shirt chest pad sweater mask catch glove blocker batting glove 25/26 15/24 16/23 17/22 18/21 19/20 7/14 8/13 9/12 10/11 2/5 3/4 1/6 33

Slide 34

Slide 34 text

Topological sort 定義: A topological sort of a dag G=(V, E) is a linear ordering of all its vertices. (dag: Directed acyclic graph) 如 edge(u, v), u appears before v in the ordering undershirts socks pants shoes belt shirt tie jacket watch 11/16 12/15 6/7 1/8 2/5 3/4 17/18 13/14 9/10 socks undershirts pants shoes watch shirt belt tie jacket 34

Slide 35

Slide 35 text

Lemma A directed graph G is acyclic if and only if a DFS of G yields no back edges. Proof ( ): Show that back edge cycle. Suppose there is a back edge (u, v). Then v is ancestor of u in depth-first forest. v u T T T B Therefore, there is a path v → u, so v → u → v is a cycle.   35

Slide 36

Slide 36 text

( ) : Show that cycle back edge. Suppose G contains cycle c. At time d[v], vertices of c form a white path v → u (since v is the first vertex discovered in c). By white-path theorem, u is descendant of v in depth-first forest. Therefore, (u, v) is a back edge.   36

Slide 37

Slide 37 text

Topological sort of a dag: a linear ordering of vertices such that if (u, v)∈E, then u appears somewhere before v. (Not like sorting numbers.) TOPOLOGICAL-SORT(V, E) call DFS(V, E) to compute finishing times f[v] for all v  V output vertices in order of decreasing finish times 37

Slide 38

Slide 38 text

Don’t need to sort by finish times.  Can just output vertices as they’re finished and understand that we want the reverse of this list.  Or put them onto the front of a linked list as they’re finished. When done, the list contains vertices in topological sorted order. Time: (V+E) 38

Slide 39

Slide 39 text

Order: 26 socks 24 shorts 23 hose 22 pants 21 skates 20 leg pads 14 t-shirt 13 chest pad 12 sweater 11 mask 6 batting glove 5 catch glove 4 blocker 39

Slide 40

Slide 40 text

Correctness: Just need show if (u, v)  E, then f[v] < f[u]. When we explore (u, v), what are the colors of u and v? u is gray. Is v gray, too?  No, because then v would be ancestor of u. (u, v) is a back edge. contradiction of previous lemma (dag has no back edges).   40

Slide 41

Slide 41 text

 Is v white?  Then becomes descendant of u. By parenthesis theorem, d[u] < d[v] < f[v] < f[u].  Is v black?  Then v is already finished. Since we’re exploring (u, v), we have not yet finished u. Therefore, f[v] < f[u]. 41

Slide 42

Slide 42 text

Strongly connected components Given directed graph G = (V,E). A strongly connected component (SCC) of G is a maximal set of vertices C V such that for all u, v  C, both u → v and v → u. Example: [Just show SCC’s at first. Do DFS a little later.]  13/20 14/19 15/16 17/18 3/4 2/5 1/12 10/11 6/9 7/8 42

Slide 43

Slide 43 text

Algorithm uses GT = transpose of G. GT = (V, ET), ET = {(u, v) : (v, u)  E}. GT is G with all edges reversed. Can create GT in (V + E) time if using adjacency lists. Observation: G and GT have the same SCC’s. (u and v are reachable from each other in G if and only if reachable from each other in GT.) 43

Slide 44

Slide 44 text

Component graph GSCC = (VSCC, ESCC). VSCC has one vertex for each SCC in G. ESCC has an edge if there’s an edge between the corresponding SCC’s in G. For our example: 44

Slide 45

Slide 45 text

Lemma GSCC is a dag. More formally, let C and C’ be distinct SCC’s in G, let u, v  C, u’, v’  C’, and suppose there is a path u → u’ in G. Then there cannot also be a path v’→ v in G. Proof Suppose there is a path v’→ v in G. Then there are paths u → u’ → v’ and v’ → v → u in G. Therefore, u and v’ are reachable from each other, so they are not in separate SCC’s. 45

Slide 46

Slide 46 text

SCC(G) call DFS(G) to compute finishing times f[u] for all u compute GT call DFS(GT), but in the main loop, consider vertices in order of decreasing f[u] (as computed in first DFS) output the vertices in each tree of the depth-first forest formed in second DFS as a separate SCC 46

Slide 47

Slide 47 text

Example: 1. Do DFS 2. GT 3. DFS (roots blackened) Time: (V + E). 47

Slide 48

Slide 48 text

48

Slide 49

Slide 49 text

48

Slide 50

Slide 50 text

1/ 48

Slide 51

Slide 51 text

1/ 2/ 48

Slide 52

Slide 52 text

3/ 1/ 2/ 48

Slide 53

Slide 53 text

3/ 1/ 2/ B 48

Slide 54

Slide 54 text

3/ 1/ 2/ B 48 B

Slide 55

Slide 55 text

3/ 3/4 1/ 2/ B 48 B

Slide 56

Slide 56 text

3/ 3/4 1/ 2/ 2/5 B 48 B

Slide 57

Slide 57 text

3/ 3/4 1/ 2/ 2/5 6/ B 48 B

Slide 58

Slide 58 text

3/ 3/4 1/ 2/ 2/5 6/ 7/ B 48 B

Slide 59

Slide 59 text

3/ 3/4 1/ 2/ 2/5 6/ 7/ B 48 B

Slide 60

Slide 60 text

3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 B 48 B

Slide 61

Slide 61 text

3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 B 48 B

Slide 62

Slide 62 text

10/ 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 B 48 B

Slide 63

Slide 63 text

10/ 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 B 48 B C

Slide 64

Slide 64 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 B 48 B C

Slide 65

Slide 65 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 B F 48 B C

Slide 66

Slide 66 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 B F 48 B C

Slide 67

Slide 67 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ B F 48 B C

Slide 68

Slide 68 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ B F 48 B C

Slide 69

Slide 69 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ B F 48 B C

Slide 70

Slide 70 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ B B F 48 B C

Slide 71

Slide 71 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ B B F 48 B C C

Slide 72

Slide 72 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 B B F 48 B C C

Slide 73

Slide 73 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ B B F 48 B C C

Slide 74

Slide 74 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ B B B F 48 B C C

Slide 75

Slide 75 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 B B B F 48 B C C

Slide 76

Slide 76 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 B B B F 48 B C C

Slide 77

Slide 77 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 B C B B F 48 B C C

Slide 78

Slide 78 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 B C B B F 48 B C C

Slide 79

Slide 79 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 B C B B F 48 B C C

Slide 80

Slide 80 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 B C B B F 48 B C C

Slide 81

Slide 81 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ B C B B F 48 B C C

Slide 82

Slide 82 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ B C B B F 48 B C C

Slide 83

Slide 83 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ B C B B F 48 B C C

Slide 84

Slide 84 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 B C B B F 48 B C C

Slide 85

Slide 85 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 B C B B F 48 B C C

Slide 86

Slide 86 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ B C B B F 48 B C C

Slide 87

Slide 87 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 B C B B F 48 B C C

Slide 88

Slide 88 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 B C B B F 48 B C C

Slide 89

Slide 89 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 B C B B F 48 B C C

Slide 90

Slide 90 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ B C B B F 48 B C C

Slide 91

Slide 91 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ B C B B F 48 B C C

Slide 92

Slide 92 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ B C B B F 48 B C C

Slide 93

Slide 93 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 B C B B F 48 B C C

Slide 94

Slide 94 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 B C B B F 48 B C C

Slide 95

Slide 95 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 B C B B F 48 B C C

Slide 96

Slide 96 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 B C B B F 48 B C C

Slide 97

Slide 97 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ B C B B F 48 B C C

Slide 98

Slide 98 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 B C B B F 48 B C C

Slide 99

Slide 99 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 B C B B F 48 B C C

Slide 100

Slide 100 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 17/ B C B B F 48 B C C

Slide 101

Slide 101 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 17/ 18/ B C B B F 48 B C C

Slide 102

Slide 102 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 17/ 18/ 18/19 B C B B F 48 B C C

Slide 103

Slide 103 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 17/ 18/ 18/19 17/20 B C B B F 48 B C C

Slide 104

Slide 104 text

10/ 10/11 3/ 3/4 1/ 2/ 2/5 6/ 7/ 7/8 6/9 1/12 13/ 14/ 15/ 15/16 17/ 17/18 14/19 13/20 1/ 2/ 3/ 3/4 2/5 6/ 6/7 1/8 9/ 10/ 11/ 11/12 10/13 9/14 15/ 15/16 17/ 18/ 18/19 17/20 B C B B F 48 B C C

Slide 105

Slide 105 text

How can this possibly work? Idea: By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the component graph in topological sort order. To prove that it works, first deal with 2 notational issues: Will be discussing d[u] and f[u]. These always refer to first DFS. Extend notation for d and f to sets of vertices U V : d(U) = minuU {d[u]} (earliest discovery time) f(U) = maxuU {f[u]} (latest finishing time)  49

Slide 106

Slide 106 text

Lemma Let C and C’ be distinct SCC’s in G = (V, E). Suppose there is an edge (u, v)  E such that u  C and v  C’. u v C C’ Then f(C) > f(C’). 50

Slide 107

Slide 107 text

Proof Two cases, depending on which SCC had the first discovered vertex during first DFS. If d(C) < d(C’), let x be the first vertex discovered in C. At time d[x], all vertices in C and C’ are white. Thus, there exist paths of white vertices from x to all vertices in C and C’. By the white-path theorem, all vertices in C and C’ are descendants of x in depth-first tree. By the parenthesis theorem, f[x] = f(C) > f(C’). 51

Slide 108

Slide 108 text

If d(C) > d(C’), let y be the first vertex discovered in C’. At time d[y], all vertices in C’ are white and there is a white path from y to each vertex in C’ all vertices in C’ become descendants of y. Again, f[y] = f(C’). At time d[y], all vertices in C are white. By earlier lemma, since there is an edge (u, v), we cannot have a path from C’ to C. So no vertex in C is reachable from y. Therefore, at time f[y], all vertices in C are still white. Therefore, for all w  C, f[w] > f[y], which implies that f(C) > f(C’).  52

Slide 109

Slide 109 text

Corollary Let C and C’ be distinct SCC’s in G = (V, E). Suppose there is an edge (u, v)  ET, where u  C and v  C’. Then f(C) < f(C’). Proof (u, v)  ET (v, u)  E. Since SCC’s of G and GT are the same, f(C’) > f(C). Corollary Let C and C’ be distinct SCC’s in G = (V, E), and suppose that f(C) > f(C’). Then there cannot be an edge from C to C’ in GT. Proof It’s the contrapositive of the previous corollary.  53

Slide 110

Slide 110 text

Why the SCC procedure works:  When we do the second DFS, on GT, start with SCC C such that f(C) is maximum. • The second DFS starts from some x  C, and it visits all vertices in C. • Corollary says that since f(C) > f(C’) for all C’ ≠ C, there are no edges from C to C’ in GT. Therefore, DFS will visit only vertices in C. 54

Slide 111

Slide 111 text

 The next root chosen in the second DFS is in SCC C’ such that f(C’) is maximum over all SCC’s other than C. DFS visits all vertices in C’, but the only edges out of C’ go to C, which we’ve already visited.  Each time we choose a root for the second DFS, it can reach only vertices in its SCC—get tree edges to these, vertices in SCC’s already visited in second DFS—get no tree edges to these. We are visiting vertices of (GT)SCC in reverse of topologically sorted order. 55