Roun 7: Grphs (prt I) Tommi Junttil Alto Univrsity Shool o Sin Dprtmnt o Computr Sin CS-A40 Dt Struturs n Alorithms Autumn 207 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 / 55
Topis: Grphs Rprsntin rphs Brth-irst srh Dpth-irst srh Topoloil sortin Stronly onnt omponnts Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55
Mtril in Introution to Alorithms, 3r. (onlin vi Alto li): Stions 22. 22.4 Extrnl links: MIT 6.006 OCW vio on rth-irst srh MIT 6.006 OCW vio on pth-irst srh n topoloil sort Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55
Dirnt kins o rphs n ntworks r vrywhr: th prossor o omputr is ntwork o loi ts intrnt soil ntworks mps pnny rphs (ours prrquisits t) t n t Grphs s mthmtil struturs r n importnt strtion or rsonin out suh systms Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55
A prt o th intrnt in 2005: (iur: Mtt Britt, Crtiv Commons-lins, sour) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55
Europn E ros: (iur: puli omin, sour) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 6 / 55
Mthmtil prliminris Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 7 / 55
Unirt Grphs Rp rom th roun on trs An unirt rph is pir (V,E), whr V is (usully init) st o vrtis (lso ll nos), n E is st o s twn th vrtis, i.. st o pirs o orm {u,v} suh tht u,v V n u v Exmpl Consir th rph (V,E) with th vrtis V = {,,,,,,,h}, n th s E = {{,},{,},{,},{,}, {,},{,},{,},{,},{,}}. It is illustrt rphilly on th riht with vrtis rwn s irls n s s lins twn th vrtis Sl-loops, i.. s twn vrtx n itsl, r orin in unirt rphs hr Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 8 / 55
A pth o lnth k rom vrtx v 0 to vrtx v k is squn (v 0,v,...,v k ) o k + vrtis suh tht {v i,v i+ } E or h i = 0,,...k A vrtx v is rhl rom vrtx u i thr is pth rom u to v. Not tht h vrtx is rhl rom itsl s thr is pth o lnth 0 rom th vrtx to itsl A pth is simpl i ll th vrtis in it r istint Th rph is onnt i vry vrtx in it is rhl rom ll th othr vrtis Exmpl Th rph on th riht hs (non-simpl) pth (,,,,,) rom to hs simpl pth (,,) o lnth 2 rom to is onnt ut ltin th {,} woul mk it isonnt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 9 / 55
A yl is pth (v 0,v,...,v k ) with k 3 n v 0 = v k Exmpl A yl (v 0,v,...,v k ) is simpl i v,v 2,...,v k r istint A rph is yli i it os not hv ny simpl yls Th rph on th lt is not yli s it ontins, mon mny othrs, simpl yl (,,,) Th rph on th riht is yli Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 0 / 55
Dirt Grphs In irt rphs th s hv irtion A irt rph (or irph), is pir (V,E), whr V is (usully init) st o vrtis, n E V V is st o s twn th vrtis: pir (u,v) E nots n rom th vrtx u to th vrtx v. Exmpl Consir th rph (V,E) with th vrtis V = {,,,,,,,h}, n th s E = {(,),(,),(,), (,),(,),(,),(,),(,),(,), (,),(,)}. It is illustrt rphilly on th riht with vrtis rwn s irls n s s lins with irtion rrows Noti th loop rom th vrtx to itsl n th two s twn th vrtis n Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 / 55
Th out-r o vrtx is th numr o s rom it whil th in-r is th numr o s to it A pth o lnht k rom vrtx u to th vrtx u is squn (v 0,v,...,v k ) o k + vrtis suh tht v 0 = u, v k = u n (v i,v i+ ) E or ll i = 0,,...,k A pth is simpl i ll vrtis in it istint A pth (v 0,v,...,v k ) is yl i k n v 0 = v k Exmpl In th rph on th riht, th in-r o is 2 n th out-r is som simpl pths r (), (,,), n (,,) th yls r (,), (,,,), (,,,), (,,) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55
Dirt Ayli Grphs (DAGs) A irt yli rph (DAG) is irt rph tht os not ontin ny yls Suh rphs ris in mny pplitions, s.. this wikipi p Exmpl DAGs ris nturlly in mny tim- n orr-rlt rs whr somthin hs to on or somthin ls n on wth pnts trousrs lt jkt ti unrshirt shirt soks shos In suh sitution yl woul mn tht som thins nnot on t ll As toy xmpl, pnny rph twn lothins or rssin up is shown on th riht ht Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55
Rprsnttions or rphs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55
In orr to rson out rphs with lorithms, on hs to rprsnt thm in mmory An unirt rph n hv t most n(n ) n irph t most n 2 2 s A rph is sprs i its numr o s is smll ompr to this mximum numr o possil s, othrwis it is ns Sprsity is not n xtly in onpt Th two ommon rprsnttion mthos r: jny mtris, whih r minly us or ns rphs, n jny lists, th most ommon rprsnttion In oth o ths rprsnttions, th vrtis r usully ssum to th irst intrs {0,,...,n } so tht inxin is sy I mor omplx vrtx nms suh s strins r to us, on n mintin on mp rom ths to smll intrs so tht intrs n us insi th rph lorithm Th xmpls low us symoli nms suh s or ttr humn rility Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55
Ajny mtris Th jny mtrix o irt rph G = ({0,,...,n },E) is n n inry mtrix A = ( u,v ) suh tht th ntry u,v is i (u,v) E n 0 othrwis Th mtrix is sy to stor in row-y-row it-vtor o lnth n 2 (n rry with n2 32 32-it intrs); th ntry u,v n oun t th inx un + v in th it-vtor Exmpl: A irt rph n its jny mtrix rprsnttion 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 As it-vtor th mtrix is 00000000000000000000000000000000000000 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 6 / 55
Th jny mtrix o n unirt rph G = ({0,,...,n },E) is simply th uppr riht hl o th mtrix With 0 u < v < n, th ntry u,v is i {u,v} E n 0 othrwis I th mtrix is prsnt s row-y-row it-vtor, thn th ntry u,v n oun in th it un u(u+) 2 + (v u ) Exmpl: An unirt rph n its jny mtrix rprsnttion 0 0 0 0 0 0 0 0 0 0 0 0 As it vtor th mtrix is 000000000000 n th it or th {,} n oun t th inx 2 7 2(2+) + (6 2 ) = 4 3 + 3 = 4 unr th ) 2 mppin 0 2 3 4 5 6 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 7 / 55
Som nlysis: Goo or ns (n smllish) rps Ain or rmovin n tks onstnt tim Ain nw vrtx is xpnsiv Grphs with mny vrtis tk lot o mmory: th jny mtrix or irt rph with million vrtis woul tk ovr 00 iyts o mmory Trvrsin th nihours o vrtx not optiml i th r o th vrtx is low ompr to th numr n o ll vrtis (hv to o throuh ll th O(n) yts mkin th row) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 8 / 55
Ajny lists Eh vrtx is ssoit to list o its nihours Nturlly, n rry or ynmi rry n us inst o th list s wll Exmpl: A irt rph n its jny list rprsnttion Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 9 / 55
Exmpl: A irt rph n its jny list rprsnttion in mor ompt orm Th vrtis r now {0,,...,6} Th nihours o vrtx u n oun in th surry s[strt[u],strt[u+]-] or s[strt[u],s.lnth-] or th lst vrtx u = 6 4 2 6 strt 0 0 2 2 4 3 7 4 8 5 9 6 0 0 3 s 0 4 2 3 5 4 0 5 3 6 6 7 6 8 5 9 0 0 3 5 In th rprsnttion mtho ov, th two rrys ontin V + E intrs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 20 / 55
Anlysis: Compt or sprs rphs: mmory us is O( V + E ) inst o O( V 2 ) Ain nw vrtx is n mortiz onstnt-tim oprtion whn ynmi rrys r us Ain n rmovin s linr in th r o th vrtis involv in th worst s whn th list/rry pr vrtx rprsnttion o th irst xmpl is us Itrtin th nihours o vrtx is sy Mor ommonly us s rphs in mny pplitions tn to oth lr n sprs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 2 / 55
Rprsntin vrtx ttriuts In mny rph lorithms, w n to ssoit som t ttriuts to vrtis Thr r multipl wys to hiv this ut s/whn th vrtis r {0,,...,n }, th sist wy is to hv n rry o n lmnts holin th ttriut(s) From now on, w ssum tht ssin n sttin vrtx ttriut n on in onstnt tim Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 22 / 55
Brth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 23 / 55
An iint trvrsl lorithm tht ins, or th ivn sour vrtx s, ll th vrtis rhl rom s, n shortst simpl pth rom s to h rhl vrtx Th i is to strt rom th sour vrtx s n trvrs its outoin s. Th nihour vrtis tht hv not yt n visit r put in irst-in-irst-out quu. Eh vrtx is tkn out o th quu n its nihours r thn visit in th sm mnnr n put t th n o th quu i not yt visit. Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 24 / 55
At h point, vrtx is olor whit i it hs not yt n oun rhl rom s ry i it hs n oun rhl rom s ut not ll its outoin s hv n trvrs lk i it hs n oun rhl rom s n ll its outoin s hv n trvrs In ition, h vrtx t is ssoit with istn ttriut tht tlls th lnth o th shortst pth rom s to t, n prssor ttriut tht stors th nihour rom whih shortst pth th sour to it ws prou Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 25 / 55
In psuoo Brth-irst-srh(G, s): or h vrtx v V \ {s}: // Initiliz olors, istns n prssors v.olor WHITE v.ist v.pr NIL Q /0 // Q is FIFO-quu s.olor GRAY // Initiliz th sour vrtx s to ry s.ist 0 s.pr NIL nquu(q, s) whil Q is not mpty: u quu(q) // Tk th rlist ry vrtx rom th quu or h v with {u,v} E: // Trvrs h rom it i v.olor = WHITE: // Th nihour v hs not yt n visit? v.olor GRAY // Color it ry v.ist u.ist + // Stor th istn v.pr u // An th pth rom th sour nquu(q, v) // Put it t th n o th quu u.olor := BLACK // All s rom u trvrs Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 26 / 55
Exmpl Brth-irst srh on rph with two onnt omponnts. Th sour vrtx is Eh nnott rph low shows th olors, istns, n BFS-tr s in th innin o h whil loop n t th n o th lorithm (th lst rph). Th nihours r in th quu in n ritrry orr. 2 2 0 0 0 0 2 2 2 3 3 0 0 0 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 27 / 55
For irt rphs on just rpls th lin or h v with {u,v} E with or h v with (u,v) E Exmpl Brth-irst srh on irt rph Th nihours r in th quu in n ritrry orr. 2 0 0 0 0 2 2 2 3 3 0 0 0 Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 28 / 55
At th n o th lorithm, vrtx is lk i it is rhl rom th sour vrtx is whit i it is not rhl rom th sour Furthrmor, or h lk vrtx th istn ttriut ivs th lnth o th shortst pth(s) rom th sour to th vrtx ollowin th prssor s prous shortst pth to th sour vrtx Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 29 / 55
Run-tim nlysis in th worst s: Eh vrtx is in th quu on Eh is onsir twi or unirt rphs; on or h n-point whn it turns rom ry to lk on or irt rphs whn th sour n-point turns rom ry to lk Enquu n quu r onstnt tim oprtions th lorithm runs in tim O( V + E ) On unirt rphs, inin th onnt omponnts is sy with BFS Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 30 / 55
Dpth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 3 / 55
Anothr systmi mtho or trvrsin rphs Fins ll th vrtis tht r rhl rom vrtx Dos not in th shortst pths or smllst istn rom sour vrtx But rvls som strutur o th rph tht n us in othr lorithm Th i (si vrsion irst): Strt visitin vrtis rom th sour vrtx Rursivly visit th whit nihours o th urrnt vrtx Whn strtin th visit, olor th vrtx ry Whn ll nihours hv n visit, olor th vrtx lk Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 32 / 55
In psuo-o Dpth-irst-srh(G, s): or h vrtx v V : v.olor WHITE v.pr NIL visit(u): u.olor GRAY or h v with {u,v} E: i v.olor = WHITE: v.pr u visit(v) u.olor BLACK visit(s) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 33 / 55
Exmpl Dpth-irst srh on n unirt rph Th iurs low show th sitution t th innin o h ll to visit (th visit no is shown in lu) n t th n o th lorithm (th lst iur) Th nihours r in th quu in n ritrry orr Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 34 / 55
Ain, or irt rphs on just rpls th lin or h v with {u,v} E with or h v with (u,v) E Exmpl Dpth-irst srh on irt rph Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 35 / 55
Worst s run-tim nlysis: Eh vrtx is visit on (it must whit to visit n immitly turns ry whn visit) Whil vrtx is visit, h rom it is trvrs th lorithm runs in tim O( V + E ) Th (rvrs) prssor links iv pth, not nssrily shortst on, rom th sour vrtx to h vrtx rhl rom th sour Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 36 / 55
An xtn vrsion Lt us xtn th si vrsion y tthin to h vrtx two visit timstmp ttriuts: th strt timstmp tlls th tim on whih visitin th vrtx strt (th vrtx ws olor to ry), n th inish timstmp tlls th tim whn visitin th vrtx ws omplt (th vrtx ws olor to lk) Th timstmps r snin intrs strtin rom In ition to th timstmps, th lorithm is xtn so tht thr is no sinl sour vrtx or th srh ut nw sour vrtis r tkn (ritrrily or with som strty) or susrhs until ll th vrtis hv n visit Th lorithm now os not in th vrtis rhl rom sinl sour vrtx ut rvls som strutur o th rph Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 37 / 55
In psuo-o Dpth-irst-srh(G = (V, E)): or h vrtx v V : v.olor WHITE v.pr NIL tim 0 visit(u): // Innr rursiv untion or trvrsin vrtis tim tim + u.strt = tim u.olor GRAY or h v with {u,v} E: // (u,v) E or irt rphs i v.olor = WHITE: v.pr u visit(v) u.olor BLACK tim tim + u.inish = tim or h vrtx u V : i u.olor = WHITE: visit(u) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 38 / 55
Exmpl Consir th rph on th riht Th susrhs (lls to th visit-untion in th lst lin o th min prour) strt rom th vrtis in lphtil orr hr Th s rom th vrtis r trvrs in n ritrry orr Th s rom th prssor vrtis r hihliht in lu A irt rph: 3/6 2/9 /0 /6 y z s t x w v u 4/5 7/8 2/3 4/5 Th sm rwn irntly: /0 /6 s t Th pir / nxt to vrtx tlls th strt n inish timstmp o th vrtx 2/9 z v 2/3 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 39 / 55
Th s omin rom th prssor vrtis orm pth-irst orst Formlly, it is th su-rph G pr = (V,E pr ), whr E pr = {(u.pr,u) u V n u.pr NIL} Th pth-irst orst n onsist o svrl pth-irst trs root t th vrtx irst visit in h susrh Th pth-irst orst is not nssrily uniqu or th rph ut pns on trvrsl orr o th pth-irst srh Consir th tim intrvl [u.strt, u.inish] orm y th timstmps ssoit to h vrtx For th intrvls o two isjoint vrtis u n v on o th ollowin hols: [u.strt,u.inish] n [v.strt,v.inish] o not intrst h othr [u.strt,u.inish] is ully ontin in [v.strt,v.inish] [v.strt, v.inish] is ully ontin in [u.strt, u.inish] Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 40 / 55
Exmpl Consir th rph on th riht Th s in th pth-irst orst r hihliht in lu A irt rph: 3/6 2/9 y z /0 /6 s t Th vrtis z, y, x n w n rh rom s in th pth-irst orst n thir tim intrvls r ully ontin in th intrvl o s Th intrvls o th vrtis t, v n u lonin to irnt pth-irst tr o not ovrlp with th intrvl o s x w v 4/5 7/8 2/3 Th sm rwn in irnt wy: /0 /6 s t 2/9 z v 2/3 u 4/5 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 4 / 55
Assum tht pth-irst srh hs n prorm in irt rph Th s o th rph n lssii orin to th srh s ollows: Tr s r th s lonin to th pth-irst orst Bk s r th s tht l rom vrtx to som nstor vrtx in th sm pth-irst tr (this inlus loop-s) Forwr s r th s tht l rom vrtx to som propr snnt vrtx in th sm pth-irst tr Cross s r th rst o th s For unirt rphs k n orwr s r usully onsir to th sm thin, n thr r no ross s Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 42 / 55
Exmpl Consir th irt rph n pth-irst srh shown on th riht A irt rph: 3/6 2/9 y z /0 /6 s t Th tr s r hihliht in lu Th k s r: (x,z) n (u,t) Th orwr s r: (s,w) x w v 4/5 7/8 2/3 Th sm rwn in irnt wy: /0 /6 s t u 4/5 Th ross s r: (u,v), (v,s), (v,w) n (w,x) 2/9 z v 2/3 u 4/5 3/6 y w 7/8 4/5 x Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 43 / 55
Som o th typs r sy to tt urin th pth-irst srh I th lorithm is in th untion invotion visit(u) n onsirs th (u,v) E, thn tht is tr i v is whit k i v is ry, n ithr orwr or ross i v is lk A irt rph is yli i n only i (ny) pth-irst orst os not ontin k s th yliity o irt rph n tt in tim O( V + E ) Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 44 / 55
Topoloil sort Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 45 / 55
A topoloil sort, or topoloil orrin, or irt yli rph (V,E) is linr orrin o its vrtis V suh tht or h (u,v) E it hols tht u pprs or v in th orrin Nturlly, irt rphs with yls o not hv suh linr orrins n nnot topoloilly sort Exmpl Rll th DAG rom prvious xmpl shown on th riht pnts trousrs ti unrshirt shirt ht Two topoloil sorts or th DAG r: wth lt soks jkt shos pnts,unrshirt,trousrs,shirt,lt,ti,soks,shos,wth,jkt,ht 2 wth,pnts,soks,trousrs,shos,lt,unrshirt,ht,shirt,ti,jkt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 46 / 55
A topoloil sort n otin sily with pth-irst srh: Prorm pth-irst srh on th rph ut whn vrtx is inish (olor lk), it in th ront o link list t th n, rturn th list Th ky i or th orrtnss o this lorithm is: in DAGs, whn vrtx is olor lk, ll th vrtis rhl rom it r lk n thus ltr in th prou topoloil orrin In DAGs suh rhl vrtis nnot ry us tht woul mn tht th rph hs yl Topoloil sort n on in tim O( V + E ) s th lorithm ov is simpl vrint o DFS Othr lorithms lso xists, s.. this wikipi p Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 47 / 55
Exmpl Rll th DAG rom prvious xmpl shown on th riht Th timstmps prou y n ritrry DFS on it r lso shown [9,20] pnts unrshirt [,0] [,6] trousrs [5,6] ti shirt [2,7] ht [8,9] wth [2,3] lt soks [7,8] [2,22] jkt [3,4] shos [4,5] Th topoloil sort prou y th DFS is wth,pnts,soks,trousrs,shos,lt,unrshirt,ht,shirt,ti,jkt Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 48 / 55
Stronly Connt Componnts Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 49 / 55
Finin stronly onnt omponnts o irt rphs is n nothr lorithm s on pth-irst srh For irt rph G = (V,E), stronly onnt omponnt (SCC) is mximl sust C V suh tht or ll vrtis u,v C it hols tht v is rhl rom u n u is rhl rom v Exmpl Thus h yl o th irt rph is insi n SCC Vrtis tht r not in ny yl orm thir own unit omponnts Consir th rph on th riht y z s t Its stronly onnt omponnts r {s}, {t,u}, {v}, n {w,x,y,z} x w v u Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 50 / 55
Th omponnt rph G SCC = (V SCC,E SCC ) o irt rph G is th irt rph whr th vrtis V SCC r th onnt omponnts o G, n thr is n (C,C ) V SCC rom omponnt vrtx C to irnt omponnt vrtx C i (u,v) is n in G or som u C n v C Th omponnt rph is yli. Why? Exmpl Consir in th rph in th prvious xmpl Its omponnt rph is shown on th riht {w, x, y, z} {s} {v} {t, u} Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 5 / 55
Computin SCCs Thr r svrl linr, i. O( V + E ), tim lorithms or omputin th SCCs o rph G = (V,E), s.. wikipi rtil Th ollowin prsnts vrint o Kosrju s lorithm It uss pth-irst srh twi: Prorm pth-irst srh on th oriinl rph to otin th visit timstmps 2 Mk th trnspos G T o th rph G = (V,E) y rvrsin th irtions o th s: G T = (V,E T ), whr E T = {(u,v) (v,u) E} 3 Prorm pth-irst srh on th trnspos rph G T ut strt th susrhs rom th vrtis in th rvrs orr o th inish timstmps prou y th irst pth-irst srh Th SCCs r th nos in th pth-irst srh orsts prou y th son pth-irst srh Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 52 / 55
Consir th rph on th riht Its stronly onnt omponnts r {s}, {t,u}, {v}, n {w,x,y,z} 3/6 2/9 y z /0 /6 s t Also shown r th timstmps n tr s otin y prormin pth-irst srh on th rph x 4/5 w 7/8 v 2/3 u 4/5 Th trnspos o th rph is shown on th riht y z s t Noti tht th SCCs r th sm An th omponnt rph is in ut trnspos x w v u Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 53 / 55
Th rph(s) on th riht show th rsult o pplyin pth-irst srh on th trnspos rph in th rvrs orr o th inish timstmps o th irst DFS Th susrh strtin rom t ins th l omponnt {t, u} Th susrh rom v isovrs th unit omponnt {v} Th susrh rom s isovrs th unit omponnt {s} Th susrh rom z isovrs th root omponnt {w,x,y,z} DFS on th trnspos rph /2 9/6 7/8 /4 y z s t x w v u 3/4 0/5 5/6 2/3 Th sm rwn in irnt orr: /4 5/6 7/8 9/6 t v s z u x 2/3 3/4 y /2 0/5 w Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 54 / 55
Why os this lorithm work? Rouh skth (s th ook or th proos): Th irst pth-irst srh ivs, in th inish timstmps, rvrs topoloil sortin or th SCCs in th omponnt rph Th omponnt rph o th trnspos rph is th trnspos o th omponnt rph o th oriinl rph Prormin th son DFS on th trnspos rph in th rvrs topoloil orr ivn y th irst DFS irst ins th omponnts t hiht 0 in th trnspos omponnt rph thn ins th omponnts t hiht in th trnspos omponnt rph (it os not o to th omponnts t hiht 0 s thy r lry visit) n so on Tommi Junttil (Alto Univrsity) Roun 7 CS-A40 / Autumn 207 55 / 55