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 gs Somtims w wnt to rprsnt irtionlity: n Uniirtionl ntwork onntions n On wy strts n Th w http://nousls.om/wp-ontnt/uplos/2/2/ figur--mning-irultion.png http://lin-r-th-inking.logspot.om/2/2/ visulizing-goti-informtion-with.html CS2 - Hsh Tls
Grph Trminology Grph Trminology G=(V, E) Vrtis Egs Egs Two vrtis r jnt if thy r onnt y n g. An g is inint on two vrtis Dgr of vrtx: numr of gs inint on it A sugrph of grph G = (V,E) is grph (V,E ) suh tht V is sust of V n, E is sust of E sugrph u Vrtis/ Nos v Grph trminology: 4. in Prihr,. in Rosn Pths Grph Trminology n Pth: squn of gs Slf loop (loop): n g tht onnts vrtx to itslf v v 2 2 v 4 n (, 2, 3 ) is pth of lngth 3 from v to v 4 n In simpl grph pth n rprsnt s squn of vrtis Simpl grph: no slf loops n no two gs onnt th sm vrtis Multigrph: my hv multipl gs onnting th sm vrtis v 3 3 Psuogrph: multigrph with slf-loops 2
Complt Grphs n Simpl grph tht ontins xtly on g twn h pir of istint vrtis. Cyls Th yl C n, n 3, onsists of n vrtis v, v 2,, v n n gs {v, v 2 }, {v 2, v 3 },., n {v n-, v n }. Complt Grph Whls n-cus (n-imnsionl hypru) n W otin th whl W n whn w n itionl vrtx to th yl C n, for n 3, n onnt this nw vrtx to h of th n vrtis in C n, y nw gs Hypru 2 3
Th gr of vrtx Exmpl n Th gr of vrtx in n unirt grph q th numr of gs inint with it q xpt tht loop t vrtx ontriuts twi to th gr of tht vrtx. f g 4 g() = 2 g() = g(f) = 4 g() = g() = 3 g(g) = Som Grph Thorms Biprtit Grphs n Hnshking: Lt G=(V,E) n unirt grph with gs. Thn 2 = v V g(v) n An unirt grph hs n vn numr of vrtis of o gr. n Lt G=(V,E) grph with irt gs. Thn g (v) = g + (v) = E v V v V n n n A simpl grph on whih th vrtx st V n prtition into two isjoint sts V n V2 suh tht vry g onnts vrtx in V to on in V2. Biprtit? f g Thorm: A simpl grph is iprtit iff it is possil to ssign on of two iffrnt olors to h vrtx of th grph so tht no two jnt vrtis r ssign th sm olor. f 4
Dirt Grphs w Ingr: numr of inoming gs Outgr: numr of outgoing gs n n Connt Componnts An unirt grph is ll onnt if thr is pth twn vry pir of vrtis of th grph. A onnt omponnt of grph G is onnt sugrph of G tht is not propr sugrph of nothr onnt sugrph of G. v G={{,,,,,f,g},E} G ={{,,},E } G 2 ={{,,f,g}, E 2 } f g Grph ADT n Crt n Empty? n Numr of vrtis? n Numr of gs? n Eg xists twn two vrtis? n A vrtx n A n g twn two vrtis n Dlt vrtx (n ny onnt gs) n Dlt th g twn two vrtis n Rtriv vrtx Clsss for Unirt Grph with Instn Vrils n Eg: q vrtx, vrtx2 q wight n Grph: q Numr of gs, numr of vrtis q orgniz olltion of vrtis n gs
Grph Dt Struturs - Ajny Mtrix n Vrtis q lls mpp into inis q on vrtx mpp to on inx q Vlus: n ooln to init prsn/sn of g in (un)irt grph n int to init vlu of wight g n Egs q squr mtrix of gs n siz = numr of vrtis n g: two (vrtx) inis n usful for ns grphs Ajny Mtrix Exmpl Ll Inx A B C 2 D 3 E 4 mpping of vrtx lls to rry inis B D A E C 2 3 4 2 3 4 Ajny Mtrix: rry of gs inx y vrtx Grph Dt Struturs - Ajny List Ajny List Exmpl n Vrtis q mpp to list of jnis q jny: g n Egs: lists of jnis q link-list of out-going gs pr vrtx n usful for sprs grphs B D A E C B D A E C Inx A B 2 C 3 D 4 E Ll B E A B C D mpping of vrtx lls to list of gs 6
Whih Implmnttion Is Bst? Implmnttion: Eg Clss n Whih implmnttion st supports ommon Grph Oprtions: q Is thr n g twn vrtx i n vrtx j? q Fin ll vrtis jnt to vrtx j n Whih st uss sp? Clss Eg { privt Intgr v,w; // vrtis privt int wight; puli Eg(Intgr first, Intgr son, int gwight){ v = first; w = son; wight = gwight; } puli int gtwight() { rturn wight; } puli Intgr gtv() { rturn v; } puli Intgr gtw() { rturn w; } Implmnttion: Grph Clss lss Grph { privt int numvrtis; privt int numegs; privt Vtor<TrMp<Intgr, Intgr>> jlist; puli Grph(int n) { numvrtis = n; numegs = ; jlist = nw Vtor<TrMp<Intgr, Intgr>>(); for (int i=; i<numvrtis; i++) { jlist.(nw TrMp<Intgr, Intgr>()); } } Implmnttion: Grph Clss Mthos puli int gtnumvrtis() puli int gtnumegs() puli int gtegwight(intgr v, Intgr w) puli voi Eg(Intgr v, Intgr w) puli voi Eg(Eg ) puli voi rmoveg(eg ) puli Eg fineg(intgr v, Intgr w)
Six Dgrs of Kvin Bon mg ryn john mlkovih pollo 3 tom hnks lint stwoo kvin on lur linny jk niholson morgn frmn mihll pfiffr n Ator x hs Kvin Bon Numr of n if th shortst pth twn x n Kvin Bon hs lngth n Shortst Pth Algorithms (Dijkstr s Algorithm) n Grph G(V,E) with non-ngtiv wights ( istns ) n Comput shortst istns from vrtx s to vry othr vrtx in th grph Grph m with th hlp of th orl of Bon: http://orlofon.org/ Shortst Pth Algorithms (Dijkstr s Algorithm) Shortst Pth Algorithms (Dijkstr s Algorithm) n Algorithm q Mintin rry (minimum istn stimts) n Init: [s]=, [v]= v V-s q Priority quu of vrtis not yt visit q slt minimum istn vrtx, visit v, upt nighors n Intrtiv Dijkstr s lgorithm: q http://stunts.i.uptrs.gr/~ppgl/projt/ kf.htm 2
Shortst Pth Algorithms (Dijkstr s Algorithm): Initiliz Shortst Pth Algorithms (Dijkstr s Algorithm): stp / / -- -- 2 2 Shortst Pth Algorithms (Dijkstr s Algorithm): stp 2 / / -- -- / / / 4/ 4 Shortst Pth Algorithms (Dijkstr s Algorithm): stp 3 / / -- -- / / / 4/ / / / / 2 2
Shortst Pth Algorithms (Dijkstr s Algorithm): stp 4 / / -- -- / / / 4/ / / / / / / / / Shortst Pth Algorithms (Dijkstr s Algorithm): Don / / / / 2 2 Minimum istn from to : Minimum istn from to : Minimum istn from to : Minimum istn from to : Exmpl Dijkstr s Algorithm Dijkstr(G: grph with vrtis v v n- n wights w[u][v])!!// omputs shortst istn of vrtx to vry othr vrtx!!rt st vrtxst tht ontins only vrtx!![] =!!for (v = through n-)!!![v] = infinity!!for (stp = 2 through n)!!!fin th smllst [v] suh tht v is not in vrtxst!!! v to vrtxst!!!for (ll vrtis u not in vrtxst)!!!!if ([u] > [v] + w[v][u])!!!!![u] = [v] + w[v][u]! 4
Shortst Pth Algorithms Using Priority Quu (Dijkstr s Algorithm): stp Shortst Pth Algorithms (Dijkstr s Algorithm): stp 2 4 [,],[,] 2 [,],[,],[4,] 2 Shortst Pth Algorithms (Dijkstr s Algorithm): stp 3 Shortst Pth Algorithms (Dijkstr s Algorithm): stp 4 [,],[,] 2 [,] 2
Shortst Pth Algorithms (Dijkstr s Algorithm): Don Dijkstr s Algorithm n How to otin th shortst pths? q At h vrtx mintin pointr tht tlls you th vrtx from whih you rriv. 2 2