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 Grut Shool o Mngmnt 5: PM, Wnsy, Novmbr 9th Upson Loung FREE PIZZA! ACSU (Assoition o Computr Sin Unrgruts) his is not Grph hs r Grphs 9 8 7 6 5 4 st Qtr n Qtr r Qtr 4th Qtr Est Wst North K 5 K, =...not th kin w mn, nywy Applitions o Grphs Communition ntworks Routing n shortst pth problms Commoity istribution (low) ri ontrol Rsour llotion Gomtri moling... 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
Exmpl Dirt Grph Exmpl Unirt Grph Exmpl: V = {,b,,,, E = {(,b), (,), (,), (b,), (b,), (b,), (,), (,), (,), (,), (,) V = 6, E = b An unirt grph is just lik irt grph, xpt th gs r unorr pirs (sts) {u,v Exmpl: b V = {,b,,,, E = {{,b, {,, {,, {b,, {b,, {b,, {,, {,, {,, {,, {, b Som 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 by n g h outgr o vrtx u in irt grph is th numbr o gs or whih u is th sour h ingr o vrtx v in irt grph is th numbr o gs or whih v is th sink h gr o vrtx u in n unirt grph is th numbr o gs o whih u is n npoint b Mor Grph rminology 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 numbr 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 b A grph is yli i it hs no yls A irt yli grph is ll g his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs
his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs his i ls to n lgorithm vrtis until th grph ispprs
opologil Sort Just omput topologil sort o th g A numbring o th vrtis suh tht ll gs go rom lowr- to highr-numbr vrtis his i ls to n lgorithm vrtis until th grph ispprs Usul in job shuling with prn onstrints 4 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 jobs Eg (u,v) is prsnt i jobs u n v h rquir ss to th sm shr rsour, n thus nnot xut simultnously Colors r tim slots to shul th jobs Minimum numbr o olors n to olor th grph = minimum numbr o tim slots rquir Plnrity A grph is plnr i it n b mb in th pln with no gs rossing Is this grph plnr?
Plnrity A grph is plnr i it n b mb in th pln with no gs rossing Plnrity A grph is plnr i it n b mb 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, (possibly with othr nos long th gs shown) h Four-Color horm Evry plnr grph is 4-olorbl (Appl & Hkn, 976) Biprtit Grphs Biprtit Grphs A irt or unirt grph is biprtit i th vrtis n b prtition into two sts suh tht ll gs go btwn th two sts h ollowing r quivlnt G is biprtit G is -olorbl G hs no yls o o lngth
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 5 8 Ajny List h list or u ontins v, i thr is n g rom u to v tht hs ost 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 = numbr o vrtis m = numbr o gs m u = numbr 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 problm tht ours otn Fin th lst-ost rout btwn Ith n Dtroit Rsult pns on our notion o ost lst milg lst tim hpst lst boring All o ths osts n b rprsnt s g osts on grph How o w in shortst pth?
Shortst Pths or Unwight Grphs S A B C D E F bsdistn(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 bsdistn How mny tims n vrtx b 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 bsdistn(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 bs Pik up t s; mrk h no s it lv th tbl Not: Ngtiv g-osts r not llow 5 9 S A B 9 4 C D E F 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, but 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 Problm: 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 b brought 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) );
Dijkstr s Algorithm or Digrphs Algorithm works on both unirt n irt grphs without moiition As bor: Ngtiv g-osts r not llow 5 9 S A B 9 4 C D E F 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) ); Gry Algorithms 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 problms h gol is to in th bst solution Works whn th problm hs th gry-hoi proprty A globl optimum n b rh by mking lolly optimum hois Problm: Givn n mount o mony, in th smllst numbr o oins to mk tht mount Solution: Us Gry Algorithm Giv s mny lrg oins s you n his gry strtgy prous th optimum numbr o oins or th US oin systm Dirnt mony systm gry strtgy my il For xmpl: suppos th US introus 4 oin