Analysis of Algorithms - Elmntary graphs algorithms - Anras Ermahl MRTC (Mälaralns Ral-Tim Rsarch Cntr) anras.rmahl@mh.s Autumn 004 Graphs Graphs ar important mathmatical ntitis in computr scinc an nginring Oftn us to rprsnt iffrnt kins of rlational information Dpnncis btwn ntitis Distanc btwn ntitis Graphs In many applications it is intrsting to sarch or travrs th graph Th last numbr of subway stations to gt from Hässlby to Norsborg? How o I visit all subway stations in an fficint mannr? This an following lcturs will al with algorithms for this, an som applications Rprsnting graphs Thr ar many ways of rprsnting graphs A graph G = (V, E) consists of vrtics (nos) V an gs E 1 5 4 V = {1,,,4,5} E = {(1,),(1,5),(,),(,5),(,4),(4,5)} Th rprsntations iffr in fficincy pning on th algorithm, whthr th graph is ns or spars, an othr factors W will consir two rprsntations: Ajacncy-list rprsntation Ajacncy-matrix rprsntation Rprsnting graphs Rprsnting graphs Unirct graph 1 5 4 Dirct graph 1 4 5 6 1 5 1 5 4 1 4 4 5 5 4 1 Ajacncy-list rprsntation 1 4 5 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 Ajacncy-matrix rprsntation 1 4 5 6 5 4 5 4 6 6 Ajacncy-list rprsntation 1 4 5 6 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1 Ajacncy-matrix rprsntation
Ajacncy-list Ajacncy-list G = (V,E) rprsnt by an array Aj of V lists, on pr vrtx Aj[u] contains pointrs to (ID s of) all vrtics ajacnt to u Not ajacnt is iffrnt whn th graph is irct or unirct Dirct: sum of lngth of all ajacncy lists is E Unirct: sum of lngth of all ajacncy lists is E Mmory rquir: Θ(V + E) 1 4 Oftn fficint rprsntation if G is spars 5 6 5 E not too big compar with V Easily aapt to wight graphs Stor wight w on g in ajacncy list 4 5 6 4 6 Ajacncy-list rprsntation Ajacncy-matrix Ajacncy-matrix rprsntation: G = (V,E) rprsnt by a boolan matrix A, whr: Siz O( V ) (inpnnt of E ), fficint whn E = Θ( V ) (an graph siz is small) 1 4 5 Ajacncy matrix symmtric whn graph is unirct, thn 1 0 1 0 0 1 suffics to stor on half 1 0 1 1 1 (still siz O( V ), though) 0 1 0 1 0 Unwight graph can b stor using on bit pr ntry 4 0 1 1 0 1 5 1 1 0 1 0 Easily aapt to wight graphs Ajacncy-matrix Stor wight w for g (u,v) rprsntation at ntry in row u an column v Travrsing graphs Most basic algorithms on graphs will b applications of graph travrsal Printing or valiating ach g/vrtx Copying a graph or convrting btwn rprsntations Counting th numbr of gs/vrtics Intifying connct componnts Fining paths btwn two vrtics, or cycls W will invstigat som classical graph travrsal algorithms in tail Travrsing graphs Important: Efficincy an corrctnss Efficincy: Don t loop or visit vrtics rpatly Corrctnss: Don t miss any vrtx W mark vrtics as w travrs th graph 1 1. Uniscovr (whit), th initial stat, bfor w v sn it. Discovr (gry), w v sn th vrtx but not all of its incint gs. Finish (black), all incint gs hav bn visit. Orr of xploration Th orr in which w xplor vrtics pns on th containr us for storing iscovr but not finish vrtics Two typ of containrs frquntly us: Quu: las to so call brath-first sarch Stack: las to so call pth-first sarch W will invstigat ths two graph travrsal algorithms in mor tail Brath-first sarch Simpl algorithm for sarching a graph Input: A irct/unirct graph with sourc s Output: Th shortst istanc from s [u] = shortst istanc of no u to s p[u] = prcssor of no u in shortst path to s Rprsnt by a brath-first tr Only visits vrtics rachabl from s Running tim: Θ(V+E) Linar tim with rspct to ajacncy list
Brath-first sarch Givn graph G an sourc vrtx s, fin all vrtics rachabl from s by a brath-first sarch from s (whr s is sn as th root in a tr spanning G) Brath-first mans all vrtics at pth k from s ar visit bfor thos at pth k + 1 ar Th algorithm yils th following rsults: Th istanc (lngth of shortst path) from s to any othr rachabl vrtx u ([u]) A s-root brath-first tr that consists of th shortst paths from s to all othr vrtics Works on both irct an unirct graphs Th BFS algorithm s is put in quu Q Each vrtx is st to hav color WHITE, istanc an no prcssor Starting no s is st to hav color GREY, istanc 0 an no prcssor As long as Q not mpty w quu on no u Procss no u is st to BLACK Invstigat all non-visit ajacnt nos to u W st ach such no as GREY, u as its prcssor, an its istanc to 1+u s istanc Exampl: Brath-first sarch Aftr start no s has bn a to Q 0 Q s 0 Exampl: Brath-first sarch Aftr s has bn procss an its nighbours r an w hav bn a 1 0 Q w r 1 1 1 Exampl: Brath-first sarch Aftr no w has bn procss an its nighbours t an x hav bn a 1 0 Q r t x 1 1 Exampl: Brath-first sarch Aftr no r has bn procss an its nighbour v has bn a 1 0 Q t x 1 v
Exampl: Brath-first sarch Aftr no t has bn procss an its nighbour u has bn a 1 0 Q x v u 1 Exampl: Brath-first sarch Aftr no x has bn procss an its nighbour y has bn a 1 0 Q v u y 1 Exampl: Brath-first sarch Aftr no v has bn procss r s t u 1 0 Q u y 1 v w x y Exampl: Brath-first sarch Aftr no u has bn procss r s t u 1 0 Q y 1 v w x y Exampl: Brath-first sarch Aftr no y has bn procss r s t u 1 0 Q «1 v w x y Analysis of BFS Taks O( 1 ) tim What about th outr whil-loop? First loop taks O( V ) tim What about th innr for-loop?
Analysis of BFS Outr loop (whil): Can w boun th tim until Q bcoms mpty? Only whit vrtics ar nquu, an thy ar always gray whn nquu ï A vrtx can b nquu at most onc On vrtx quu for vry lap ï Outr whil loop xcut O( V ) tims Innr loop (for): Analysis of BFS Th ajacncy list of a vrtx is scann at most onc, an ach lmnt in th list is accss only onc Thus, th total tim in innr loop is proportional to sum of lngths of all ajacncy lists fl O( E ) Whol loop nst fl O( V ) + O( E ) = O( V + E ) Analysis of BFS Analysis of BFS Stp 5..9 runs in O(1) tim Whol loop nst (stp 10..18) runs in O( V + E ) tim First loop (stp 1..4) runs in O( V ) tim Total running tim for BFS is: O( V ) + O(1) + O( V + E ) = O( V + E ) tim Dpth-first sarch Simpl algorithm which sarchs pr in th graph whnvr possibl Input: A irct/unirct graph. Output: Dp-first forst (compos of pth-first trs) Each vrtx u is tim-stamp: iscovr [u] an finish f[u]. Egs { tr, back, forwar or cross g }. Visits all vrtics Running tim: Θ(V+E) Linar tim with rspct to ajacncy list. Dpth-first sarch Sarchs graph by rcursivly xploring th vrtics in th ajacncy list All vrtics rachabl from a vrtx in th ajacncy list ar rcursivly sarch bfor nxt vrtx in th list is xplor DFS constructs a pth- first forst that contains all vrtics in th graph BFS only buils a tr of vrtics rachabl from som givn root vrtx Th DFS algorithm [u] is timstamp whn vrtx u first iscovr f[u] is timstamp whn sarch of u s ajacncy list is complt No u gts tim whn first iscovr No u gts tim whn visiting finish All vrtics ar visit onc All vrtics rachabl an from u an not yt visit gts visit by rcursiv call
Dpth-first sarch: vrtx classification Tim-stamp vrtics whn thy ar iscovr/finish: [u]: whn iscovr Th vrtx colors ar quivalnt to th following cass: Whit: th initial stat, bfor w v sn th vrtx Gray: w v sn th vrtx but not all of its incint gs f[u]: whn finish Black: all incint gs hav bn visit Dpth-first sarch: g classification Egs (u,v) ar classifi accoring to th following four cass: Tr-g (T) Eg to whit vrtx Back-g (B) Eg to gry vrtx Forwar-g (F) Eg to black vrtx [u] < [v] Cross-g (C): Eg to black vrtx [u] > [v] Forwar-gs (F) ar non-tr gs (u,v) conncting a vrtx u to a scnant v in a pth-first tr F T T B T C Tr-gs (T) ar gs in th pth-first forst Back-gs (B) ar non-tr gs conncting a vrtx u to an ancstor v in a pth-first tr Cross-gs (C) ar all othr non-tr gs Dpth-first sarch xampl Dpth-first sarch xampl S: { } S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a,, f}
Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a,, g} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, } S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: {a, b} S: {a, b, }
Dpth-first sarch xampl Dpth-first sarch xampl S: {a, b} S: {a} Dpth-first sarch xampl Dpth-first sarch xampl S: { } S: {c} Dpth-first sarch xampl Dpth-first sarch xampl S: {c, h} S: {c}
Dpth-first sarch xampl 1 16 First loop taks O( V ) tim How many tims will DFS-VISIT b call? How many tims will this loop run? Rcursiv call S: { } Rsult of DFS run is a forst! Prcnc graph for how to gt rss unrwar watch socks Analysis of DFS Running tim of DFS: First loop in DFS taks Θ( V ) tim (ach vrtx visit xactly onc) In scon loop with th rcursiv calls, w obsrv that DFS visit will b call xactly onc on ach vrtx For ach vrtx v whr DFS-VISIT(v) is call, th loop in DFS-VISIT is call Aj[v] tims Sinc vv Aj[v] = Θ( E ) th total cost of scon loop in DFS will b Θ( E ) fl Running tim of DFS is Θ( V + E ) What is an actual schul for gtting rss? Topological sort A linar orr of all nos in th jackt graph G such that if G contains an g (u,v) thn u appars bfor v in th orring Topological sort is only possibl if th graph is acyclic Exampl application: Dirct Acyclic Graph (DAG) rprsnts prcnc rlations btwn tasks or vnts Eg btwn tasks if first task must b prform bfor scon task Thn a topological sort givs a possibl schul of th tasks on a singl rsourc On prcnc graph might hav svral possibl schuls pants blt shirt ti shos Topological sort: algorithm W can us DFS to gt a topological sort Informal scription: Call DFS(G) to comput finishing tims f[u] for all vrtics u Put nos into list so thy ar stor in crasing orr w.r.t. finishing tim A irct way is to first us DFS(G) an thn sort w.r.t. finishing tim f[u] This yils tim Θ( V + E )+ O( V lg V ) = O( V lg V + E ) But asy to moify DFS to comput th sort list on th fly : Just insrt ach vrtx into list immiatly whn finish As no asymptotic complxity to DFS: still Θ( V + E ) 11/16 1/15 6/7 Exampl: Topological sort unrwar pants blt shirt ti jackt 1/8 /5 /4 socks 17/18 shos 1/14 watch Prcnc graph for how to gt rss socks shos watch unrwar pants blt shirt ti jackt 17/18 11/16 1/15 1/14 9/10 1/8 6/7 /5 /4 9/10 DFS algorithm givs th shown [u] an f[u] Evnts sort accoring to thir finishing tim = topological sort
Strongly connct componnts A strongly- connct componnt (SCC) of irct graph G = (V,E) is a maximal subst of vrtics C Œ V such that for vry pair of vrtics u an v in C both vrtics ar rachabl from ach othr E A C B Dirct graph D Strongly connct componnts: {A,B,C,E} an {D} Graph Transpos Th transpos of a irct graph G=(V,E) is th graph G T = (V,E T ) such that G T ={(v,u) V x V : (u,v) E } Thus, G T is G with all gs rvrs Both graphs contains th sam nos Both graphs has th sam SCCs G T can b crat in O(V + E) tim 1 4 5 Original graph G 6 1 4 5 Transpos graph G T 6 Fining SCCs W can us algorithms for DFS an graph transposal for fining SCCs of graph G Stps: 1. Call DFS(G) to comput finishing tims f[u] for ach vrtx u in G. Comput transpos graph G T. Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1) 4. Each pth-first tr foun in G T forms a SCC in G Original graph G Fining SCCs Fining SCCs 1. Call DFS(G) to comput finishing tim f[u] for ach vrtx u 1/14 11/16 1/15 /4 1/10 8/9 Sort nos on thir finishing tim: ï b,,a,c,,g,f,h /7 5/6 Fining SCCs. Comput G T from original graph
Fining SCCs. Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1 ï b,,a,c,,g,f,h) Dp- first- trs foun: {b,a,}, {c,}, {g,f}, {h} Fining SCCs 4. Each pth- first tr foun in G T forms a SCC in G: {b,a,}, {c,}, {g,f}, {h} 5. Collaps all SCC to on singl no. Th rsult is th acyclic G SCC graph Each vrtx is on SCC ab fg c h Th visits of nos in th scon DFS corrspons to visiting th vrtics in G SCC in topological sort orr Analysis of SCC algorithm 1. Call DFS(G) to comput finishing tims f[u] for ach vrtx u in G ï Θ(V + E). Comput transpos graph G T ï Θ(V + E). Call DFS(G T ), but in th main loop, consir th vrtics in crasing f[u] (as comput in lin 1) ï Θ(V + E) 4. Collaps ach pth-first tr foun in G T to a SCC ï Θ(V + E) Ovrall total tim: Θ(V + E) Th En!