Sintifi Progrmming Grphs Alrto Montrsor Univrsità i Trnto 08//07 This work is lins unr Crtiv Commons Attriution-ShrAlik 4.0 Intrntionl Lins.
Tl of ontnts Introution Exmpls Dfinitions Spifition Rprsnttions Grph trvrsls 3 BFS Shortst pths 4 DFS Connt omponnts Unirt yli grphs Eg lssifition Dirt yli grphs Topologil Sort Strongly onnt omponnts
Introution Exmpls Exmpls Esmpi i grfi Alrto Montrsor Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Exmpls Grph prolms Trvrsls Brth-first srhs (Shortst pth, istn) Dpth-first srhs (onnt omponnt, yl ttion, topologil sort) Wight grphs Shortst pth Minimum spnning trs Mximum flow Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Exmpls Exmpls of grph prolms A hug numr of prolms n onvrt in grph prolm W will stuy prolms in strt form. Thir pplition n foun in th most ivrs rs. Alrto Montrsor (UniTN) SP - Grphs 08//07 3 / 96
Introution Exmpls Exmpls in Soil Sins Whn you srh for somon in LinkIn, you gt "onntion istn": for xmpl, th lngth of th shortst pth twn m n Bill Gts is 3 (in th LinkIn soil grph) Cntrlitis (losnss, twns, ignvtor, pgrnk, t) r us to msur how "ntrl" nos r in soil grph It hs n show tht usinssmn who mintin mny struturl hols h signifintly highr rt of suss in omptitiv mrktpl 3 https://www.linkin.om/hlp/linkin/suggst/0/your-ntwork-n-grs-of-onntion?lng=n https://mrig-intllign.om/kylins-fqs-soil-ntwork-nlysis/ 3 Burt, Ronl. Struturl Hols: Th Strutur of Comptition. Hrvr Univrsity Prss, 99 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
Introution Exmpls Exmpls in Computtionl Biology Gnom ssmly. Grph thory is us in gnrtions of ssmly softwr, in th form of ovrlp grphs n Brujin grphs. Stuy of gnom rrrngmnts. Sorting th most prsimonious gnom rrrngmnt snrio orrspons to th rkpoint grph prolm. Strutur prition of RNAs n protins. Ajny of rsius in RNA or protin struturs n sri s ontt grphs. In ition, grph omposition is us to intify moulr omponnts or motifs (for xmpl, psuo-knot). Grph-s lustring. Us to fin motifs in protin-protin intrtion ntworks or protin fmilis in squn similrity grphs. Grph lyout lgorithms n us to visuliz lrg-sl mtoli pthwys or phylognti trs/ntworks. Alrto Montrsor (UniTN) SP - Grphs 08//07 5 / 96
Introution Exmpls Applition xmpl Wtson Holms ingno sull mort l u MPollok Wtson: Ci sono novità, Holms: pr h il tstmnto, nto istrutto nll splosion, foss stto fvorvol un ll stt mih l u. Holms: Ciò h è più strno, è h l om si stt frit ppositmnt pr ssr nsost nll rmtur ll mr ltto, il h f supporr h l ssssino i nssrimnt ftto più i un visit l stllo. Wtson: Ho intrrogto prsonlmnt l stt onn, m isun h giurto i ssr stt nl stllo un sol volt nll su vit. Dgli intrrogtori risult h: Ann h inontrto Btty, Chrlott, Flii Gorgi; Btty h inontrto Ann, Chrlott, Eith, Flii Hln; Chrlott h inontrto Ann, Btty Eith; Eith h inontrto Btty, Chrlott, Flii; Flii h inontrto Ann, Btty, Eith, Hln; Gorgi h inontrto Ann Hln; Hln h inontrto Btty, Flii Gorgi. Vt, Holms, h l tstimoninz onorno. M hi srà l ssssino? Holms: Elmntr, mio ro Wtson: iò h mi vt tto iniviu inquivoilmnt l ssssino! Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96
Introution Exmpls Applition xmpl f h g Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Introution Exmpls Applition xmpl f h g h g h g h g Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Introution Dfinitions Dirt n unirt grphs: finitions Dirt grph G = (V, E) V is st of vrtxs/nos E is st of gs, i.. orr pirs (u, v) of nos V = {,,,,,f } E = { (,),(,),(,),(,) (,),(,),(,) } Unirt grph G = (V, E) V is st of vrtxs/nos E is st of gs, i.. unorr pirs [u, v] of nos V = {,,,,,f } E = { [,],[,],[,], [,],[,],[,] } f f Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96
Introution Dfinitions Trminology Vrtx v is jnt to u if n only if (u, v) E. In n unirt grph, th jny rltion is symmtri An g (u, v) is si to inint from u to v f (, ) is inint from to (, ) is inint from to (, ) is inint from to is jnt to is jnt to is jnt to Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96
Introution Dfinitions Grph siz Dfinitions n = V : numr of nos m = E : numr of gs Rltionships twn n n m In n unirt grph, m n(n ) = O(n ) In irt grph, m n n = O(n ) Complxity of grph lgorithms Th omputtionl omplxity is msur s on oth n n m (.g. O(n + m)) Alrto Montrsor (UniTN) SP - Grphs 08//07 0 / 96
Introution Dfinitions Som spil ss A grph with n g twn ll pirs of nos is omplt Informlly (thr is no grmnt on th finitions) A grph with "fw" gs is si to sprs;.g., grphs with m = O(n), m = O(n log n) A grph with "svrl" gs is si to ns;.g. m = Ω(n ) Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Dfinitions Som spil ss An unroot tr is onnt grph with m = n A root tr is onnt grph with m = n in whih on no is signt s th root. A st of trs is ll forst f root f g h i Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Dfinitions Dfinition: Dgr Unirt grphs Th gr of no is th numr of gs inint on it. Dirt gs Th in-gr (out-gr) of no is th numr of gs inint to (from) it. 3 3 0 f in out in out in out 3 in 3 out 0 in out f in 0 out 0 Alrto Montrsor (UniTN) SP - Grphs 08//07 3 / 96
Introution Dfinitions Dfinition: Pth Pth In grph G = (V, E), pth C of lngth k is squn of nos u 0, u,..., u k suh tht (u i, u i+ E) for 0 i k. Exmpl:,,,, is pth of lngth 4 It is lso th shortst pth twn n Not: pth is si to simpl if ll its nos r istint Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
Introution Dfinitions Dfinition: Pth Pth In grph G = (V, E), pth C of lngth k is squn of nos u 0, u,..., u k suh tht (u i, u i+ E) for 0 i k. Exmpl:,,,, is pth of lngth 4 Not: pth is si to simpl if ll its nos r istint Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
Introution Spifition Spifition Dynmi grphs In th most gnrl s, grphs r ynmi t struturs in whih nos n gs n /rmov Grph Grph( ) St siz() St V() St j(no u) insrtno(no u) insrteg(no u, No v) ltno(no u) lteg(no u, No v) % Crt nw grph % Rturns th numr of nos % Rturns th st of ll nos % Rturns th st of nos jnt to u % A no u to th grph % A g (u, v) to th grph % Rmovs no u from th grph % Rmovs g (u, v) from th grph Alrto Montrsor (UniTN) SP - Grphs 08//07 5 / 96
Introution Spifition Simplifi spifition without rmovls In som ss, w onsir only th possiility of insrtions Th grph is lo t th ginning, n thn nvr hng This simplifis th unrlying implmnttion Grph Grph( ) St siz() St V() St j(no u) insrtno(no u) insrteg(no u, No v) % Crt nw grph % Rturns th numr of nos % Rturns th st of ll nos % Rturns th st of nos jnt to u % A no u to th grph % A g (u, v) to th grph Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96
Introution Rprsnttions Rprsnttions Two possil "lssi" implmnttions Ajny mtrix Ajny lists Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Introution Rprsnttions Ajny mtrix: irt grphs m uv = { (u, v) E 0 (u, v) E Sp = n its 0 3 4 5 0 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 4 0 0 0 0 0 5 0 0 0 0 0 0 Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96
Introution Rprsnttions Ajny list: irt grph G.j(u) = {v (u, v) E} Sp = n + m its 0 0 3 3 3 4 5 3 4 0 4 5 Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96
Introution Rprsnttions Ajny mtrix: unirt grphs m uv = { (u, v) E 0 (u, v) E Sp = n or n(n )/ 0 3 4 5 0 3 4 5 0 0 0 0 0 0 0 0 3 0 4 0 5 Alrto Montrsor (UniTN) SP - Grphs 08//07 0 / 96
Introution Rprsnttions Ajny list: unirt grph G.j(u) = {v (u, v) E} Sp = n + m 0 0 0 3 3 4 3 0 4 4 3 3 4 5 5 Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Rprsnttions Ajny mtrix: wight grphs Wight grphs Egs my ssoit with wight (ost, profit, t.) Th wight is ssoit through ost funtion w : V V R If thr is no g twn two vrtis u, v, w(u, v) = + 0 3 4 5 0 0 3 0 0 0 3 4 4 0 0 0 4 7 0 7 3 8 0 4 4 0 5 3 4 5 8 Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96
Introution Rprsnttions Ajny lists possil vritions Both th onpts of jny list n th onpt of no list n implmnt in svrl wys: Strutur Jv C++ Python Link list LinkList list Stti vtor [] [] [] Dynmi vtor ArryList vtor list St HshSt st st TrSt Ditionry HshMp TrMp mp it Alrto Montrsor (UniTN) SP - Grphs 08//07 3 / 96
Introution Rprsnttions An implmnttion in Python - Wight grph lss Grph: f init (slf): slf.nos = { } f siz(slf): rturn ln(slf.nos) f V(slf): rturn slf.nos.kys() f j(slf, u): if u in slf.nos: rturn slf.nos[u] f insrtno(slf,u): if u not in slf.nos: slf.nos[u] = { } f insrteg(slf, u, v, w=0): slf.insrtno(u) slf.insrtno(v) slf.nos[u][v] = w Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
Introution Rprsnttions An implmnttion in Python - Wight grph G = Grph() for u,v in [ (, ), (, ), (, ), (, ), (, ), (, ), (, ) ]: G.insrtEg(u,v) for u in G.V(): print(u, "-->", G.j(u)) f -> {} -> { : 0} -> { : 0} -> { : 0, : 0} -> { : 0, : 0, : 0} -> {} f Alrto Montrsor (UniTN) SP - Grphs 08//07 5 / 96
ttor i inz: grfo orintto Introution Rprsnttions Ajny vtor: irt grph j(u) = { v (u,v) E } G.j(u) = {v (u, v) E} Sp = n + mspzio: it n + m 0 0 3 3 3 3 0 4 3 4 5 4 5 6 4 5 nil Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96
Introution Rprsnttions Implmnttion tils In ll th following slis, w ssum tht: Th implmnttion is s on jny vtors/lists, ithr stti or ynmi Th No intifir is quivlnt to n intgr, n thus ring informtion out nos n gs rquir O() tim Th mthos to nos or gs rquir O() tim Aftr th initiliztion, th grph is stti Exris Th implmnttion s on vtors (Python lists) is lft s xris Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Introution Rprsnttions Initiliztion itionris / vtors Th following lgorithms r s on lists (vtors). To mk thm work using th implmnttion s on itionris, lins suh s must rpl with visit = [Fls] * G.siz() visit = it.fromkys(g.v(), Fls) Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96
Introution Rprsnttions Summry Ajny mtrix Rquir sp O(n ) To hk whthr u is jnt to v rquirs O() tim Il for ns grphs Ajny lists/vtors Rquir sp O(n + m) To hk whthr u is jnt to v rquirs O(n) Il for sprs grphs Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96
Introution Rprsnttions Itrtions ovr ll nos n gs To itrt ovr ll nos, w writ: for u in G.V(): # Prform som oprtions on no u To itrt ovr ll nos n gs, w writ: for u in G.V(): # Prform som oprtions on no u for v in G.j(u): # Prform som oprtions on g (u,v) Th omputtionl ost of suh oprtions r: Alrto Montrsor (UniTN) SP - Grphs 08//07 30 / 96
Introution Rprsnttions Itrtions ovr ll nos n gs To itrt ovr ll nos, w writ: for u in G.V(): # Prform som oprtions on no u To itrt ovr ll nos n gs, w writ: for u in G.V(): # Prform som oprtions on no u for v in G.j(u): # Prform som oprtions on g (u,v) Th omputtionl ost of suh oprtions r: O(m + n) with jny lists n thir vrint O(n ) with jny mtrixs Alrto Montrsor (UniTN) SP - Grphs 08//07 30 / 96
Tl of ontnts Introution Exmpls Dfinitions Spifition Rprsnttions Grph trvrsls 3 BFS Shortst pths 4 DFS Connt omponnts Unirt yli grphs Eg lssifition Dirt yli grphs Topologil Sort Strongly onnt omponnts
Grph trvrsls Grph trvrsls Prolm finition Givn grph G = (V, E) n vrtx r V (root), visit xtly on ll th vrtxs of th grph tht n rh from r Brth-first srh (BFS) Trvrs th grph y visiting th nos y lvls: first y visiting th nos t istn from th sour, thn istn, t. Applition: omput th shortst pths from singl sour Alrto Montrsor (UniTN) SP - Grphs 08//07 3 / 96
Grph trvrsls Grph trvrsls Prolm finition Givn grph G = (V, E) n vrtx r V (root), visit xtly on ll th vrtxs of th grph tht n rh from r Dpth-first srh (DFS) Trvrs th grph y visiting ll th nos tht n rh y no, n ll th nos tht n rh y thos nos, t. Applition: topologil sorting Applition: yl ttion Applition: onnt omponnts, strongly onnt omponnts Alrto Montrsor (UniTN) SP - Grphs 08//07 3 / 96
Grph trvrsls Trvrsl: not s sy s you my think On possil (niv) pproh: just itrt ovr nos n gs for u in G.V(): # Prform som oprtions on no u for v in G.j(u): # Prform som oprtions on g (u,v) Th strutur of th grph is not tkn into ount Just itrting ovr ll nos, ll gs, in no prtiulr orr Complxity (jny list, vtor): O(m + n) Alrto Montrsor (UniTN) SP - Grphs 08//07 33 / 96
Grph trvrsls Trvrsl: not s sy s you my think On possil (wrong) pproh: us th tr trvrsl f BFSTrvrsl(G, r): Q = qu() Q.ppn(r) whil ln(q)>0: u = Q.poplft() # visit of u for v in G.j(u): Q.ppn(v) Cll BFS s on quu Ajnt nos r trt s hilrn Alrto Montrsor (UniTN) SP - Grphs 08//07 34 / 96
Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = { } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96
Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = {,f, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96
Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = { f,,,, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96
Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = {,,,,,,,g } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96
Grph trvrsls Gnri trvrsl lgorithm f gnrivisit(g, r): s = DtStrutur() s.insrt(r) ### "mrk" r whil s.siz() > 0: u = s.rmov() ### visit no u for v in G.j(u): ### visit g(u,v) if not v in mrk: ### "mrk" v s.insrt(v) Alrto Montrsor (UniTN) SP - Grphs 08//07 36 / 96
Tl of ontnts Introution Exmpls Dfinitions Spifition Rprsnttions Grph trvrsls 3 BFS Shortst pths 4 DFS Connt omponnts Unirt yli grphs Eg lssifition Dirt yli grphs Topologil Sort Strongly onnt omponnts
BFS Brth-first srh - Gols To visit nos t inrsing istns from th sour Visit nos t istn k for visiting nos t istn k + Gnrt rth-first tr To gnrt tr ontining ll th nos rhl from r n suh tht th pth twn th root r n th no in th tr orrspons to shortst pth in th grph Comput th shortst pth from s to ll th othr rhl nos Distn msur s th numr of gs to trvrs Alrto Montrsor (UniTN) SP - Grphs 08//07 37 / 96
BFS Brth-first srh f fstrvrsl(g,r): Q = qu() Q.ppn(r) visit = [Fls] * G.siz() visit[r] = Tru whil ln(q)>0: u = Q.poplft() # Visit no u for v in G.j(u): # Visit g (u,v) if not visit[v]: visit[v] = Tru Q.ppn(v) Alrto Montrsor (UniTN) SP - Grphs 08//07 38 / 96
BFS Shortst pths BFS Applition: Shortst pth Pul Erös (93-996) Mthmtiin 500+ pprs, 500+ o-uthors Erös numr Erös hs ros = 0 Th o-uthors of Erös hv ros = If X is o-uthor of somon with ros = k, ut is not o-uthor of somon with ros < k, thn X hs ros = k + Popl who r not rh y this finition hv ros = + Alrto Montrsor (UniTN) SP - Grphs 08//07 39 / 96
BFS Shortst pths Alrto Montrsor, ros = 4 Alrto Montrsor, ros = 4 Alrto Montrsor (UniTN) SP - Grphs 08//07 40 / 96 33
BFS Shortst pths Computing th Erös numr f ros(g, r): rosn = [mth.inf] * G.siz() prnt = [-] * G.siz() Q = qu() Q.ppn(r) rosn[r] = 0 prnt[r] = r whil ln(q) > 0: u = Q.poplft() for v in G.j(u): if rosn[v] == mth.inf: rosn[v] = rosn[u]+ prnt[v] = u Q.ppn(v) rturn (ros, prnt) Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {,, f } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {, f,, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { f,,, h } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {,, h, g } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {, h, g } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { h, g } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { g, j } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { j } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { j } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96
BFS Shortst pths BFS Tr Th BFS visit n us to otin th shortst pth twn two nos (msur in numr of gs) "Spnning tr" root in r Stor in prnt vtor prnt f ros(g,r): [...] whil ln(q) > 0: u = Q.poplft() for v in G.j(u): if rosn[v] = mth.inf: rosn[v] = rosn[u]+ prnt[v] = u Q.ppn(v) f printpth(r, s, prnt): if r == s: print(s) lif prnt[s] < 0: print(rror) ls: printpth(r, prnt[s], prnt) print(s) Alrto Montrsor (UniTN) SP - Grphs 08//07 43 / 96
BFS Shortst pths BFS Tr 0 k f g h l Quu = { } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 44 / 96
Tl of ontnts Introution Exmpls Dfinitions Spifition Rprsnttions Grph trvrsls 3 BFS Shortst pths 4 DFS Connt omponnts Unirt yli grphs Eg lssifition Dirt yli grphs Topologil Sort Strongly onnt omponnts
Dpth-First Srh (DFS) Dpth-first srh Oftn suroutin of th solution of othr prolms Us to xplor th ntir grph, not just th nos rhl from singl sour (unlik BFS) Output Inst of tr, pth-first forst G f = (V, E f ) Contins olltion of pth-first trs Dt strutur Expliit Stk Or impliit stk, through rursion Alrto Montrsor (UniTN) SP - Grphs 08//07 45 / 96
Dpth-First Srh (Rursiv, impliit stk) f fs(g, u, mrk): mrk[u] = Tru ### Visit no u (pr-orr) for v in G.j(u): if not mrk[v]: fs(g, v, mrk) ### Visit no u (post-orr) Alrto Montrsor (UniTN) SP - Grphs 08//07 46 / 96
If you o not unit, you will gt th BFS vs DFS To xut DFS s on rursiv lls my risky in vry lrg grphs It is possil tht th rh pth is lrgr thn th siz of th lngug stk In suh ss, you shoul prfr BFS or DFS s on xpliit stk Dfult Vlu Xss fult vlus r pltform spif Stk siz in Jv Tl 9 Xss Dfult Vlus Pltform Dfult Winows IA3 64 KB Linux IA3 8 KB Winows x86_64 8 KB Linux x86_64 56 KB Winows IA64 30 KB Linux IA64 04 KB ( MB) Solris Spr 5 KB Flgs or Othr Options A Alrto Montrsor (UniTN) SP - Grphs 08//07 47 / 96
DFS (Itrtiv, xpliit stk, pr-orr) f fs(g, r): S = Stk( ) S.push(r) visit = [Fls] * G.siz() whil not S.isEmpty(): u = S.pop() if not visit[u]: # visit no u (pr-orr) visit[v] = Tru for v in G.j(u): # visit g (u,v) S.push(v) Not A no n insrt in th stk svrl tims Th hk if no hs n lry visit is on t th xtrtion, not whn insrting Complxity O(m + n) O(m) g visits O(m) insrt, rmov O(n) no visits Alrto Montrsor (UniTN) SP - Grphs 08//07 48 / 96
Connt omponnts (Strongly) Connt omponnts Motivtions Svrl lgorithms tht oprt on grphs strt y omposing th grph into isonnt omponnts Th lgorithm is thn xut in h of th omponnts Th rsults r thn ompos k togthr Dfinitions Connt omponnts (CC), fin on unirt grphs Strongly onnt omponnts (SCC), fin on irt grphs Alrto Montrsor (UniTN) SP - Grphs 08//07 49 / 96
Connt omponnts Dfinition: Rhility Rhl A no v is rhl from no u if thr is t lst on pth from u to v. No is rhl from no n vi-vrs No is rhl from no A, ut not vi-vrs Alrto Montrsor (UniTN) SP - Grphs 08//07 50 / 96
Connt omponnts Connt grphs n onnt omponnts Dfinitions Dfinizioni: Grfi onnssi omponnti onnss An unirt grph G = (V, E) is onnt iff vry no is rhl In un from grfo non vry orintto othr G no G è onnsso sist un mmino ogni vrti ogni ltro vrti An unirt grph G = (V, E ) is onnt omponnt iff G Un grfo G = (V, E ) è un omponnt onnss i G is onnt n mximl sugrph of G è un sottogrfo i G onnsso mssiml Dfinizioni G is sugrph of G (G G) iff V V n E E G è un sottogrfo i G (G G) s solo s V V E E G is mximl G è mssiml iff thr non is sist not un othr grph sottogrfo G of G Gi G suh h si tht G is onnt onnsso n più grn lrgri thn G, G ovvro (i.. G tl G pr ui G G G G) A 4 6 3 5 Alrto Montrsor (UniTN) SP - Grphs 08//07 5 5 / 96
Connt omponnts DFS Applition: Connt omponnts Prolm To hk whthr n unirt grph is onnt or not To intify its onnt omponnts Solutions A grph is onnt if, t th n of th DFS, ll nos hv n mrk If not, singl pss is not suffiint; th trvrsl must strt gin from n unmrk no, intifying nw omponnt of th grph Dt struturs List is tht ontins th omponnt intifirs is[u] is th intifir of th onnt omponnt to whih u longs Alrto Montrsor (UniTN) SP - Grphs 08//07 5 / 96
Connt omponnts DFS Applition: Connt omponnts f (G): f fs(g, ountr, u, is): is = [0] * G.siz() is[u] = ountr ountr = 0 for v in G.j(u): for u in G.V(): if is[v] == 0: if is[u] == 0: fs(g, ountr, v, is) ountr = ountr + fs(g, ountr, u, is) rturn (ountr, is) Alrto Montrsor (UniTN) SP - Grphs 08//07 53 / 96
Connt omponnts Exmpl: onnt omponnts g f h i j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Connt omponnts Exmpl: onnt omponnts f i g h 3 j 3 k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96
Unirt yli grphs Dfinition: Cyl Cyl In unirt grph G = (V, E), yl C of lngth k > is squn of nos u 0, u,..., u k suh tht (u i, u i+ E) for 0 i k n u 0 = u k. k > is mnt to xlu trivil yls ompos y g pirs (u, v) n (v, u), whih r vrywhr in unirt grphs Alrto Montrsor (UniTN) SP - Grphs 08//07 55 / 96
Unirt yli grphs Dfinition: Ayli grphs Ayli grph A unirt grph tht os not ontin yls, is ll yli. f Prolm Givn unirt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. How woul you solv th prolm? Alrto Montrsor (UniTN) SP - Grphs 08//07 56 / 96
Unirt yli grphs DFS Applition: Dtting yls f hscylr(g, u, from, visit): visit[u] = Tru for v in G.j(u): if v!= from: if visit[v]: rturn tru ls: if hscylr(g, v, u, visit): rturn Tru rturn Fls f hscyl(g): visit = [Fls] * G.siz() for u in G.V(): if not visit[v]: if hscylr(g, u, non,visit): rturn Tru rturn Fls Alrto Montrsor (UniTN) SP - Grphs 08//07 57 / 96
Unirt yli grphs Dfinition: Cyl Cyl In irt grph G = (V, E), yl C of lngth k is squn of nos u 0, u,..., u k suh tht (u i, u i+ E) for 0 i k n u 0 = u k. Exmpl:,,,,, is yl of lngth 5 Not: yl is ll simpl if ll its nos r istint (xluing th first n th lst ons) Alrto Montrsor (UniTN) SP - Grphs 08//07 58 / 96
Unirt yli grphs Dfinition: Dirt yli grph (DAG) DAG A irt yli grph (DAG) is irt grph tht os not ontin yls. f Cyli grph A grph ontining yl is ll yli f Alrto Montrsor (UniTN) SP - Grphs 08//07 59 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Unirt yli grphs DFS: Cyl ttion Prolm Givn irt grph G, writ n lgorithm tht rturns tru if G ontins yl, fls othrwis. Prolm Cn you rw irt grph suh tht th lgorithm w hv sn for os not rturn th orrt nswr? Alrto Montrsor (UniTN) SP - Grphs 08//07 60 / 96
Eg lssifition Eg Clssifition DFS Spnning Tr Whnvr n g onnting mrk no to n unmrk on, it is insrt into tr T Evry g (u, v) not inlu in T longs to on of thr tgoris (u, v) is forwr g iff v is snnt of u in T (u, v) is k g iff v is n nstor of u in T Othrwis, (u, v)is ross g Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok lok: inrsing ountr t: isovry tim ft: finish tim Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] t[v] = 0 Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, ] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, ] t[v] = 0 Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, ] [3, ] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, ] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] t[u] < t[v], ft[v] 0 [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok t[v] = 0 [, ] [, 5] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] [6, ] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] [6, ] t[u] > t[v], ft[v] = 0 [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] [6, ] othrwis [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, ] [, 5] [6, 7] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, 8] [, 5] [6, 7] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, 8] [9, ] [, 5] [6, 7] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, 8] [9, ] [, 5] othrwis [6, 7] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition DFS Shm f fs-shm(g, u): nonlol lok, t, ft lok = lok + t[u] = lok for v in G.j(u): if t[v] == 0: # visit tr g (u, v) fs-shm(g, v) lif t[u] > t[v] n ft[v] == 0: # visit k g (u, v) lif t[u] < t[v] n ft[v]!= 0: # visit forwr g (u, v) ls: # visit ross g (u, v) lok = lok + ft[u] = lok [, 8] [9, 0] [, 5] [6, 7] [3, 4] Alrto Montrsor (UniTN) SP - Grphs 08//07 63 / 96
Eg lssifition Eg lssifition Why r w lssifying gs? W n prov proprtis on th typ of gs n us ths proprtis to uil ttr lgorithms Thorm In h DFS visit of grph G = (V, E), for h pir of nos u, v V, only on of th following onitions is tru: Th intrvls [t[u], ft[u]] [t[v], ft[v]] r non-ovrlpping; u, v r not snnt of h othr in th DF forst Intrvl [t[u], ft[u]] is ompltly ontin in [t[v], ft[v]]; u is snnt of v in DF tr Intrvl [t[v], ft[v]] is ompltly ontin in [t[u], ft[u]]; v is snnt of u in DF tr Alrto Montrsor (UniTN) SP - Grphs 08//07 64 / 96
Dirt yli grphs Cyl ttion Thorm A grph G ontins yl if k g is foun whn DFS is prform on G. Informl proof if: If thr is yl, lt u th first no of it tht is visit. Givn tht u longs to th yl, thr is n g (v, u) in th yl. Givn tht v longs to th yl, thr is pth from u to v. So (u, v) is k g. only if: if thr is k g (u, v), whr v is n nstor of u, thn thr is pth from v to u n n g from u to v, thus thr is yl. Alrto Montrsor (UniTN) SP - Grphs 08//07 65 / 96
Dirt yli grphs DFS Applition: DAG f hscyl(g): f hscylr(g, u): nonlol lok, t, ft lok = lok+ t[u] = lok for v in G.j(u): if t[v] == 0: if hscylr(g,v): rturn Tru lif t[u] > t[v] n ft[v] == 0: rturn Tru lok = lok+ ft[u] = lok rturn Fls Alrto Montrsor (UniTN) SP - Grphs 08//07 66 / 96
Dirt yli grphs DFS Applition: DAG f hscyl(g): f hscylr(g, u): [...] lok = 0 t = [0] * G.siz() ft = [0] * G.siz() for u in G.V(): if ft[u] == 0: if hscylr(g, u): rturn Tru rturn Fls Alrto Montrsor (UniTN) SP - Grphs 08//07 67 / 96
Dirt yli grphs DFS Applition: DAG Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG t[v] = 0 [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, ] t[v] = 0 [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] [3, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] [3, 4] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, 5] [, ] [3, 4] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, 5] [, ] t[u] < t[v], ft[v] 0 [3, 4] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG [, 5] [, 6] [3, 4] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 68 / 96
Dirt yli grphs DFS Applition: DAG No k g is foun, so ll rursiv lls will omplt n will rturn Fls f hscyl(g, u): nonlol lok, t, ft lok = lok+ t[u] = lok for v in G.j(u): if t[v] == 0: if hscyl(g,v): rturn Tru lif t[u] > t[v] n ft[v] == 0: rturn Tru lok = lok+ ft[u] = lok rturn Fls Alrto Montrsor (UniTN) SP - Grphs 08//07 69 / 96
Dirt yli grphs DFS Applition: DAG Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG t[v] = 0 [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG [, ] t[v] = 0 [, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] [3, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG [, ] [, ] t[u] > t[v], ft[v] = 0 [3, ] Tr g t[v] == 0 Bk g: t[u] > t[v] n ft[v] = 0 Forwr g: t[u] < t[v] n ft[v] 0 Cross g: othrwis Alrto Montrsor (UniTN) SP - Grphs 08//07 70 / 96
Dirt yli grphs DFS Applition: DAG A k g is foun; in on of th rursiv ll, Tru is rturn. On you gt Tru from rursiv ll, vry ll rturns Tru. f hscyl(g, u): nonlol lok, t, ft lok = lok+ t[u] = lok for v in G.j(u): if t[v] == 0: if hscyl(g,v): rturn Tru lif t[u] > t[v] n ft[v] == 0: rturn Tru lok = lok+ ft[u] = lok rturn Fls Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Topologil Sort Topologil Sort rinmnto topologio Dfinition Dto un DAG G (irt yli grph), un orinmnto topologio su G è un orinmnto Givn linr DAG i G, suoi topologil vrtii tl pr sort ui: of G is linr orring of its s nos G ontin suh l ro tht if (u,v), llor v) u E, ompr thn uprim pprs i v nll orinmnto for v th orring Pr trnsitività, n onsgu h s v è rggiungiil u, Nots: llor u ompr prim i v nll'orinmnto Thr oul svrl topologil sorts Not: If thr possono isssri yl, più orinmnti no topologil topologii sort is possil 3 5 4 3 4 5 3 4 5 lrto Montrsor 74 Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96
Topologil Sort Topologil Sort Prolm Writ n lgorithm tht tks DAG G s input n rturns topologil sort of G s output. How woul you solv this prolm? Alrto Montrsor (UniTN) SP - Grphs 08//07 73 / 96
Topologil Sort Topologil Sort Prolm Writ n lgorithm tht tks DAG G s input n rturns topologil sort of G s output. How woul you solv this prolm? Niv solution Fin no u with no inoming gs Appn u to list; rmov u, togthr with ll its gs Rpt th prour until ll nos hv n rmov Alrto Montrsor (UniTN) SP - Grphs 08//07 73 / 96
Topologil Sort Topologil Sort - Niv lgorithm Soluzion irtt 3 4 3 4 4 5 5 5 Output: Output: Output: 3 4 4 Output: 3 5 Output: 3 5 Output: 3 5 4 Alrto Montrsor 76 Alrto Montrsor (UniTN) SP - Grphs 08//07 74 / 96
Topologil Sort Topologil Sort s on DFS Algorithm Exut DFS in whih th "visit" oprtion onsists of ing th no t th h of list "t finish tim" (post-orr) Rturn th list of nos otin in this wy Output Th squn of nos, sort y rsing finish tim Why os it work? Alrto Montrsor (UniTN) SP - Grphs 08//07 75 / 96
Topologil Sort Topologil Sort s on DFS Algorithm Exut DFS in whih th "visit" oprtion onsists of ing th no t th h of list "t finish tim" (post-orr) Rturn th list of nos otin in this wy Output Th squn of nos, sort y rsing finish tim Why os it work? Whn no is "finish", ll its snnts hv n isovr n to th list. By ing th no in front of th list, nos r sort orrtly W us stk inst Alrto Montrsor (UniTN) SP - Grphs 08//07 75 / 96
Topologil Sort Topologil Sort - Th lgorithm f topsort(g): S = Stk() visit = [Fls]*G.siz() for u in G.V(): if not visit[u]: topsortr(g, u, visit, S) rturn S f topsortr(g, u, visit, S): visit[u] = Tru for v in G.j(u): if not visit[v]: topsortr(g, v, visit, S) S.push(u) Alrto Montrsor (UniTN) SP - Grphs 08//07 76 / 96
Topologil Sort Topologil Sort Exmpl Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] [, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] [, ] [3, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] [, ] [3, 4] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [7, ] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [7, 8] [3, 4] Stk = {,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, ] [, 5] [7, 8] [3, 4] Stk = {,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] Stk = {,,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] Stk = {,,,, } Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] [, ] Stk = {,,,, } Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] [3, 4] [, ] Stk = {,,,, } Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] [5, 6] [3, 4] [, ] Stk = {,,,, } Stk = {,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [7, 8] [, 0] [, 5] [7, 8] [3, 4] [5, 6] [3, 4] [, ] Stk = {,,,, } Stk = {,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Topologil Sort Topologil Sort Exmpl [6, 9] [7, 8] [, 0] [, 5] [7, 8] [3, 4] [9, 0] [5, 6] [3, 4] [, ] Stk = {,,,, } Stk = {,,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96
Strongly onnt omponnts Strongly onnt grphs n omponnts Dfinitions A irt grph G = (V, E) is strongly onnt iff vry no is rhl from vry othr no A irt grph G = (V, E ) is strongly onnt omponnt iff G is onnt n mximl sugrph of G G is sugrph of G (G G) iff V V n E E G is mximl iff thr is not othr grph G of G suh tht G is strongly onnt n lrgr thn G (i.. G G G) Alrto Montrsor (UniTN) SP - Grphs 08//07 78 / 96
Strongly onnt omponnts Strongly onnt omponnts Qustion Wht r th strongly onnt omponnts of this grph? f Alrto Montrsor (UniTN) SP - Grphs 08//07 79 / 96
Strongly onnt omponnts Strongly onnt omponnts Qustion Wht r th strongly onnt omponnts of this grph? f Alrto Montrsor (UniTN) SP - Grphs 08//07 79 / 96
Strongly onnt omponnts Niv (n wrong) solution Just pply th CC lgorithm to irt grphs Th rsult pns on th strting no 3 f Alrto Montrsor (UniTN) SP - Grphs 08//07 80 / 96
Strongly onnt omponnts Niv (n wrong) solution Just pply th CC lgorithm to irt grphs Th rsult pns on th strting no f Alrto Montrsor (UniTN) SP - Grphs 08//07 80 / 96
Strongly onnt omponnts Niv (n wrong) solution Just pply th CC lgorithm to irt grphs Th rsult pns on th strting no f Alrto Montrsor (UniTN) SP - Grphs 08//07 80 / 96
Strongly onnt omponnts SCC Algorithm Kosrju Algorithm (978) Prform DFS of G Comput th trnspos grph G T Run th onnt omponnt lgorithm on G T, xmining th nos in rsing finish tim w.r.t. th first visit Rturns th intifirs of th nos f s(g): S = topsort(g) GT = trnspos(g) ountr, is = (GT, S) rturn ountr, is Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96
Strongly onnt omponnts Topologil sort on gnrl grphs Gnrl i By pplying th topologil sort lgorithm on gnrl grph, w r sur tht: if n g (u, v) os not long to yl, thn u pprs for v in th sort squn W us thus topsort() to otin nos in rsing finish tim. Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96
Strongly onnt omponnts Exution : Topologil Sort [, ] [3, 0] [, ] [7, 8] [4, 9] f [5, 6] Stk = {,,,,, f } Alrto Montrsor (UniTN) SP - Grphs 08//07 83 / 96
Strongly onnt omponnts Comput th trnspos grph Dfinition Givn grph G = (V, E), th trnspos grph G T = (V, E T ) hs th sm nos, whil gs r irt in th opposit wy: E T = {(u, v) (v, u) E} f trnspos(g): GT = Grph() for u in G.V(): for v in G.j(u): GT.insrtEg(v,u) rturn GT Computtionl ost: O(m + n) O(n) nos O(m) gs Eh oprtion osts O() Alrto Montrsor (UniTN) SP - Grphs 08//07 84 / 96
Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 85 / 96
Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 85 / 96
Strongly onnt omponnts Exution : Connt omponnts Inst of xmining th nos in n ritrry orr, this vrsion of () xmins thm in th orr in whih thy r stor in stk. f (G, S): f fs(g, ountr, u, is): is = [0] * G.siz() is[u] = ountr ountr = 0 for v in G.j(u): whil S.siz()>0: if is[v] == 0: u = s.pop() fs(g, ountr, v, is) if is[u] == 0: ountr = ountr + fs(g, ountr, u, is) rturn (ountr, is) Alrto Montrsor (UniTN) SP - Grphs 08//07 86 / 96
Strongly onnt omponnts Connt omponnts [3, 4] [5, ] [, ] [6, 9] f [0, ] [7, 8] Stk = {,,,,, f } Alrto Montrsor (UniTN) SP - Grphs 08//07 87 / 96
Strongly onnt omponnts SCC: Th lgorithm f s(g): S = topsort(g) GT = trnspos(g) ountr, is = (GT, S) rturn ountr, is Computtionl ost: O(m + n) Eh phs rquirs O(m + n) Alrto Montrsor (UniTN) SP - Grphs 08//07 88 / 96
Strongly onnt omponnts Exution : Topologil Sort [0, ] [, 7] [9, ] [4, 5] [3, 6] f [, 8] Stk = {,, f,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 89 / 96
Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 90 / 96
Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 90 / 96
Strongly onnt omponnts Exution : Connt omponnts [3, 4] [7, 0] [, ] [5, ] f [6, ] [8, 9] Stk = {,, f,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96
Strongly onnt omponnts Corrtnss proof Componnt Grph V = (V, E ) V = {C, C,..., C k }, whr C i is th i-th SCC of G E = {(C u, C v ) (u, v) E u C u v C v } Qustions Wht is th rltionship twn th SCCs of G n th SCCs of G T? Is th omponnt grph yli? f Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96
Strongly onnt omponnts Corrtnss proof Disovry tim n finish for th omponnt grph t(c) = min{t(u) u C} ft(c) = mx{ft(u) u C} Ths isovry/finish tims orrspon to th isovry/finish tim of th first no to visit in omponnt C Alrto Montrsor (UniTN) SP - Grphs 08//07 93 / 96
Strongly onnt omponnts Corrtnss proof Thorm Lt C n C two istint SCCs in th irt grph G = (V, E). If thr is n g (C, C ) E, thn ft(c) > ft(c ). [, ] [0, ] [3, 0] [, 7] [, ] [5, 6] [9, ] f [, 8] f [7, 8] [4, 9] [4, 5] [3, 6] Alrto Montrsor (UniTN) SP - Grphs 08//07 94 / 96
Strongly onnt omponnts Corrtnss proof Corollry Lt C u n C v two istint SCCs in th irt grph G = (V, E). If thr is n g (u, v) E t with u C u n v C v, thn ft(c u ) < ft(c v ). (u, v) E t [0, ] (v, u) E [, 7] (C v, u ) E ft(c v ) > ft(c u ) [9, ] [, 8] f ft(c u ) < ft(c v ) [4, 5] [3, 6] Alrto Montrsor (UniTN) SP - Grphs 08//07 95 / 96
Strongly onnt omponnts Corrtnss proof Corollry Lt C u n C v two istint SCCs in th irt grph G = (V, E). If thr is n g (u, v) E t with u C u n v C v, thn ft(c u ) < ft(c v ). (, ) E t [0, ] (, ) E [, 7] (C, C ) E = ft(c ) > ft(c ) = [9, ] [, 8] f = ft(c ) < ft(c ) = [4, 5] [3, 6] Alrto Montrsor (UniTN) SP - Grphs 08//07 95 / 96
Strongly onnt omponnts Corrtnss proof If th omponnt C u n th omponnt C v r onnt y n g (u, v) E t, thn: From th orollry, ft(c u ) < ft(c v ) From th lgorithm, th visit of C v will strt for th visit of C u Thr is no pth twn C v n C u in G t (othrwis th grph woul yli) From th lgorithm, th visit of C v will not rh C u, In othr wors, () will orrtly ssign th omponnt intifirs to nos. Alrto Montrsor (UniTN) SP - Grphs 08//07 96 / 96