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 Gui.pf (Progrss pag) Dmonstration wsit: https://visualgo.nt/n/fsfs 320 Graphs&GraphSarhs 2
Graph Typs irt/unirt? yli/ayli? a a In grs/ a out grs? 320 Graphs&GraphSarhs 3
Graph Rprsntations G = (E, V) How shoul w a rprsnt: nos, gs, g attriut, ajany, in/out gr, Possil ata struturs: list, matrix, hash, How muh work: 1) to fin numr of ajant nos? 2) whih nos ar ajant? 3) sink nos? 4) itrat through V, E? 320 Graphs&GraphSarhs 4
Graph Typs a Crat an ajany list for this graph, thn an ajany matrix for it. How an you tll how many in grs/out grs for ah no? 320 Graphs&GraphSarhs 5
Smantis of Eg Dirtion a Look at g (,) what os th arrow tll you? Now onsir (,). How oul you intrprt it? 320 Graphs&GraphSarhs 6
Transposing a Graph to Math a Tak th ajany matrix you rat prviously, an transpos it: A T [i][j] = 1 whn A[j][i] = 1. Now rat th graph G T from this matrix. Compar it to this graph. 320 Graphs&GraphSarhs 7
Miro Survy Rsults 1 Whn to hoos ajany matrix vs list? List whn you hav a graph with << n 2 gs (osn t tak Θ(n 2 ) spa lik a matrix; taks O(m + n) spa) List whn you hav an algorithm that has to xamin all gs inint to a no Matrix has O(1) tim to hk if g (u,v) is in th graph; list has follow list at no u; tim is proportional to th gr (in an out) of th no ut an fin nxt no in list in onstant tim 320 Graphs&GraphSarhs 8
Miro Survy Rsults 2 Smantis: oms for ( ) an pns on ( ) Us transpos to hang from on to th othr BFS rath first sarh (fins shortst path from sour to all nos it an rah) Symol NIL or NULL Colors us to show whih nos hav n isovr, ar ing work on, or ar finish Uss a quu nquu an quu Masur of progrss if thr ar nos in th quu (nos with gs to pross) 320 Graphs&GraphSarhs 9
Brath First Sarh of a graph W n: V, Aj, Quu For ah vrtx u: s (# gs to gt to u from s: ), (prssor of u: ), olor (whit, g gray, lak: w) Visit all nos in a rath first way an uil a rath first tr of shortst paths (u.) from s to ah no (via u. ). f 320 Graphs&GraphSarhs 10
Aj={ s: [,, g] : [, ] : [, f] : [, s] : [, ] f: [] g: []} Num gs m = 13 Brath First Data No info: u ol s w w w w w f w g w s g V ={s,,,,, f, g} Num nos n = 7 f 320 Graphs&GraphSarhs 11
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Brath First Algorithm 320 Graphs&GraphSarhs 12
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak s g f u aj s,,g w, w,f w,s w, w f w g w 320 Graphs&GraphSarhs 13
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Q = {s} s Q = { } s g f u aj s,,g w g 0 X, w,f w,s w, w f w g w 320 Graphs&GraphSarhs 14
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Q = {s} s Q = { } Q = {} s g f u aj s,,g w g 0 X X, wg 1 s,f w,s w, w f w g w 320 Graphs&GraphSarhs 15
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Q = {s} s Q = { } Q = {,} s g f u aj s,,g w g 0 X X, wg 1 s,f w,s w, wg 1 s X f w g w 320 Graphs&GraphSarhs 16
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Q = {s} s Q = { } Q = {,,g} s g f u aj s,,g w g 0 X X, wg 1 s,f w,s w, wg 1 s X f w g wg 1 s X 320 Graphs&GraphSarhs 17
Brath First Algo Rsults v aj ol s,,g w g 0 X, w g 1 X s X X X,f w g 2 XX X,s w X Xg X 2 X, w g 1 X s X X f w g X 3 X X g w g X 1 X s X X X X X X s g f s g f Complxity? Do you always hav to hk all nos? All gs? 320 Graphs&GraphSarhs 18
for ah u G.V {s} u. = whit, u. =, u. = s. = gray, s. = 0 Q = ENQUEUE (Q,s) whil Q u= DEQUEUE (Q) for ah v G.Aj[u] if v.olor == whit v.olor = gray v. = u. + 1 v. = u ENQUEUE (Q,v) u.olor = lak Brath First Algorithm 320 Graphs&GraphSarhs 19
Dpth First Sarh of a graph Works with graphs that hav isonnt parts Basis of othr algorithms (.g. topologial sort, omposing a irt graph into strongly onnt omponnts) 320 Graphs&GraphSarhs 23
Dpth First Sarh of a graph Sarhs all vrtis, uss olor gray whn starting a no, lak s aftr all aj nos pross. tims from gray (isovr tim) to lak (finish tim). Disovr tim olor tlls aout strutur. Visit all nos in a pth first way an uil a forst of pth first trs. 320 Graphs&GraphSarhs 24
Miro Survy Rsults 1 How to trmin omplxity for BFS, graph algos why not just stat th worst as of omplt intronntivity? Complxity has to o with how an algorithm prforms in th worst as, ut this is usually ovrkill if w think of th worst as graph with omplt intronntivity an only talk aout it in trms of th numr of nos. So y onvntion w talk aout how a graph prforms with rspt to th input sts of nos an gs. Thrfor whil w oul say that omplxity is O(n 2 ), it is atually mor pris to say it is O( V + E ). Not that talking aout it this way also shows that w hav a linar omplxity in ths trms. In fat th BFS algorithm rally is linar w pross all nos on to initializ it, an thn all th onnt gs on. 320 Graphs&GraphSarhs 25
Miro Survy Rsults 2 DFS why is th tim varial t twn 1 an 2 V? Dosn t th olor hang to whit an you only visit th no on? Ys, ut th tim varial is not how many tims you visit th no, it is how many nos you visit whil you ar working on th no. If you look at th DFS algo, tim gts inrmnt whn w isovr th no (mak it gray), an thn whn w r on with it (mak it lak). Thrfor t is inrmnt 2 tim for ah no, so its maximum valu is 2 V. Also rmmr w hk vry singl no in DFS. 320 Graphs&GraphSarhs 26
Miro Survy Rsults 3 How o you trmin omplxity with graph algorithms? First, think of input siz ing in trms of th sizs of th sts V an E, th no/vrtis an gs. Nxt, stuy th algorithm to s how th sizs of ths sts afft it. Dos DFS rturn th shortst path to a no? No. W ll s this in ation in th graph worksht nxt wk. 320 Graphs&GraphSarhs 27
DFS Algorithm tim = 0; for u V if u.olor == whit; fsvisit(u) fsvisit tim = tim + 1; u.isovr = tim ; u.olor gray for v u.aj if v.olor == whit v. = u; fsvisit(v) u.olor = lak; t = tim +1; u.finish = tim 320 Graphs&GraphSarhs 28
tim = 0; s for u V if u.olor == whit; fsvisit(u) fsvisit tim = tim + 1; u.isovr = tim ; u.olor gray for v u.aj if v.olor == whit v. = u; fsvisit(v) u.olor = lak; t = tim +1; u.finish = tim In your groups, figur out th rang of intgr tim valus for th graph. Now figur this out for any graph G = (V, E) 320 Graphs&GraphSarhs 29
tim = 0; s for u V if u.olor == whit; fsvisit(u) fsvisit tim = tim + 1; u.isovr = tim ; u.olor gray for v u.aj if v.olor == whit v. = u; fsvisit(v) u.olor = lak; t = tim +1; u.finish = tim What is th oun on th work that has to on y th algorithm? This tim you an us V an E to man th sizs of th sts. 320 Graphs&GraphSarhs 31
tim = 0; s for u V if u.olor == whit; fsvisit(u) fsvisit tim = tim + 1; u.isovr = tim ; u.olor gray for v u.aj if v.olor == whit v. = u; fsvisit(v) u.olor = lak; t = tim +1; u.finish = tim 320 Graphs&GraphSarhs 33
No Color at Disovr Tim s Bak Forwar Cross Tr Color of v whn w xplor (u,v): Whit tr g Gray ak g Blak forwar or ross g 320 Graphs&GraphSarhs 34
s Tr g: Bak g: Forwar g: Cross g: Rrawn with all tr, forwar gs going own, ak gs going up. 320 Graphs&GraphSarhs 35
Topologial Sort Usful for prolms suh as jo shuling, projt ritial path analysis, orr of ompilation tasks, whih orr to loa forign kys in ataass, ata srialization, Graph must irt, ayli (DAG) Linar orring of all vrtis suh that if an g (u,v) xists, thn vrtx u appars for vrtx v in th orring Shows prn among vrtis Uss a DFS to omput finish tims for ah vrtx Insrts ah vrtx into th front of a link list as it is finish 320 Graphs&GraphSarhs 36
A:gt atgoris B:sort into atgoris M:stalish gri C:photograph L:rat sription D:a to DB N:gt voluntrs, K:ig quipmnt E:rsarh J:ollt artifats F:intrprt I:stor H:lan G:sn for analysis P:ag O:masur, ror, map Q:akfill R:a provnin 320 Graphs&GraphSarhs 37
C F A D E B L I H J K P M O N Q G R 320 Graphs&GraphSarhs 38
C F A D E I H B L J K M N O P Q Topologial sort Call DFS to omput v.f for ah vrtx v As ah vrtx is finish insrt it into th front of a link list Rturn th link list G R 320 Graphs&GraphSarhs 39
In liu of ritations 320 Offi Hours Mon Tu W Thurs Fri 8 Col 9 Dr. Gorg Dr. Gorg 10 Dr. Gorg/ Jim 11 Ali Jim 12 Col Ali 1 Col/ Ali Shannon 2 Shannon 3 Dr. Gorg Dr. Gorg 4 Upoming Chk Progrss pag, Piazza postings for upats Graph worksht: Wnsay 320 Graphs&GraphSarhs 40 Topologial sort program u Ot 28
a l l f f k j g f m k j o o q j p p h h i p r n k C F A D E B L I H G J K M N O P Q R a l l f f g f m k k j j o o q j p p h h i p r n k ['n', 'm', 'g', 'k', 'j', 'p', 'r', 'h', 'i', 'o', 'q', '', '', 'a', '', 'l', '', 'f'] ['n', 'm', 'k', 'j', 'p', 'r', 'h', 'i', 'o', 'q', 'g', '', '', 'a', '', 'l', '', 'f'] 320 Graphs&GraphSarhs 41
BFS of all 16.8 M possil olors in th 24 it RGB olor spa. (Python o on wsit.) 320 Graphs&GraphSarhs 42
Strongly Connt Componnts Strongly Connt: a maximal st of vrtis whr all vrtx pairs in th omponnt ar somhow onnt Implis a yl must xist Uss DFS to ompos a irt graph Many algorithms ompos, thn work on omponnts sparatly an thn omin solutions aoring to strutur among omponnts 320 Graphs&GraphSarhs 43
Dtrmining SCCs Algorithm prforms DFS on a graph, thn DFS on its transpos. Th first DFS intifis groups of nos that w liv ar strongly onnt, an th son an thought of as vrifying that othr nos in th graph ar in not part of this sam omponnt. Aftr intifying all th strongly onnt omponnts in th graph, w an ollaps ah into a singl no an w n up with a DAG. 320 Graphs&GraphSarhs 44
Givn this graph: a f g 320 Graphs&GraphSarhs 45
Givn this graph: a f g In your groups, figur out whih nos ar strongly onnt. 320 Graphs&GraphSarhs 46
Givn this graph: a f g In your groups, figur out whih nos ar strongly onnt. Now ontrat all gs until only 1 no rmains in ah omponnt an raw th gs to otain a DAG. 320 Graphs&GraphSarhs 47
a f g Strongly onnt omponnts a f g Ayli omponnt graph G SCC 320 Graphs&GraphSarhs 48
SCC Algorithm Strongly Connt Componnts (G) 1. Call DFS(G) to omput th finish tims of ah vrtx u V, u.f 2. Comput G T 3. Call DFS(G T ), ut in th main loop, onsir th vrtis in rasing orr of u.f 4. Output th vrtis of ah tr in th pth first forst; ah tr is a sparat SCC 320 Graphs&GraphSarhs 51
SCC Algorithm Analysis 1 Assum w hav an ajany list for G. How muh work has to on to rat G T? a f g h Can you think of a way to o this linarly? g, g f, h f g h h h,, f, a, f a 320 Graphs&GraphSarhs 52
SCC Algorithm Analysis 2 Strongly Connt Componnts (G) Call DFS(G) to gt u.f for all u V v_or = all u V y rasing u.f Comput G T Call DFS(G T ) using v_or orring Output th vrtis of ah tr in th pthfirst forst; ah tr is a sparat SCC What is th total work for this algorithm? 320 Graphs&GraphSarhs 53
a f Strongly Connt Componnts (G) Call DFS(G) to gt u.f for all u V v_or = all u V y rasing u.f Comput G T Call DFS(G T ) using v_or orring g h g f h a g, f, h g h h,, f, a, f Output th vrtis of ah tr in th pth first forst; ah tr is a sparat SCC 320 Graphs&GraphSarhs 54
G 13/14 11/16 1/10 8/9 a f 12/15 3/4 2/7 5/6 v_or =,, a,,, g, h, f g h 320 Graphs&GraphSarhs 55
Conntions What hav w rntly n stuying that an hlp gnrat this orring? How os it iffr from what w r trying to o now? 320 Graphs&GraphSarhs 56
G T a f v_or =,, a,,, g, h, f SCC ALGORITHM ontinu. Call DFS(G T ) using v_or orring g h g f h a, f, g,, h, g, Output th vrtis of ah tr in th pth first forst; ah tr is a sparat SCC a 320 Graphs&GraphSarhs 57
G T 2/5 1/6 a f 3/4 12/13 7/10 8/9 g h 11/14 15/16 g f h a, f, g,, h, g, a 320 Graphs&GraphSarhs 58
G T 2/5 1/6 a f 3/4 12/13 7/10 8/9 g h 11/14 15/16 g f h a, f, g,, h, g, a 320 Graphs&GraphSarhs 59
G T 2/5 1/6 a f 3/4 12/13 7/10 8/9 g h 11/14 15/16 g f h a, f, g,, h, g, a 320 Graphs&GraphSarhs 60
Bak to G SCC a gf h Go ak to th original G an ontrat all th gs in ah strongly onnt omponnt until thr is just 1 vrtx. Rtain th gs onnting omponnts. 320 Graphs&GraphSarhs 61
Imag Crits lugolpia: http://www.zi./faturs/sign funtional moluls http://mathworl.wolfram.om/voronoidiagram.html Voronoi: http://www.ams.org/samplings/fatur olumn/far voronoi RGBolorSpa: https://possilywrong.worprss.om/2014/04/18/allrg hilrt urvs an ranom spanning trs/ 320 Graphs&GraphSarhs 62