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 o rs to vry stunt o Choos suh ollg Lt s th highst rnk stunt to whom hs not m n o r if s is fr thn n s om ngg ls if s is ngg to 0 ut prfrs to 0 thn 0 oms fr n s om ngg ls rmins fr n if n whil Dt Struturs Running-tim pns on implmnttion tils n t struturs (.g. how to hoos suh ollg ). Q: How shoul w think out t struturs whn signing lgorithms? A: Most of th tim, s lk oxs with running-tim gurnts (.g., fin n lmnt in O(log n) tim ). Goo nws: on t n to rmmr tils of t struturs B nws: thy my sm opqu O(n ) itrtions. Ar ll sttmnts insi th loop onstnt tim? Rviw: Lists n Arrys Wht Dt Struturs to Us For G-S? Arry List Gt ith ntry O() O(i) Fin lmnt O(n) O(log n) if sort O(n) nsrt/lt O(n) O() N to o th following in O() tim Fin fr ollg Fin nxt stunt s in prfrn list of Fin urrnt ollg 0 of s Chk if s liks 0 ttr thn Not: O() = onstnt numr of stps
Wht Dt Struturs to Us For G-S? Anothr Exmpl: Hpsort nput: prfn lists = D rrys,.g. CollgPrf[, i] = stunt in position i on s list Oprtion Dt strutur Fn fr ollg link list: frcollgs Fin nxt stunt s in prfrn list of rry i = Nxt[] s = CollgPrf[, i] Fin urrnt ollg 0 of s -D rry: Currnt[s] Chk if s liks 0 ttr thn nput unsort rry A[] of lngth n Lt Q hp-s priority quu for i = to n o nsrt(q, A[i]) n for for i = to n o A[i] = ExtrtMin(Q) n for Running tim: (n nsrt) + (n ExtrtMin) -D rry: Rnking[s,] O(n log n) if oth oprtions r O(log n) xmpl on or Grphs Unirt Grph Motivtion n Dfinitions Brth-First Srh (BFS) n Dpth-First Srh (DFS) Unirt grph. G = (V, E)! V = nos (vrtis)! E = gs twn pirs of nos.! Cpturs pirwis rltionship twn ojts.! Grph siz prmtrs: n = V, m = E. V = {,,,, }! E = {(,), (,), (,), (,), (,),! (,)}! n=! m=6 Grphs Four Dgrs of Sprtion Four Dgrs of Sprtion Lrs Bkstrom Boli Mro Ros John Ugnr Lrs Bkstrom Polo Boli Mro RosPolo John Ugnr Sstino Vign Jnury 6, 0 Fook: how mny grs of sprtion twn m n Brk Om? An mny mor... stuying th istn istriution of vry lrg grphs: istriution HyAstrt stuying th istn of v pranf []. Builing on prviouspranf grph omprssion [] work []. Builing on prvious grph Frigys Krinthy, in his 99 shortkrinthy, story Lánszmk on th i Lánszmk of iffusiv omputtion pionr [], omputti Frigys in his 99n short story n on th i of in iffusiv ( Chins ) suggst tht ny( Chins ) two prsons r istn y two th prsons nw toolr m it possil urtly suggst tht ny istn y toth nw toolstuy mth it ispossil to u t most six frinship links. t Stnly in his links. fmous tn istriution orrs of mgnitu lrgr thn orrs of m most Milgrm six frinship Stnly Milgrm inofhisgrphs fmous tn istriution of grphs xprimnt [0, ] hllngxprimnt popl to rout postrs to it ws prviously possil. [0, ] hllng popl to rout postrs to it ws prviously possil. fix ripint y pssing thm onlyripint through y irt quinonthrough of th gols in stuying istriution th fix pssing thm only irt quin-th istn On of th gols in is stuying th ist tns. Th vrg numr tns. of intrmiris on th pth ofintifition of intrsting sttistil prmtrs tht n sttistil Th vrg numr intrmiris on th pth intifition of intrsting of th postrs ly twn of.th npostrs.7, pning on th. n us.7, to pning tll propr on soil from othr omplx ly twn thntworks us to tll propr soil ntworks smpl of popl hosn. ntworks, suh s w grphs. Mor gnrlly, th istn smpl of popl hosn. ntworks, suh s w grphs. Mor g W rport th rsults of th first worl-sl soil-ntwork istriution is on intrsting glol ftur tht mks it W rport th rsults of th first worl-sl soil-ntwork istriution is on intrsting glol f grph-istn omputtions, using th ntir Fook nt- possil to rjt proilisti mols vn whn thy mth grph-istn omputtions, using th ntir Fook ntpossil to rjt proilisti mols work of tiv usrs ( 7 million usrs, 69 illion frin- lol fturs suh s th in-gr istriution. work of tiv usrs ( 7 million usrs, 69 illion frinlol fturs suh s th in-gr ist ship links). Th vrg istn w osrv is.7, orn prtiulr, rlir work h shown tht th spi, ship or links). Th vrg istn w osrv is.7, orn istn prtiulr, rlir work h sh rsponing to.7 intrmiris grs of sprtion, whih msurs th isprsion of th istriution, rsponing to.7 intrmiris or grs of sprtion, whih msurs thfor isprsion of th showing tht th worl is vn smllr thn w xpt, n ppr to smllr thn (unrisprsion) soshowing tht th worl vn smllr thn w xpt, n ppr to smllr prompting th titl of this ppr. Mor gnrlly, w isstuy il ntworks, ut lrgr thn on (ovrisprsion) for wthn (un prompting th titl of this ppr. Mor gnrlly, w stuy th istn istriution of Fook n of som intrst- grphs []. Hn, uring th tlk, il ntworks, lrgr on of thut min opnthn on (ov th istn istriution of Fook n som is intrsting gogrphi sugrphs, looking lso t thir volution ovr grphs []. Hn, uring th tlk, o qustions wsof Wht th spi of Fook?. ing gogrphi sugrphs, looking Lrs lso t thir volution ovrto listn tim. qustions wstlk, Wht th spi of F Bkstrom hppn to th n issugth ntworks w r l totim. xplor r lmost two orrs gst ollortion stuying thlrs Bkstrom to listn Fook grph.hppn This of mgnitu lrgr thn thos nlys in th prvious litr-to xplor Th ntworks w r l r lmost two orrs ws of ours n xtrmly intriguing possiility: si tstgst ollortion stuying th F tur. W rport til sttistil mtt showing tht nlys of mgnitu lrgr thn thos in th prvious omputing litr- wsth ing th spi hypothsis, istnnistriution of ours xtrmly intriguing po our msurmnts (whih rly on proilisti lgorithms) tur. W rport til sttistil showing ththving of th mtt Fook grph woul n lrgst Milgrm-omputing th thth spi hypothsis, r vry urt. our msurmnts (whih rly lik on proilisti lgorithms) [0] xprimnt vr prform, of mgnitus of thorrs Fook grph woul hv n lrgr thn prvious ttmpts (uring our xprimnts r vry urt. lik [0] xprimnt Fvr prform, o ook hs 7 million tiv usrs nthn 69prvious illion frinlrgr ttmpts (uring o ntroution ship links). ook hs 7 million tiv usrs n Astrt v:.70v [s.s] Jn 0 Googl Mps: wht is th shortst riving rout from South Hly to Flori? Xiv:.70v [s.s] Jn 0 Sstin Jnury 6, 0
Trminology Dfinitions f = (u, v) is n g, thn: () u is nighor of v () u is jnt to v () is inint on u n v () u n v r th npoints of is jnt to! (,) is inint on n Pth Distn A pth is squn P of nos v, v,, v k-, v k with th proprty tht h onsutiv pir v i, v i+ is join y n g in E. Th istn from u to v is th minimum numr of gs in ny pth from u to v -- is pth.! -- is NOT pth. istn(,) =! istn(,) = Cyl A yl is pth v, v,, v k-, v k in whih v = v k, k >, n th first k- nos r ll istint. Conntivity An unirt grph is onnt if for vry pir of nos u n v, thr is pth twn u n v. --- is yl.! -- is NOT yl.! ---- is NOT yl.! ------- is NOT yl. is onnt grph. is NOT onnt grph.
Prnts, snnts, nstors? (Upsi-own) Trs Trs A tr is n unirt grph tht is onnt n os not ontin yl. is tr is NOT tr http://www.offttrvl.om/moca.html Rviw Dfinitions Grph Trvrsl s grph onnt? Wht to know: n, m, nighor, inint, pth, istn, yl, onnt, tr! Exmpl on or sy Grph Trvrsl hmmm... s Grph Connt? Algorithm : Brth-first srh (BFS) Explor outwr y istn s grph onnt?! Approh: xplor outwr from ritrry strting no s to fin ll nos rhl from s (onnt omponnt) Strt t : Visit ll nos t istn from : Visit ll nos t istn from :
Brth-First Srh BFS Tr Lyrs L0 = {s} L = ll nighors of L0 L = ll nos with n g to L tht on t long to L0 or L... Li+ = nos with n g to Li tht on t long to n rlir lyr: f w kp only th gs trvrs whil oing rth-first srh, w will hv tr Exmpl on or Li+ = {v : 9(u, v) E, u Li, v / (L0 [... [ Li )} Osrvtion: Li onsists of ll nos t istn xtly i from s. Thr is pth from s to t if n only if t pprs in som lyr. A Mor Gnrl Strtgy BFS Tr Proprty. Lt T BFS tr of G = (V, E), n lt (x, y) n g of G. Thn th lyr of x n y iffr y t most. Lyr 0: {}! Lyr : {,, }! Lyr : {} To xplor th onnt omponnt, ny no v for whih (u, v) is n g u is xplor, ut v is not Pitur on or Proof on or DFS Algorithm s Grph Connt? Algorithm : Dpth-first srh (DFS) - Kp xploring from most rntly no until you hv to ktrk DFS(u) Mrk u s Explor for h g (u, v) inint to u o if v is not mrk Explor thn Rursivly invok DFS(v) n if n for
Dpth First Srh Thorm: Lt T pth-first srh tr. Lt x n y nos in th tr. Lt (x, y) n g tht is in G ut not in T. Thn ithr x is n nstor of y or y is n nstor of x in T. Summry Dfinitions G =(V,E),n= V,m= E nighor, inint, yl, pth, onnt BFS n DFS Two wys to trvrs grph, h prous tr BFS tr: shllow n wi ( ushy ) DFS tr: p n nrrow ( srggly ) Proof?