Strongly Connctd Componnts Lt G = (V, E) b a dirctd graph Writ if thr is a path from to in G Writ if and is an quivalnc rlation: implis and implis s quivalnc classs ar calld th strongly connctd componnts (SCC s) of G For V, C( ) := s SCC 1
a b c d a b c d f g h f g h a b c d a b c d f g h f g h 2
Th componnt graph Ida: collaps ach SCC s into a singl nod Formally: componnt graph G scc = (V scc, E scc ) V scc = th SCC s C 1,..., C k of G E scc = {(C, C j ) : = j, (, ) E for som C, C j } G : G scc : a b c d a b c d f g h f g h 3
Lmma 1. in G C( ) C( ) in G scc 4
Lmma 2. G scc is acyclic. Suppos thr is a cycl. By dfinition, no slf loops in G scc, so th cycl must contain two distinct nods, say C( ) and C( ) Thn w hav C( ) C( ) and C( ) C( ) in G scc By Lmma 1, and in G Thus, C( ) = C( ) QED 5
An application Considr th gathring coins problm for a gnral dirctd graph Givn a dirctd graph G = (V, E) On ach nod thr ar N[ ] coins Goal: find th max numbr of coins that can b gathrd on any on path through G Th path nd not b simpl, but onc you pick up th coins on a nod, thy ar gon 6
W alrady know how to solv this for a DAG For a gnral graph, w start by computing G scc For ach SCC C, w assign to it C N[ ] coins Now run th DAG algorithm on G scc Exampl: G : G scc : 1 1 4 1 4 1 1 1 2 1 2 2 2 1 2 2 Gnral principl: Try to rduc qustions about graphs to qustions about DAG s 7
Anothr application Problm: A graph G = (V, E) is calld smi-connctd if for all, V, or. Show how to tst if G is smi-connctd 8
First considr th problm for DAG s Lt 1,..., n b a topological sort of G Claim: G is smi-connctd thr is an dg +1 for ach = 1.. n 1 1 2 3 4 5 6 7 9
Now considr a gnral graph Claim: G is smi-connctd G scc is smi-connctd (follows dirctly from Lmma 1) Algorithm: 1. Run algorithm SCC to gt G scc (which is a DAG) 2. Tst if G scc is smi-connctd (as abov) 10
Computing SCC s: th Kosaraju/Sharir Algorithm Th ida G : G scc : a b c d a b c d f g h G scc : f g h a b c f g h d a b G scc : c d Somhow (by magic) find a nod in a sink componnt and prform DFS from thr G scc : f g h G scc : a b c d a b c d f g h f g h 11
Computing SCC s: th Kosaraju/Sharir Algorithm For a graph G, lt G T dnot its transpos or rvrs sam as G but with all dgs rvrsd G and G T hav th sam SCC s in fact, (G T ) scc = (G scc ) T Algorithm SCC(G): 1. call DFS(G), and ordr th nods 1,..., n in ordr of dcrasing finishing tim (as in DFSTopSort) 2. comput G T 3. call DFS(G T ) but in th top-lvl loop, procss in th ordr 1,..., n th trs in th DFS forst ar th SCC s of G Running tim: O( V + E ) 12
Exampl: G : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 G T : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 G T : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 G T : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 G T : 13/14 11/16 1/10 8/9 G T : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 12/15 3/4 2/7 5/6 G scc : 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 13
Notation: lt ƒ [ ] b th finish tim in th first DFS, and lt ƒ (U) := m x{ƒ [ ] : U} Lmma 3. Suppos (C, C ) E scc. Thn ƒ (C) > ƒ (C ) Proof. In th first DFS, lt b th first nod discovrd in C C Cas 1: C C C By th Whit Path Thorm, all nods in C C ar dscndnts of in th DFS forst By th Parnthsis Thorm, ƒ [ ] = ƒ (C) > ƒ (C ) 14
Cas 2: C C C By th Whit Path Thorm, all nods in C ar dscndnts of in th DFS forst By Lmma 2, thr is no path from C to C in G scc, and so no nod in C is rachabl from so at tim ƒ [ ], all nods in C ar still whit ƒ (C) > ƒ [ ] = ƒ (C ). QED 15
Thorm. Algorithm SCC is corrct. Proof. Lt T 1,..., T l b th trs of th DFS forst cratd in stp 3 Lt C 1,..., C k b th SCC s, with ƒ (C ) > ƒ (C +1 ) G scc : C 1 C 2 C3 C 4 (G scc ) T : C 1 C 2 C3 C 4 16
(G scc ) T : C 1 C 2 C3 C 4 At stp 3, w start with a vrtx 1 in C 1 By Whit Path Thorm, all nods in C 1 will b in T 1 By Lmma 3, in G T, thr ar no dgs laving C 1 th nods of C 1 ar xactly th nods of T 1 17
(G scc ) T : C 1 C 2 C3 C 4 Nxt, w pick a nod in C 2, and at this tim, all nods in C 1 ar black, and all nods in C 2,..., C k ar whit By Whit Path Thorm, T 2 contains all nods in C 2, and by Lmma 3, T 2 contains no othr nods th nods of C 2 ar xactly th nods of T 2 Procding by induction, w gt T = C for = 1,..., l, and so k = l. QED 18
Rprsntation of G scc Lt C 1,..., C k b th SCC s Numbr th nods 1.. k Standard adjacncy list rprsntation of G scc Also: An array mapping V to j {1,..., k}, whr C j An array mapping j {1..., k} to a list rprsntation of C j This can all b don in tim O( V + E ), and w may assum that C 1,..., C k ar alrady in topological ordr in fact Algorithm SCC outputs C 1,..., C k in topological ordr 19
Connctivity in undirctd graphs Suppos G is undirctd (, ) E (, ) E SCC s ar just calld connctd componnts Th componnt graph consists of isolatd nods no dgs btwn componnts Easy to comput: th trs in th DFS forst ar th connctd componnts 20