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 Routing n shortst pth prolms Commoity istriution (low) ri ontrol Rsour llotion Gomtri moling... Grphs n us to rprsnt mny things Grph Dinitions A irt grph (or igrph) is pir (V,E) whr V is st E is st o orr pirs (u,v) whr u,v V Usully rquir u v (no sl-loops) An lmnt o V is ll vrtx (pl. vrtis) or no An lmnt o E is ll n g or r V = siz o V, otn not n E = siz o E, otn not m grph lgorithms hv mny pplitions
Exmpl Dirt Grph Exmpl Unirt Grph Exmpl: An unirt grph is just lik irt grph, xpt th gs r unorr pirs (sts) {u,v V = {,,,,, E = { (,), (,), (,), (,), (,), (,), (,), (,), (,), (,), (,) V = 6, E = Exmpl: V = {,,,,, E = { {,, {,, {,, {,, {,, {,, {,, {,, {,, {,, {, Som Grph rminology Mor Grph rminology Vrtis u n v r ll th sour n sink o th irt g (u,v), rsptivly Vrtis u n v r ll th npoints o (u,v) wo vrtis r jnt i thy r onnt y n g h outgr o vrtx u in irt grph is th numr o gs or whih u is th sour h ingr o vrtx v in irt grph is th numr o gs or whih v is th sink h gr o vrtx u in n unirt grph is th numr o gs o whih u is n npoint v v 5 A pth is squn v,v,v,...,v p o vrtis suh tht (v i,v i+ ) E, i p h lngth o pth is its numr o gs In this xmpl, th lngth is 5 A pth is simpl i it os not rpt ny vrtis A yl is pth v,v,v,...,v p suh tht v = v p A yl is simpl i it os not rpt ny vrtis xpt th irst n lst A grph is yli i it hs no yls A irt yli grph is ll g Is this g? Is this g? his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs
Is this g? Is this g? his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs Is this g? Is this g? his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs Is this g? Is this g? his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs
Is this g? opologil Sort Just omput topologil sort o th g A numring o th vrtis suh tht ll gs go rom lowr- to highr-numr vrtis his i ls to n lgorithm vrtis until th grph ispprs Dg is lik tr ut nos n hv multipl prnts : : : :4 : :5 4 A g: gnrliztion o tr w/ multipl prnts Usul in jo shuling with onstrints 5 Grph Coloring A oloring o n unirt grph is n ssignmnt o olor to h no suh tht no two jnt vrtis gt th sm olor Grph Coloring A oloring o n unirt grph is n ssignmnt o olor to h no suh tht no two jnt vrtis gt th sm olor How mny olors r n to olor this grph? How mny olors r n to olor this grph? An Applition o Coloring Vrtis r jos Eg (u,v) is prsnt i jos u n v h rquir ss to th sm shr rsour, n thus nnot xut simultnously Colors r tim slots to shul th jos Minimum numr o olors n to olor th grph = minimum numr o tim slots rquir Plnrity A grph is plnr i it n m in th pln with no gs rossing Is this grph plnr? 4
Plnrity A grph is plnr i it n m in th pln with no gs rossing Plnrity A grph is plnr i it n m in th pln with no gs rossing Is this grph plnr? Ys Is this grph plnr? Ys Dtting Plnrity Kurtowski's horm K 5 K, A grph is plnr i n only i it os not ontin opy o K 5 or K, (possily with othr nos long th gs shown) h Four-Color horm Evry plnr grph is 4-olorl (Appl & Hkn, 976) Biprtit Grphs A irt or unirt grph is iprtit i th vrtis n prtition into two sts suh tht ll gs go twn th two sts Cn rprsnt rltionship twn two sts,.g. is tking twn stunts n lsss Biprtit Grphs h ollowing r quivlnt G is iprtit G is -olorl G hs no yls o o lngth 5
rvling Slsprson Boston Ith Copnhgn 556 4 6 5 Nw York Lonon 89 6 Amstrm 78 Pris Wshington Munih 66 4 55 49 56 44 8 Rom Atlnt Fin pth o minimum istn tht visits vry ity Implmnting Digrphs Ajny Mtrix g[u][v] is tru i thr is n g rom u to v Ajny List h list or u ontins v i thr is n g rom u to v Implmnting Wight Digrphs Ajny Mtrix g[u][v] is i thr is n g o ost rom u to v 8 5 8 Ajny List h list or u ontins v, i thr is n g rom u to v tht hs ost 5 5 8 Implmnting Unirt Grphs Ajny Mtrix g[u][v] is tru i thr is n g rom u to v Ajny List h list or u ontins v i thr is n g rom u to v Ajny Mtrix or Ajny List? n = numr o vrtis m = numr o gs m u = numr o gs lving u Ajny Mtrix Uss sp O(n ) Cn itrt ovr ll gs in tim O(n ) Cn nswr Is thr n g rom u to v? in O() tim Bttr or ns (i.., lots o gs) grphs Ajny List Uss sp O(m+n) Cn itrt ovr ll gs in tim O(m+n) Cn nswr Is thr n g rom u to v? in O(m u ) tim Bttr or sprs (i.., wr gs) grphs Gol: Fin Shortst Pth in Grph Fining th shortst (min-ost) pth in grph is prolm tht ours otn Fin th lst-ost rout twn Ith n Dtroit Rsult pns on our notion o ost lst milg lst tim hpst lst oring All o ths osts n rprsnt s g osts on grph How o w in shortst pth? 6
S Shortst Pths or Unwight Grphs A B C D E F sdistn(s): // s is th strt vrtx // ist[v] is lngth o s-to-v pth // Initilly ist[v] = or ll v ist[s] = ; Q.insrt(s); whil (Q nonmpty) { v = Q.gt(); or (h w jnt to v) { i (ist[w] == ) { ist[w] = ist[v]+; Q.insrt(w); Anlysis or sdistn How mny tims n vrtx pl in th quu? How muh tim or th orloop? Dpns on rprsnttion Ajny Mtrix: O(n) Ajny List: O(m v ) im: O(n ) or j mtrix O(m+n) or j list sdistn(s): // s is th strt vrtx // ist[v] is lngth o s-to-v pth // Initilly ist[v] = or ll v ist[s] = ; Q.insrt(s); whil (Q nonmpty) { v = Q.gt(); or (h w jnt to v) { i (ist[w] == ) { ist[w] = ist[v]+; Q.insrt(w); I hr r Eg Costs? I # A ls nos so tht ll g osts r But wht i g osts r lrg? Wht i th osts rn t intgrs? I # Nothing intrsting hppns t th ls nos Cn t w just jump h to th nxt rl no Rul: lwys o th losst (rl) no irst Us th rry ist[ ] to Rport nswrs Kp trk o wht to o nxt Dijkstr s Algorithm Intuition Egs r thrs; vrtis r s Pik up t s; mrk h no s it lv th tl Not: Ngtiv g-osts r not llow S 5 A B 9 4 C D E F 9 s is th strt vrtx (i,j) is th ost rom i to j Initilly, vrtis r unmrk ist[v] is lngth o s-to-v pth Initilly, ist[v] =, or ll v ijsktr(s): ist[s] = ; whil (som vrtis r unmrk) { v = unmrk vrtx with smllst ist; Mrk v; or (h w j to v) { ist[w] = min ( ist[w], ist[v] + (v,w) ); Dijkstr s Algorithm using Aj Mtrix Dijkstr s Algorithm using Aj List Whil-loop is on n tims Within th loop Choosing v tks O(n) tim Coul o this str using PQ, ut no rson to For-loop tks O(n) tim otl tim = O(n ) s is th strt vrtx (i,j) is th ost rom i to j Initilly, vrtis r unmrk ist[v] is lngth o s-to-v pth Initilly, ist[v] =, or ll v ijsktr(s): ist[s] = ; whil (som vrtis r unmrk) { v = unmrk vrtx with smllst ist; Mrk v; or (h w j to v) { ist[w] = min ( ist[w], ist[v] + (v,w) ); Looks lik w n PQ Prolm: prioritis r upt s lgorithm runs Cn insrt pir (v,ist[v]) in PQ whnvr ist[v] is upt At most m things in PQ im O(n + m log m) Using mor omplit PQ (.g., Piring Hp), tim n rought own to O(m + n log n) s is th strt vrtx (i,j) is th ost rom i to j Initilly, vrtis r unmrk ist[v] is lngth o s-to-v pth Initilly, ist[v] =, or ll v ijsktr(s): ist[s] = ; whil (som vrtis r unmrk) { v = unmrk vrtx with smllst ist; Mrk v; or (h w j to v) { ist[w] = min ( ist[w], ist[v] + (v,w) ); 7
Dijkstr s Algorithm or Digrphs Gry Algorithms Algorithm works on oth unirt n irt grphs without moiition As or: Ngtiv g-osts r not llow S 5 A B 9 4 C D E F 9 s is th strt vrtx (i,j) is th ost rom i to j Initilly, vrtis r unmrk ist[v] is lngth o s-to-v pth Initilly, ist[v] =, or ll v ijsktr(s): ist[s] = ; whil (som vrtis r unmrk) { v = unmrk vrtx with smllst ist; Mrk v; or (h w j to v) { ist[w] = min ( ist[w], ist[v] + (v,w) ); Dijkstr s Algorithm is n xmpl o Gry Algorithm h Gry Strtgy is n lgorithm sign thniqu Lik Divi & Conqur h Gry Strtgy is us to solv optimiztion prolms h gol is to in th st solution Works whn th prolm hs th gry-hoi proprty A glol optimum n rh y mking lolly optimum hois Prolm: Givn n mount o mony, in th smllst numr o oins to mk tht mount Solution: Us Gry Algorithm Giv s mny lrg oins s you n his gry strtgy prous th optimum numr o oins or th US oin systm Dirnt mony systm gry strtgy my il For xmpl: suppos th US introus 4 oin 8