Scientific Programming. Graphs

Size: px
Start display at page:

Download "Scientific Programming. Graphs"

Transcription

1 Sintifi Progrmming Grphs Alrto Montrsor Univrsità i Trnto 08//07 This work is lins unr Crtiv Commons Attriution-ShrAlik 4.0 Intrntionl Lins.

2 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

3 Introution Exmpls Exmpls Esmpi i grfi Alrto Montrsor Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96

4 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

5 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

6 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 Burt, Ronl. Struturl Hols: Th Strutur of Comptition. Hrvr Univrsity Prss, 99 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96

7 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

8 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

9 Introution Exmpls Applition xmpl f h g Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96

10 Introution Exmpls Applition xmpl f h g h g h g h g Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96

11 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

12 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

13 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

14 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

15 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

16 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 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

17 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

18 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

19 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

20 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

21 Introution Rprsnttions Rprsnttions Two possil "lssi" implmnttions Ajny mtrix Ajny lists Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96

22 Introution Rprsnttions Ajny mtrix: irt grphs m uv = { (u, v) E 0 (u, v) E Sp = n its Alrto Montrsor (UniTN) SP - Grphs 08//07 8 / 96

23 Introution Rprsnttions Ajny list: irt grph G.j(u) = {v (u, v) E} Sp = n + m its Alrto Montrsor (UniTN) SP - Grphs 08//07 9 / 96

24 Introution Rprsnttions Ajny mtrix: unirt grphs m uv = { (u, v) E 0 (u, v) E Sp = n or n(n )/ Alrto Montrsor (UniTN) SP - Grphs 08//07 0 / 96

25 Introution Rprsnttions Ajny list: unirt grph G.j(u) = {v (u, v) E} Sp = n + m Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96

26 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) = Alrto Montrsor (UniTN) SP - Grphs 08//07 / 96

27 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

28 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

29 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

30 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 nil Alrto Montrsor (UniTN) SP - Grphs 08//07 6 / 96

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = { } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96

42 Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = {,f, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96

43 Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = { f,,,, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96

44 Grph trvrsls Exmpl: Wrong trvrsl k f g h l Quu = {,,,,,,,g } j Alrto Montrsor (UniTN) SP - Grphs 08//07 35 / 96

45 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

46 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

47 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

48 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

49 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

50 BFS Shortst pths Alrto Montrsor, ros = 4 Alrto Montrsor, ros = 4 Alrto Montrsor (UniTN) SP - Grphs 08//07 40 / 96 33

51 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

52 BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96

53 BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {,, f } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96

54 BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = {, f,, } j Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96

55 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

56 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

57 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

58 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

59 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

60 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

61 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

62 BFS Shortst pths Exmpl: Erös 0 k f g h l Quu = { } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 4 / 96

63 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

64 BFS Shortst pths BFS Tr 0 k f g h l Quu = { } j 3 Alrto Montrsor (UniTN) SP - Grphs 08//07 44 / 96

65 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

66 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

67 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

68 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

69 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

70 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

71 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

72 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 Alrto Montrsor (UniTN) SP - Grphs 08// / 96

73 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

74 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

75 Connt omponnts Exmpl: onnt omponnts g f h i j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

76 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

77 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

78 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

79 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

80 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

81 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

82 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

83 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

84 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

85 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

86 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

87 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

88 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

89 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

90 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

91 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

92 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

93 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

94 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

95 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

96 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

97 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

98 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

99 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

100 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

101 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

102 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

103 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

104 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

105 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

106 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

107 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

108 Connt omponnts Exmpl: onnt omponnts f i g h j k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

109 Connt omponnts Exmpl: onnt omponnts f i g h 3 j 3 k Alrto Montrsor (UniTN) SP - Grphs 08//07 54 / 96

110 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

111 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

112 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

113 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

114 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

115 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

116 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

117 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

118 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

119 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

120 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

121 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

122 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

123 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

124 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

125 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

126 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

127 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

128 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

129 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

130 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

131 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

132 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

133 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

134 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

135 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

136 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

137 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

138 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

139 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

140 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

141 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

142 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

143 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

144 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

145 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

146 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

147 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

148 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

149 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

150 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

151 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

152 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

153 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

154 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

155 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

156 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

157 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

158 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

159 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

160 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

161 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

162 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

163 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

164 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 lrto Montrsor 74 Alrto Montrsor (UniTN) SP - Grphs 08//07 7 / 96

165 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

166 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

167 Topologil Sort Topologil Sort - Niv lgorithm Soluzion irtt Output: Output: Output: Output: 3 5 Output: 3 5 Output: Alrto Montrsor 76 Alrto Montrsor (UniTN) SP - Grphs 08//07 74 / 96

168 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

169 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

170 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

171 Topologil Sort Topologil Sort Exmpl Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

172 Topologil Sort Topologil Sort Exmpl [, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

173 Topologil Sort Topologil Sort Exmpl [, ] [, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

174 Topologil Sort Topologil Sort Exmpl [, ] [, ] [3, ] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

175 Topologil Sort Topologil Sort Exmpl [, ] [, ] [3, 4] Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

176 Topologil Sort Topologil Sort Exmpl [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

177 Topologil Sort Topologil Sort Exmpl [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

178 Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

179 Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [7, ] [3, 4] Stk = {, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

180 Topologil Sort Topologil Sort Exmpl [6, ] [, ] [, 5] [7, 8] [3, 4] Stk = {,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

181 Topologil Sort Topologil Sort Exmpl [6, 9] [, ] [, 5] [7, 8] [3, 4] Stk = {,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

182 Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] Stk = {,,,, } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

183 Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] Stk = {,,,, } Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

184 Topologil Sort Topologil Sort Exmpl [6, 9] [, 0] [, 5] [7, 8] [3, 4] [, ] Stk = {,,,, } Stk = { } Alrto Montrsor (UniTN) SP - Grphs 08//07 77 / 96

185 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

186 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

187 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

188 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

189 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

190 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

191 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

192 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

193 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

194 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

195 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

196 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

197 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

198 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

199 Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 85 / 96

200 Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 85 / 96

201 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

202 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

203 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

204 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

205 Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 90 / 96

206 Strongly onnt omponnts Exution : Trnspos grph f Alrto Montrsor (UniTN) SP - Grphs 08//07 90 / 96

207 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

208 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

209 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

210 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

211 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

212 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

213 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

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} Introution Computr Sin & Enginring 423/823 Dsign n Anlysis of Algorithms Ltur 03 Elmntry Grph Algorithms (Chptr 22) Stphn Sott (Apt from Vinohnrn N. Vriym) I Grphs r strt t typs tht r pplil to numrous

More information

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} s s of s Computr Sin & Enginring 423/823 Dsign n Anlysis of Ltur 03 (Chptr 22) Stphn Sott (Apt from Vinohnrn N. Vriym) s of s s r strt t typs tht r pplil to numrous prolms Cn ptur ntitis, rltionships twn

More information

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018 CSE 373: Mor on grphs; DFS n BFS Mihl L Wnsy, F 14, 2018 1 Wrmup Wrmup: Disuss with your nighor: Rmin your nighor: wht is simpl grph? Suppos w hv simpl, irt grph with x nos. Wht is th mximum numr of gs

More information

An undirected graph G = (V, E) V a set of vertices E a set of unordered edges (v,w) where v, w in V

An undirected graph G = (V, E) V a set of vertices E a set of unordered edges (v,w) where v, w in V Unirt Grphs An unirt grph G = (V, E) V st o vrtis E st o unorr gs (v,w) whr v, w in V USE: to mol symmtri rltionships twn ntitis vrtis v n w r jnt i thr is n g (v,w) [or (w,v)] th g (v,w) is inint upon

More information

CS61B Lecture #33. Administrivia: Autograder will run this evening. Today s Readings: Graph Structures: DSIJ, Chapter 12

CS61B Lecture #33. Administrivia: Autograder will run this evening. Today s Readings: Graph Structures: DSIJ, Chapter 12 Aministrivi: CS61B Ltur #33 Autogrr will run this vning. Toy s Rings: Grph Struturs: DSIJ, Chptr 12 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 1 Why Grphs? For xprssing non-hirrhilly rlt itms Exmpls:

More information

CS 461, Lecture 17. Today s Outline. Example Run

CS 461, Lecture 17. Today s Outline. Example Run Prim s Algorithm CS 461, Ltur 17 Jr Si Univrsity o Nw Mxio In Prim s lgorithm, th st A mintin y th lgorithm orms singl tr. Th tr strts rom n ritrry root vrtx n grows until it spns ll th vrtis in V At h

More information

CS 241 Analysis of Algorithms

CS 241 Analysis of Algorithms CS 241 Anlysis o Algorithms Prossor Eri Aron Ltur T Th 9:00m Ltur Mting Lotion: OLB 205 Businss HW6 u lry HW7 out tr Thnksgiving Ring: Ch. 22.1-22.3 1 Grphs (S S. B.4) Grphs ommonly rprsnt onntions mong

More information

Paths. Connectivity. Euler and Hamilton Paths. Planar graphs.

Paths. Connectivity. Euler and Hamilton Paths. Planar graphs. Pths.. Eulr n Hmilton Pths.. Pth D. A pth rom s to t is squn o gs {x 0, x 1 }, {x 1, x 2 },... {x n 1, x n }, whr x 0 = s, n x n = t. D. Th lngth o pth is th numr o gs in it. {, } {, } {, } {, } {, } {,

More information

CSC Design and Analysis of Algorithms. Example: Change-Making Problem

CSC Design and Analysis of Algorithms. Example: Change-Making Problem CSC 801- Dsign n Anlysis of Algorithms Ltur 11 Gry Thniqu Exmpl: Chng-Mking Prolm Givn unlimit mounts of oins of nomintions 1 > > m, giv hng for mount n with th lst numr of oins Exmpl: 1 = 25, 2 =10, =

More information

Graphs. CSC 1300 Discrete Structures Villanova University. Villanova CSC Dr Papalaskari

Graphs. CSC 1300 Discrete Structures Villanova University. Villanova CSC Dr Papalaskari Grphs CSC 1300 Disrt Struturs Villnov Univrsity Grphs Grphs r isrt struturs onsis?ng of vr?s n gs tht onnt ths vr?s. Grphs n us to mol: omputr systms/ntworks mthm?l rl?ons logi iruit lyout jos/prosss f

More information

12/3/12. Outline. Part 10. Graphs. Circuits. Euler paths/circuits. Euler s bridge problem (Bridges of Konigsberg Problem)

12/3/12. Outline. Part 10. Graphs. Circuits. Euler paths/circuits. Euler s bridge problem (Bridges of Konigsberg Problem) 12/3/12 Outlin Prt 10. Grphs CS 200 Algorithms n Dt Struturs Introution Trminology Implmnting Grphs Grph Trvrsls Topologil Sorting Shortst Pths Spnning Trs Minimum Spnning Trs Ciruits 1 Ciruits Cyl 2 Eulr

More information

CS200: Graphs. Graphs. Directed Graphs. Graphs/Networks Around Us. What can this represent? Sometimes we want to represent directionality:

CS200: Graphs. Graphs. Directed Graphs. Graphs/Networks Around Us. What can this represent? Sometimes we want to represent directionality: CS2: Grphs Prihr Ch. 4 Rosn Ch. Grphs A olltion of nos n gs Wht n this rprsnt? n A omputr ntwork n Astrtion of mp n Soil ntwork CS2 - Hsh Tls 2 Dirt Grphs Grphs/Ntworks Aroun Us A olltion of nos n irt

More information

5/9/13. Part 10. Graphs. Outline. Circuits. Introduction Terminology Implementing Graphs

5/9/13. Part 10. Graphs. Outline. Circuits. Introduction Terminology Implementing Graphs Prt 10. Grphs CS 200 Algorithms n Dt Struturs 1 Introution Trminology Implmnting Grphs Outlin Grph Trvrsls Topologil Sorting Shortst Pths Spnning Trs Minimum Spnning Trs Ciruits 2 Ciruits Cyl A spil yl

More information

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s?

b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s? MATH 3012 Finl Exm, My 4, 2006, WTT Stunt Nm n ID Numr 1. All our prts o this prolm r onrn with trnry strings o lngth n, i.., wors o lngth n with lttrs rom th lpht {0, 1, 2}.. How mny trnry wors o lngth

More information

Graphs. Graphs. Graphs: Basic Terminology. Directed Graphs. Dr Papalaskari 1

Graphs. Graphs. Graphs: Basic Terminology. Directed Graphs. Dr Papalaskari 1 CSC 00 Disrt Struturs : Introuon to Grph Thory Grphs Grphs CSC 00 Disrt Struturs Villnov Univrsity Grphs r isrt struturs onsisng o vrs n gs tht onnt ths vrs. Grphs n us to mol: omputr systms/ntworks mthml

More information

CSE 373. Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9. slides created by Marty Stepp

CSE 373. Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9. slides created by Marty Stepp CSE 373 Grphs 1: Conpts, Dpth/Brth-First Srh ring: Wiss Ch. 9 slis rt y Mrty Stpp http://www.s.wshington.u/373/ Univrsity o Wshington, ll rights rsrv. 1 Wht is grph? 56 Tokyo Sttl Soul 128 16 30 181 140

More information

Outline. Circuits. Euler paths/circuits 4/25/12. Part 10. Graphs. Euler s bridge problem (Bridges of Konigsberg Problem)

Outline. Circuits. Euler paths/circuits 4/25/12. Part 10. Graphs. Euler s bridge problem (Bridges of Konigsberg Problem) 4/25/12 Outlin Prt 10. Grphs CS 200 Algorithms n Dt Struturs Introution Trminology Implmnting Grphs Grph Trvrsls Topologil Sorting Shortst Pths Spnning Trs Minimum Spnning Trs Ciruits 1 2 Eulr s rig prolm

More information

Section 10.4 Connectivity (up to paths and isomorphism, not including)

Section 10.4 Connectivity (up to paths and isomorphism, not including) Toy w will isuss two stions: Stion 10.3 Rprsnting Grphs n Grph Isomorphism Stion 10.4 Conntivity (up to pths n isomorphism, not inluing) 1 10.3 Rprsnting Grphs n Grph Isomorphism Whn w r working on n lgorithm

More information

Exam 1 Solution. CS 542 Advanced Data Structures and Algorithms 2/14/2013

Exam 1 Solution. CS 542 Advanced Data Structures and Algorithms 2/14/2013 CS Avn Dt Struturs n Algorithms Exm Solution Jon Turnr //. ( points) Suppos you r givn grph G=(V,E) with g wights w() n minimum spnning tr T o G. Now, suppos nw g {u,v} is to G. Dsri (in wors) mtho or

More information

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes.

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes. Nm: UCA ID Numr: Stion lttr: th 61 : Disrt Struturs Finl Exm Instrutor: Ciprin nolsu You hv 180 minuts. No ooks, nots or lultors r llow. Do not us your own srth ppr. 1. (2 points h) Tru/Fls: Cirl th right

More information

Graph Isomorphism. Graphs - II. Cayley s Formula. Planar Graphs. Outline. Is K 5 planar? The number of labeled trees on n nodes is n n-2

Graph Isomorphism. Graphs - II. Cayley s Formula. Planar Graphs. Outline. Is K 5 planar? The number of labeled trees on n nodes is n n-2 Grt Thortil Is In Computr Sin Vitor Amhik CS 15-251 Ltur 9 Grphs - II Crngi Mllon Univrsity Grph Isomorphism finition. Two simpl grphs G n H r isomorphi G H if thr is vrtx ijtion V H ->V G tht prsrvs jny

More information

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura Moul grph.py CS 231 Nomi Nishimur 1 Introution Just lik th Python list n th Python itionry provi wys of storing, ssing, n moifying t, grph n viw s wy of storing, ssing, n moifying t. Bus Python os not

More information

Algorithmic and NP-Completeness Aspects of a Total Lict Domination Number of a Graph

Algorithmic and NP-Completeness Aspects of a Total Lict Domination Number of a Graph Intrntionl J.Mth. Comin. Vol.1(2014), 80-86 Algorithmi n NP-Compltnss Aspts of Totl Lit Domintion Numr of Grph Girish.V.R. (PES Institut of Thnology(South Cmpus), Bnglor, Krntk Stt, Ini) P.Ush (Dprtmnt

More information

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management nrl tr T is init st o on or mor nos suh tht thr is on sint no r, ll th root o T, n th rminin nos r prtition into n isjoint susts T, T,, T n, h o whih is tr, n whos roots r, r,, r n, rsptivly, r hilrn o

More information

Similarity Search. The Binary Branch Distance. Nikolaus Augsten.

Similarity Search. The Binary Branch Distance. Nikolaus Augsten. Similrity Srh Th Binry Brnh Distn Nikolus Augstn nikolus.ugstn@sg..t Dpt. of Computr Sins Univrsity of Slzurg http://rsrh.uni-slzurg.t Vrsion Jnury 11, 2017 Wintrsmstr 2016/2017 Augstn (Univ. Slzurg) Similrity

More information

Why the Junction Tree Algorithm? The Junction Tree Algorithm. Clique Potential Representation. Overview. Chris Williams 1.

Why the Junction Tree Algorithm? The Junction Tree Algorithm. Clique Potential Representation. Overview. Chris Williams 1. Why th Juntion Tr lgorithm? Th Juntion Tr lgorithm hris Willims 1 Shool of Informtis, Univrsity of Einurgh Otor 2009 Th JT is gnrl-purpos lgorithm for omputing (onitionl) mrginls on grphs. It os this y

More information

Planar Upward Drawings

Planar Upward Drawings C.S. 252 Pro. Rorto Tmssi Computtionl Gomtry Sm. II, 1992 1993 Dt: My 3, 1993 Sri: Shmsi Moussvi Plnr Upwr Drwings 1 Thorm: G is yli i n only i it hs upwr rwing. Proo: 1. An upwr rwing is yli. Follow th

More information

Outline. 1 Introduction. 2 Min-Cost Spanning Trees. 4 Example

Outline. 1 Introduction. 2 Min-Cost Spanning Trees. 4 Example Outlin Computr Sin 33 Computtion o Minimum-Cost Spnnin Trs Prim's Alorithm Introution Mik Joson Dprtmnt o Computr Sin Univrsity o Clry Ltur #33 3 Alorithm Gnrl Constrution Mik Joson (Univrsity o Clry)

More information

COMP108 Algorithmic Foundations

COMP108 Algorithmic Foundations Grdy mthods Prudn Wong http://www.s.liv..uk/~pwong/thing/omp108/01617 Coin Chng Prolm Suppos w hv 3 typs of oins 10p 0p 50p Minimum numr of oins to mk 0.8, 1.0, 1.? Grdy mthod Lrning outoms Undrstnd wht

More information

Announcements. Not graphs. These are Graphs. Applications of Graphs. Graph Definitions. Graphs & Graph Algorithms. A6 released today: Risk

Announcements. Not graphs. These are Graphs. Applications of Graphs. Graph Definitions. Graphs & Graph Algorithms. A6 released today: Risk Grphs & Grph Algorithms Ltur CS Spring 6 Announmnts A6 rls toy: Risk Strt signing with your prtnr sp Prlim usy Not grphs hs r Grphs K 5 K, =...not th kin w mn, nywy Applitions o Grphs Communition ntworks

More information

Weighted graphs -- reminder. Data Structures LECTURE 15. Shortest paths algorithms. Example: weighted graph. Two basic properties of shortest paths

Weighted graphs -- reminder. Data Structures LECTURE 15. Shortest paths algorithms. Example: weighted graph. Two basic properties of shortest paths Dt Strutur LECTURE Shortt pth lgorithm Proprti of hortt pth Bllmn-For lgorithm Dijktr lgorithm Chptr in th txtook (pp ). Wight grph -- rminr A wight grph i grph in whih g hv wight (ot) w(v i, v j ) >.

More information

5/7/13. Part 10. Graphs. Theorem Theorem Graphs Describing Precedence. Outline. Theorem 10-1: The Handshaking Theorem

5/7/13. Part 10. Graphs. Theorem Theorem Graphs Describing Precedence. Outline. Theorem 10-1: The Handshaking Theorem Thorm 10-1: Th Hnshkin Thorm Lt G=(V,E) n unirt rph. Thn Prt 10. Grphs CS 200 Alorithms n Dt Struturs v V (v) = 2 E How mny s r thr in rph with 10 vrtis h of r six? 10 * 6 /2= 30 1 Thorm 10-2 An unirt

More information

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs Outlin Computr Sin 33 Computtion o Minimum-Cost Spnnin Trs Prim s Mik Joson Dprtmnt o Computr Sin Univrsity o Clry Ltur #34 Introution Min-Cost Spnnin Trs 3 Gnrl Constrution 4 5 Trmintion n Eiiny 6 Aitionl

More information

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review rmup CSE 7: AVL trs rmup: ht is n invrint? Mihl L Friy, Jn 9, 0 ht r th AVL tr invrints, xtly? Disuss with your nighor. AVL Trs: Invrints Intrlu: Exploring th ln invrint Cor i: xtr invrint to BSTs tht

More information

Constructive Geometric Constraint Solving

Constructive Geometric Constraint Solving Construtiv Gomtri Constrint Solving Antoni Soto i Rir Dprtmnt Llngutgs i Sistms Inormàtis Univrsitt Politèni Ctluny Brlon, Sptmr 2002 CGCS p.1/37 Prliminris CGCS p.2/37 Gomtri onstrint prolm C 2 D L BC

More information

Garnir Polynomial and their Properties

Garnir Polynomial and their Properties Univrsity of Cliforni, Dvis Dprtmnt of Mthmtis Grnir Polynomil n thir Proprtis Author: Yu Wng Suprvisor: Prof. Gorsky Eugny My 8, 07 Grnir Polynomil n thir Proprtis Yu Wng mil: uywng@uvis.u. In this ppr,

More information

Solutions for HW11. Exercise 34. (a) Use the recurrence relation t(g) = t(g e) + t(g/e) to count the number of spanning trees of v 1

Solutions for HW11. Exercise 34. (a) Use the recurrence relation t(g) = t(g e) + t(g/e) to count the number of spanning trees of v 1 Solutions for HW Exris. () Us th rurrn rltion t(g) = t(g ) + t(g/) to ount th numr of spnning trs of v v v u u u Rmmr to kp multipl gs!! First rrw G so tht non of th gs ross: v u v Rursing on = (v, u ):

More information

Problem solving by search

Problem solving by search Prolm solving y srh Tomáš voo Dprtmnt o Cyrntis, Vision or Roots n Autonomous ystms Mrh 5, 208 / 3 Outlin rh prolm. tt sp grphs. rh trs. trtgis, whih tr rnhs to hoos? trtgy/algorithm proprtis? Progrmming

More information

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology!

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology! Outlin Computr Sin 331, Spnnin, n Surphs Mik Joson Dprtmnt o Computr Sin Univrsity o Clry Ltur #30 1 Introution 2 3 Dinition 4 Spnnin 5 6 Mik Joson (Univrsity o Clry) Computr Sin 331 Ltur #30 1 / 20 Mik

More information

Present state Next state Q + M N

Present state Next state Q + M N Qustion 1. An M-N lip-lop works s ollows: I MN=00, th nxt stt o th lip lop is 0. I MN=01, th nxt stt o th lip-lop is th sm s th prsnt stt I MN=10, th nxt stt o th lip-lop is th omplmnt o th prsnt stt I

More information

The University of Sydney MATH2969/2069. Graph Theory Tutorial 5 (Week 12) Solutions 2008

The University of Sydney MATH2969/2069. Graph Theory Tutorial 5 (Week 12) Solutions 2008 Th Univrsity o Syny MATH2969/2069 Grph Thory Tutoril 5 (Wk 12) Solutions 2008 1. (i) Lt G th isonnt plnr grph shown. Drw its ul G, n th ul o th ul (G ). (ii) Show tht i G is isonnt plnr grph, thn G is

More information

N=4 L=4. Our first non-linear data structure! A graph G consists of two sets G = {V, E} A set of V vertices, or nodes f

N=4 L=4. Our first non-linear data structure! A graph G consists of two sets G = {V, E} A set of V vertices, or nodes f lulu jwtt pnlton sin towr ounrs hpl lpp lu Our irst non-linr t strutur! rph G onsists o two sts G = {V, E} st o V vrtis, or nos st o E s, rltionships twn nos surph G onsists o sust o th vrtis n s o G jnt

More information

ECE 407 Computer Aided Design for Electronic Systems. Circuit Modeling and Basic Graph Concepts/Algorithms. Instructor: Maria K. Michael.

ECE 407 Computer Aided Design for Electronic Systems. Circuit Modeling and Basic Graph Concepts/Algorithms. Instructor: Maria K. Michael. 0 Computr i Dsign or Eltroni Systms Ciruit Moling n si Grph Conptslgorithms Instrutor: Mri K. Mihl MKM - Ovrviw hviorl vs. Struturl mols Extrnl vs. Intrnl rprsnttions Funtionl moling t Logi lvl Struturl

More information

learning objectives learn what graphs are in mathematical terms learn how to represent graphs in computers learn about typical graph algorithms

learning objectives learn what graphs are in mathematical terms learn how to represent graphs in computers learn about typical graph algorithms rp loritms lrnin ojtivs loritms your sotwr systm sotwr rwr lrn wt rps r in mtmtil trms lrn ow to rprsnt rps in omputrs lrn out typil rp loritms wy rps? intuitivly, rp is orm y vrtis n s twn vrtis rps r

More information

Round 7: Graphs (part I)

Round 7: Graphs (part I) 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

More information

0.1. Exercise 1: the distances between four points in a graph

0.1. Exercise 1: the distances between four points in a graph Mth 707 Spring 2017 (Drij Grinrg): mitrm 3 pg 1 Mth 707 Spring 2017 (Drij Grinrg): mitrm 3 u: W, 3 My 2017, in lss or y mil (grinr@umn.u) or lss S th wsit or rlvnt mtril. Rsults provn in th nots, or in

More information

1 Introduction to Modulo 7 Arithmetic

1 Introduction to Modulo 7 Arithmetic 1 Introution to Moulo 7 Arithmti Bor w try our hn t solvin som hr Moulr KnKns, lt s tk los look t on moulr rithmti, mo 7 rithmti. You ll s in this sminr tht rithmti moulo prim is quit irnt rom th ons w

More information

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS OMPLXITY O OUNTING PLNR TILINGS Y TWO RS KYL MYR strt. W show tht th prolm o trmining th numr o wys o tiling plnr igur with horizontl n vrtil r is #P-omplt. W uil o o th rsults o uquir, Nivt, Rmil, n Roson

More information

(a) v 1. v a. v i. v s. (b)

(a) v 1. v a. v i. v s. (b) Outlin RETIMING Struturl optimiztion mthods. Gionni D Mihli Stnford Unirsity Rtiming. { Modling. { Rtiming for minimum dly. { Rtiming for minimum r. Synhronous Logi Ntwork Synhronous Logi Ntwork Synhronous

More information

12. Traffic engineering

12. Traffic engineering lt2.ppt S-38. Introution to Tltrffi Thory Spring 200 2 Topology Pths A tlommunition ntwork onsists of nos n links Lt N not th st of nos in with n Lt J not th st of nos in with j N = {,,,,} J = {,2,3,,2}

More information

Computational Biology, Phylogenetic Trees. Consensus methods

Computational Biology, Phylogenetic Trees. Consensus methods Computtionl Biology, Phylognti Trs Consnsus mthos Asgr Bruun & Bo Simonsn Th 16th of Jnury 2008 Dprtmnt of Computr Sin Th univrsity of Copnhgn 0 Motivtion Givn olltion of Trs Τ = { T 0,..., T n } W wnt

More information

Announcements. These are Graphs. This is not a Graph. Graph Definitions. Applications of Graphs. Graphs & Graph Algorithms

Announcements. These are Graphs. This is not a Graph. Graph Definitions. Applications of Graphs. Graphs & Graph Algorithms Grphs & Grph Algorithms Ltur CS Fll 5 Announmnts Upoming tlk h Mny Crrs o Computr Sintist Or how Computr Sin gr mpowrs you to o muh mor thn o Dn Huttnlohr, Prossor in th Dprtmnt o Computr Sin n Johnson

More information

Outline. Binary Tree

Outline. Binary Tree Outlin Similrity Srh Th Binry Brnh Distn Nikolus Austn nikolus.ustn@s..t Dpt. o Computr Sins Univrsity o Slzur http://rsrh.uni-slzur.t 1 Binry Brnh Distn Binry Rprsnttion o Tr Binry Brnhs Lowr Boun or

More information

QUESTIONS BEGIN HERE!

QUESTIONS BEGIN HERE! Points miss: Stunt's Nm: Totl sor: /100 points Est Tnnss Stt Univrsity Dprtmnt of Computr n Informtion Sins CSCI 710 (Trnoff) Disrt Struturs TEST for Fll Smstr, 00 R this for strtin! This tst is los ook

More information

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation A Simpl Co Gnrtor Co Gnrtion Chptr 8 II Gnrt o for singl si lok How to us rgistrs? In most mhin rhitturs, som or ll of th oprnsmust in rgistrs Rgistrs mk goo tmporris Hol vlus tht r omput in on si lok

More information

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS C 24 - COMBINATIONAL BUILDING BLOCKS - INVST 3 DCODS AND NCODS FALL 23 AP FLZ To o "wll" on this invstition you must not only t th riht nswrs ut must lso o nt, omplt n onis writups tht mk ovious wht h

More information

Numbering Boundary Nodes

Numbering Boundary Nodes Numring Bounry Nos Lh MBri Empori Stt Univrsity August 10, 2001 1 Introution Th purpos of this ppr is to xplor how numring ltril rsistor ntworks ffts thir rspons mtrix, Λ. Morovr, wht n lrn from Λ out

More information

(2) If we multiplied a row of B by λ, then the value is also multiplied by λ(here lambda could be 0). namely

(2) If we multiplied a row of B by λ, then the value is also multiplied by λ(here lambda could be 0). namely . DETERMINANT.. Dtrminnt. Introution:I you think row vtor o mtrix s oorint o vtors in sp, thn th gomtri mning o th rnk o th mtrix is th imnsion o th prlllppi spnn y thm. But w r not only r out th imnsion,

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spnning Trs Minimum Spnning Trs Problm A town hs st of houss nd st of rods A rod conncts nd only houss A rod conncting houss u nd v hs rpir cost w(u, v) Gol: Rpir nough (nd no mor) rods such tht:

More information

More Foundations. Undirected Graphs. Degree. A Theorem. Graphs, Products, & Relations

More Foundations. Undirected Graphs. Degree. A Theorem. Graphs, Products, & Relations Mr Funtins Grphs, Pruts, & Rltins Unirt Grphs An unirt grph is pir f 1. A st f ns 2. A st f gs (whr n g is st f tw ns*) Friy, Sptmr 2, 2011 Ring: Sipsr 0.2 ginning f 0.4; Stughtn 1.1.5 ({,,,,}, {{,}, {,},

More information

QUESTIONS BEGIN HERE!

QUESTIONS BEGIN HERE! Points miss: Stunt's Nm: Totl sor: /100 points Est Tnnss Stt Univrsity Dprtmnt o Computr n Inormtion Sins CSCI 2710 (Trno) Disrt Struturs TEST or Sprin Smstr, 2005 R this or strtin! This tst is los ook

More information

CMSC 451: Lecture 2 Graph Basics Thursday, Aug 31, 2017

CMSC 451: Lecture 2 Graph Basics Thursday, Aug 31, 2017 Dv Mount CMSC 45: Ltur Grph Bsis Thursy, Au, 07 Rin: Chpt. in KT (Klinr n Tros) n Chpt. in DBV (Dsupt, Ppimitriou, n Vzirni). Som o our trminoloy irs rom our txt. Grphs n Dirphs: A rph G = (V, E) is strutur

More information

Greedy Algorithms, Activity Selection, Minimum Spanning Trees Scribes: Logan Short (2015), Virginia Date: May 18, 2016

Greedy Algorithms, Activity Selection, Minimum Spanning Trees Scribes: Logan Short (2015), Virginia Date: May 18, 2016 Ltur 4 Gry Algorithms, Ativity Sltion, Minimum Spnning Trs Sris: Logn Short (5), Virgini Dt: My, Gry Algorithms Suppos w wnt to solv prolm, n w r l to om up with som rursiv ormultion o th prolm tht woul

More information

CS September 2018

CS September 2018 Loil los Distriut Systms 06. Loil los Assin squn numrs to msss All ooprtin prosss n r on orr o vnts vs. physil los: rport tim o y Assum no ntrl tim sour Eh systm mintins its own lol lo No totl orrin o

More information

1. Determine whether or not the following binary relations are equivalence relations. Be sure to justify your answers.

1. Determine whether or not the following binary relations are equivalence relations. Be sure to justify your answers. Mth 0 Exm - Prti Prolm Solutions. Dtrmin whthr or not th ollowing inry rltions r quivln rltions. B sur to justiy your nswrs. () {(0,0),(0,),(0,),(,),(,),(,),(,),(,0),(,),(,),(,0),(,),(.)} on th st A =

More information

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method:

GREEDY TECHNIQUE. Greedy method vs. Dynamic programming method: Dinition: GREEDY TECHNIQUE Gry thniqu is gnrl lgorithm sign strtgy, uilt on ollowing lmnts: onigurtions: irnt hois, vlus to in ojtiv untion: som onigurtions to ithr mximiz or minimiz Th mtho: Applil to

More information

Trees as operads. Lecture A formalism of trees

Trees as operads. Lecture A formalism of trees Ltur 2 rs s oprs In this ltur, w introu onvnint tgoris o trs tht will us or th inition o nroil sts. hs tgoris r gnrliztions o th simpliil tgory us to in simpliil sts. First w onsir th s o plnr trs n thn

More information

CSI35 Chapter 11 Review

CSI35 Chapter 11 Review 1. Which of th grphs r trs? c f c g f c x y f z p q r 1 1. Which of th grphs r trs? c f c g f c x y f z p q r . Answr th qustions out th following tr 1) Which vrtx is th root of c th tr? ) wht is th hight

More information

In order to learn which questions have been answered correctly: 1. Print these pages. 2. Answer the questions.

In order to learn which questions have been answered correctly: 1. Print these pages. 2. Answer the questions. Crystl Rports for Visul Stuio.NET In orr to lrn whih qustions hv n nswr orrtly: 1. Print ths pgs. 2. Answr th qustions. 3. Sn this ssssmnt with th nswrs vi:. FAX to (212) 967-3498. Or. Mil th nswrs to

More information

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas SnNCutCnvs Using th Printl Stikr Funtion On-o--kin stikrs n sily rt y using your inkjt printr n th Dirt Cut untion o th SnNCut mhin. For inormtion on si oprtions o th SnNCutCnvs, rr to th Hlp. To viw th

More information

Graph Theory. Vertices. Vertices are also known as nodes, points and (in social networks) as actors, agents or players.

Graph Theory. Vertices. Vertices are also known as nodes, points and (in social networks) as actors, agents or players. Stphn P. Borgtti Grph Thory A lthough grph thory is on o th youngr rnhs o mthmtis, it is unmntl to numr o ppli ils, inluing oprtions rsrh, omputr sin, n soil ntwork nlysis. In this hptr w isuss th si onpts

More information

NP-Completeness. CS3230 (Algorithm) Traveling Salesperson Problem. What s the Big Deal? Given a Problem. What s the Big Deal? What s the Big Deal?

NP-Completeness. CS3230 (Algorithm) Traveling Salesperson Problem. What s the Big Deal? Given a Problem. What s the Big Deal? What s the Big Deal? NP-Compltnss 1. Polynomil tim lgorithm 2. Polynomil tim rution 3.P vs NP 4.NP-ompltnss (som slis y P.T. Um Univrsity o Txs t Dlls r us) Trvling Slsprson Prolm Fin minimum lngth tour tht visits h ity on

More information

arxiv: v1 [cs.ds] 20 Feb 2008

arxiv: v1 [cs.ds] 20 Feb 2008 Symposium on Thortil Aspts of Computr Sin 2008 (Borux), pp. 361-372 www.sts-onf.org rxiv:0802.2867v1 [s.ds] 20 F 2008 FIXED PARAMETER POLYNOMIAL TIME ALGORITHMS FOR MAXIMUM AGREEMENT AND COMPATIBLE SUPERTREES

More information

Register Allocation. Register Allocation. Principle Phases. Principle Phases. Example: Build. Spills 11/14/2012

Register Allocation. Register Allocation. Principle Phases. Principle Phases. Example: Build. Spills 11/14/2012 Rgistr Allotion W now r l to o rgistr llotion on our intrfrn grph. W wnt to l with two typs of onstrints: 1. Two vlus r liv t ovrlpping points (intrfrn grph) 2. A vlu must or must not in prtiulr rhitturl

More information

10/30/12. Today. CS/ENGRD 2110 Object- Oriented Programming and Data Structures Fall 2012 Doug James. DFS algorithm. Reachability Algorithms

10/30/12. Today. CS/ENGRD 2110 Object- Oriented Programming and Data Structures Fall 2012 Doug James. DFS algorithm. Reachability Algorithms 0/0/ CS/ENGRD 0 Ojt- Orint Prormmin n Dt Strutur Fll 0 Dou Jm Ltur 9: DFS, BFS & Shortt Pth Toy Rhility Dpth-Firt Srh Brth-Firt Srh Shortt Pth Unwiht rph Wiht rph Dijktr lorithm Rhility Alorithm Dpth Firt

More information

Strongly connected components. Finding strongly-connected components

Strongly connected components. Finding strongly-connected components Stronly onnt omponnts Fnn stronly-onnt omponnts Tylr Moor stronly onnt omponnt s t mxml sust o rp wt rt pt twn ny two vrts SE 3353, SMU, Dlls, TX Ltur 9 Som sls rt y or pt rom Dr. Kvn Wyn. For mor normton

More information

CMPS 2200 Fall Graphs. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CMPS 2200 Fall Graphs. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 2200 Fll 2017 Grps Crol Wnk Sls ourtsy o Crls Lsrson wt ns n tons y Crol Wnk 10/23/17 CMPS 2200 Intro. to Alortms 1 Grps Dnton. A rt rp (rp) G = (V, E) s n orr pr onsstn o st V o vrts (snulr: vrtx),

More information

Graphs and Graph Searches

Graphs and Graph Searches Graphs an Graph Sarhs CS 320, Fall 2017 Dr. Gri Gorg, Instrutor gorg@olostat.u 320 Graphs&GraphSarhs 1 Stuy Ais Gnral graph nots: Col s Basi Graph Nots.pf (Progrss pag) Dpth first gui: Dpth First Sarh

More information

16.unified Introduction to Computers and Programming. SOLUTIONS to Examination 4/30/04 9:05am - 10:00am

16.unified Introduction to Computers and Programming. SOLUTIONS to Examination 4/30/04 9:05am - 10:00am 16.unii Introution to Computrs n Prormmin SOLUTIONS to Exmintion /30/0 9:05m - 10:00m Pro. I. Kristin Lunqvist Sprin 00 Grin Stion: Qustion 1 (5) Qustion (15) Qustion 3 (10) Qustion (35) Qustion 5 (10)

More information

Chapter 18. Minimum Spanning Trees Minimum Spanning Trees. a d. a d. a d. f c

Chapter 18. Minimum Spanning Trees Minimum Spanning Trees. a d. a d. a d. f c Chptr 8 Minimum Spnning Trs In this hptr w ovr importnt grph prolm, Minimum Spnning Trs (MST). Th MST o n unirt, wight grph is tr tht spns th grph whil minimizing th totl wight o th gs in th tr. W irst

More information

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski Dut with Dimons Brlt DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Lsli Roglski Photo y Anrw Wirth Supruo DUETS TM from BSmith rt olor shifting fft tht mks your work tk on lif of its own s you mov! This

More information

Lecture 20: Minimum Spanning Trees (CLRS 23)

Lecture 20: Minimum Spanning Trees (CLRS 23) Ltur 0: Mnmum Spnnn Trs (CLRS 3) Jun, 00 Grps Lst tm w n (wt) rps (unrt/rt) n ntrou s rp voulry (vrtx,, r, pt, onnt omponnts,... ) W lso suss jny lst n jny mtrx rprsntton W wll us jny lst rprsntton unlss

More information

Chapter 9. Graphs. 9.1 Graphs

Chapter 9. Graphs. 9.1 Graphs Chptr 9 Grphs Grphs r vry gnrl lss of ojt, us to formliz wi vrity of prtil prolms in omputr sin. In this hptr, w ll s th sis of (finit) unirt grphs, inluing grph isomorphism, onntivity, n grph oloring.

More information

Multipoint Alternate Marking method for passive and hybrid performance monitoring

Multipoint Alternate Marking method for passive and hybrid performance monitoring Multipoint Altrnt Mrkin mtho or pssiv n hyri prormn monitorin rt-iool-ippm-multipoint-lt-mrk-00 Pru, Jul 2017, IETF 99 Giuspp Fiool (Tlom Itli) Muro Coilio (Tlom Itli) Amo Spio (Politnio i Torino) Riro

More information

Plan. I Gale-Shapley Running Time. I Graphs. I Motivation and definitions I Graph traversal: BFS and DFS

Plan. I Gale-Shapley Running Time. I Graphs. I Motivation and definitions I Graph traversal: BFS and DFS Pln CS : Grphs: BFS n DFS Dn Shlon Gl-Shply Running Tim Grphs Motivtion n finitions Grph trvrsl: BFS n DFS Frury, 0 Running Tim of Gl-Shply? nitilly ll ollgs n stunts r fr whil som ollg is fr n hsn t m

More information

This chapter covers special properties of planar graphs.

This chapter covers special properties of planar graphs. Chptr 21 Plnr Grphs This hptr ovrs spil proprtis of plnr grphs. 21.1 Plnr grphs A plnr grph is grph whih n b rwn in th pln without ny gs rossing. Som piturs of plnr grph might hv rossing gs, but it s possibl

More information

Aquauno Video 6 Plus Page 1

Aquauno Video 6 Plus Page 1 Connt th timr to th tp. Aquuno Vio 6 Plus Pg 1 Usr mnul 3 lik! For Aquuno Vio 6 (p/n): 8456 For Aquuno Vio 6 Plus (p/n): 8413 Opn th timr unit y prssing th two uttons on th sis, n fit 9V lklin ttry. Whn

More information

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths.

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths. How os it work? Pl vlu o imls rprsnt prts o whol numr or ojt # 0 000 Tns o thousns # 000 # 00 Thousns Hunrs Tns Ons # 0 Diml point st iml pl: ' 0 # 0 on tnth n iml pl: ' 0 # 00 on hunrth r iml pl: ' 0

More information

Spanning Trees. BFS, DFS spanning tree Minimum spanning tree. March 28, 2018 Cinda Heeren / Geoffrey Tien 1

Spanning Trees. BFS, DFS spanning tree Minimum spanning tree. March 28, 2018 Cinda Heeren / Geoffrey Tien 1 Spnnn Trs BFS, DFS spnnn tr Mnmum spnnn tr Mr 28, 2018 Cn Hrn / Gory Tn 1 Dpt-rst sr Vsts vrts lon snl pt s r s t n o, n tn ktrks to t rst junton n rsums own notr pt Mr 28, 2018 Cn Hrn / Gory Tn 2 Dpt-rst

More information

Formal Concept Analysis

Formal Concept Analysis Forml Conpt Anlysis Conpt intnts s losd sts Closur Systms nd Implitions 4 Closur Systms 0.06.005 Nxt-Closur ws dvlopd y B. Gntr (984). Lt M = {,..., n}. A M is ltilly smllr thn B M, if B A if th smllst

More information

WORKSHOP 6 BRIDGE TRUSS

WORKSHOP 6 BRIDGE TRUSS WORKSHOP 6 BRIDGE TRUSS WS6-2 Workshop Ojtivs Lrn to msh lin gomtry to gnrt CBAR lmnts Bom fmilir with stting up th CBAR orinttion vtor n stion proprtis Lrn to st up multipl lo ss Lrn to viw th iffrnt

More information

Graph Contraction and Connectivity

Graph Contraction and Connectivity Chptr 17 Grph Contrtion n Conntivity So r w hv mostly ovr thniqus or solving prolms on grphs tht wr vlop in th ontxt o squntil lgorithms. Som o thm r sy to prllliz whil othrs r not. For xmpl, w sw tht

More information

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, *

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, * CmSc 365 Thory of Computtion Finit Stt Automt nd Rgulr Exprssions (Chptr 2, Sction 2.3) ALPHABET oprtions: U, conctntion, * otin otin Strings Form Rgulr xprssions dscri Closd undr U, conctntion nd * (if

More information

Quartets and unrooted level-k networks

Quartets and unrooted level-k networks Phylogntis Workshop, Is Nwton Institut or Mthmtil Sins Cmrig 21/06/2011 Qurtts n unroot lvl-k ntworks Philipp Gmtt Outlin Astrt n xpliit phylognti ntworks Lvl-k ntworks Unroot lvl-1 ntworks n irulr split

More information

Analysis for Balloon Modeling Structure based on Graph Theory

Analysis for Balloon Modeling Structure based on Graph Theory Anlysis for lloon Moling Strutur bs on Grph Thory Abstrt Mshiro Ur* Msshi Ym** Mmoru no** Shiny Miyzki** Tkmi Ysu* *Grut Shool of Informtion Sin, Ngoy Univrsity **Shool of Informtion Sin n Thnology, hukyo

More information

Page 1. Question 19.1b Electric Charge II Question 19.2a Conductors I. ConcepTest Clicker Questions Chapter 19. Physics, 4 th Edition James S.

Page 1. Question 19.1b Electric Charge II Question 19.2a Conductors I. ConcepTest Clicker Questions Chapter 19. Physics, 4 th Edition James S. ConTst Clikr ustions Chtr 19 Physis, 4 th Eition Jms S. Wlkr ustion 19.1 Two hrg blls r rlling h othr s thy hng from th iling. Wht n you sy bout thir hrgs? Eltri Chrg I on is ositiv, th othr is ngtiv both

More information

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata CSE303 - Introduction to th Thory of Computing Smpl Solutions for Exrciss on Finit Automt Exrcis 2.1.1 A dtrministic finit utomton M ccpts th mpty string (i.., L(M)) if nd only if its initil stt is finl

More information

LEO VAN IERSEL TU DELFT

LEO VAN IERSEL TU DELFT LEO VAN IERSEL TU DELFT UT LEO VAN IERSEL TU DELFT UT LEO VAN IERSEL TU DELFT TU/ CWI UT LEO VAN IERSEL TU DELFT TU/ CWI UT TUD LEO VAN IERSEL TU DELFT TU/ Tnzni & Kny yr LEO VAN IERSEL TU DELFT Nw Zln.5

More information

EE1000 Project 4 Digital Volt Meter

EE1000 Project 4 Digital Volt Meter Ovrviw EE1000 Projt 4 Diitl Volt Mtr In this projt, w mk vi tht n msur volts in th rn o 0 to 4 Volts with on iit o ury. Th input is n nlo volt n th output is sinl 7-smnt iit tht tlls us wht tht input s

More information

Section 3: Antiderivatives of Formulas

Section 3: Antiderivatives of Formulas Chptr Th Intgrl Appli Clculus 96 Sction : Antirivtivs of Formuls Now w cn put th is of rs n ntirivtivs togthr to gt wy of vluting finit intgrls tht is ct n oftn sy. To vlut finit intgrl f(t) t, w cn fin

More information