Outlins: Graps Part-4 Applications o DFS Elmntary Grap Aloritms Topoloical Sort o Dirctd Acyclic Grap Stronly Connctd Componnts PART-4 1 2 Applications o Dpt-First Sarc Topoloical Sort: Usin dpt-irst sarc to prorm topoloical sort o a dirctd acyclic rap. Stronly Connctd Componnts: Dcomposin a dirctd rap into a stronly connctd componnts usin two dpt-irst sarcs. Dirctd Acyclic Grap (DAG) Ariss in many applications wr tr ar prcdnc or ordrin constraints (.. scdulin problms) I tr ar a sris o tasks to b prormd, and crtain tasks must prcd otr tasks In nral, a prcdnc constraint rap is a DAG, in wic vrtics ar tasks and d (u, v) mans tat task u must b compltd bor task v bins. 3 4 Input: Gnric scdulin problm St o tasks T 1, T 2, T 3,, T n Exampl: ttin drssd in t mornin: put on sos, socks, sirt, pants, blt, St o dpndncis T 1 T 2, T 3 T 4, T 5 T 1, Exampl: must put on socks bor sos, pants bor blt, Want: ordrin o tasks wic is consistnt wit dpndncis Problm rprsntation: Dirctd Acyclic Grap Vrtics = tasks; Dirctd Eds = dpndncis Acyclic: i cycl o dpndncis, no solution possibl Topoloical sort o a DAG: Topoloical Sort Linar ordrin o all vrtics in rap G suc tat i d (u, v) G tn vrtx u coms bor vrtx v I t rap is not acyclic, tn no linar ordrin is possibl Topoloical sort is usul in scdulin jobs wit prcdnc In nral, tr may b many lal topoloical ordrs or a ivn DAG. Ral-world xampl: ttin drssd 5 6 1
Gttin Drssd Topoloical Sort Aloritm Undrwar Socks Pants Sos Sirt Blt Ti Jackt A possibl scdul or drssin can b: Watc Topoloical-Sort(G) 1. call DFS(G) to comput inisin tim [v] or ac vrtx v. 2. as a vrtx is inisd, insrt it onto t ront o a linkd list. 3. rturn t linkd list o vrtics. socks sirts pants blt ti jackt sos watc 7 8 DFS Aloritm Gttin Drssd DFS(G) or ac vrtx u V[G] tim = 0; or ac vrtx u V[G] i (color[u] == WHITE) ; d[u] = tim; or ac v Adj[u] i (color[v] == WHITE) [u] = tim; Undrwar Pants Blt Sirt Ti Jackt Socks Sos Watc T iur blow sows ow topoloically sortd vrtics appar in rvrs ordr o tir inisin tims Socks Undrwar Pants Sos Watc Sirt Blt Ti Jackt 17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4 9 10 Runnin Tim Stronly Connctd Componnts (SCC) Tim: O(V+E) DFS aloritm is calld onc wit a cost O(V+E) plus O(1) tim to insrt ac o V vrtics onto t ront o t linkd list 11 Diraps ar otn usd to modl communication and transportation ntworks Popl want to know tat t ntworks ar complt in t sns tat rom any location it is possibl to rac anotr location in t dirap A dirctd rap is stronly connctd i, or vry pair o vrtics u and v tr is a pat rom u to v. Not tat in a dirctd rap, t xistnc o a pat rom u to v dos not imply tr is a pat rom v to u. 12 2
Stronly Connctd Componnts (SCC) For indin stronly connctd componnts o a rap G uss t transpos o G. Givn G = (V, E), t transpos o G is G T = (V, E T ) wr E T = (u, v) : (v, u) E G and G T av xactly t sam SCCs: u and v ar racabl rom ac otr in G i ty ar racabl rom ac otr in G T Stronly Connctd Componnts (SCC) Stronly-Connctd-Componnts(G) 1. call DFS(G) to comput inis tims [u] or ac vrtx u 2. comput G T 3. call DFS(G T ) and considr vrtics in ordr o dcrasin [u], computd in stp 1 4. output vrtics o ac tr in DFS(G T ) as sparat stronly connctd componnt Atr dcomposition, t aloritm is run sparatly on ac stronly connctd componnt. T solutions ar tn combind accordin to t structur o connctions btwn componnts. Total runnin tim Θ(V + E) Sinc t tim to build G T is O(V + E) plus O(V + E) to call DFS twic 13 14 Stronly Connctd Componnts (SCC) Stp-1 o SCC STRONGLY-CONNECTED-COMPONENTS (G) 1. initializ stack S to mpty, and call DFS(G) wit t ollowin modiication: pus vrtics onto stack S in t ordr ty inis tir DFS-VISIT calls. Tat is, at t nd o t procdur DFS-VISIT(u) add t statmnt PUSH(u, S) (tr is no nd to comput d[u] and [u] valus xplicitly). 2. construct t adjacncy-list structur o G T rom tat o G. 3. call DFS(G T ) wit t ollowin modiication: initiat DFS-roots in stack-s-ordr, i.., in t main DFS aloritm prorm t ollowin: wil S φ do u = POP(S) i color[u] = wit tn DFS-VISIT(u) nd wil 4. ac DFS-tr o stp 3 (plus all ds btwn its vrtics) orms an SCC. DFS(G) or ac vrtx u V[G] tim = 0; or ac vrtx u V[G] i (color[u] == WHITE) ; d[u] = tim; or ac v Adj[u] i (color[v] == WHITE) [u] = tim; PUSH(u, S); 15 16 Stp-3 o SCC Exampl: Grap G DFS(G T ) or ac vrtx u V[G] tim = 0; wil S φ do u = POP(S) i color[u] = wit tn DFS-VISIT(u) d[u] = tim; or ac v Adj[u] i (color[v] == WHITE) [u] = tim; 17 18 3
Stp 1 : DFS(G) Stp 2 : G T 19 20 Stp 3 : DFS(G T ) Stp 4 : SCC o G 21 22 Stronly Connctd Componnts Book Exampl: G a 1 18 d 14 17 15 16 b 2 13 5 12 6 11 i 7 10 8 9 c 3 4 b a c d a b c d 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 23 24 4
Book Exampl: G T Book Exampl: SCCs o G b a b c d a c d 13/14 11/16 12/15 3/4 1/10 8/9 2/7 5/6 ab cd 25 26 Componnt DAG T main ida coms rom t componnt rap G SCC = (V SCC, E SCC ) Suppos tat G as k SCC C 1, C 2,, C k V SCC = v 1, v 2,, v k : vrtx v i or ac C i (v i, v j ) E SCC i G contains a dirctd d (x, y) or som x C i and y C j Componnt DAG I w mr t vrtics in ac SCC into a sinl supr vrtx, and join two supr vrtics C i and C j i tr ar vrtics v i C i and v j C j suc tat (v i, v j ) E, tn t rsultin dirap, calld t componnt dirap, is ncssarily acyclic. 27 28 5