s s of s Computr Sin & Enginring 423/823 Dsign n Anlysis of Ltur 03 (Chptr 22) Stphn Sott (Apt from Vinohnrn N. Vriym) s of s s r strt t typs tht r pplil to numrous prolms Cn ptur ntitis, rltionships twn thm, th gr of th rltionship, t. This hptr ovrs sis in grph thory, inluing rprsnttion, n lgorithms for si grph-thorti prolms W ll uil on ths ltr this smstr s of s A (simpl, or unirt) grph G =(V,E) onsists of V, nonmpty st of vrtis n E st of unorr pirs of istint vrtis ll gs A D B E C V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)} 1/29 2/29 3/29 s (2) s (3) of s s of s A irt grph (igrph) G =(V,E) onsists of V, nonmpty st of vrtis n E st of orr pirs of istint vrtis ll gs s of s A wight grph is n unirt or irt grph with th itionl proprty tht h g hs ssoit with it rl numr w() ll its wight 3-6 12 7 0 s of s Ajny Two ommon wys of rprsnting grph: Ajny list n jny mtrix Lt G =(V,E) grph with n vrtis n m gs 3 4 Othr vritions: multigrphs, psuogrphs, t. 4/29 5/29 6/29
Ajny (BFS) s of s Ajny For h vrtx v 2 V, stor list of vrtis jnt to v For wight grphs, informtion to h no How muh is sp rquir for storg? s of s Ajny Us n n n mtrix M, whrm(i, j) =1if (i, j) is n g, 0 othrwis If G wight, stor wights in th mtrix, using 1 for non-gs How muh is sp rquir for storg? 0 1 1 1 0 1 0 0 0 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 0 s of s Givn grph G =(V,E) (irt or unirt) n sour no s 2 V, BFS systmtilly visits vry vrtx tht is rhl from s Uss quu t strutur to srh in rth-first mnnr Crts strutur ll BFS tr suh tht for h vrtx v 2 V, th istn (numr of gs) from s to v in tr is th shortst pth in G Initiliz h no s olor to whit As no is visit, olor it to gry () in quu), thn lk () finish) 7/29 8/29 9/29 BFS(G, s) BFS Exmpl BFS Exmpl (2) for h vrtx u 2 V \{s} o 1 olor[u] =whit 2 [u] =1 3 [u] =nil s of s 4 n 5 olor[s] =gry 6 [s] =0 7 [s] =nil 8 Q = ; 9 Enquu(Q, s) 10 whil Q 6= ; o 11 u = Dquu(Q) 12 for h v 2 Aj[u] o 13 if olor[v] ==whit thn 14 olor[v] =gry 15 [v] =[u] +1 16 [v] =u 17 Enquu(Q, v) s of s s of s 18 19 n 20 olor[u] =lk 21 n 10 / 29 11 / 29 12 / 29
BFS Proprtis (DFS) DFS(G) s of s Wht is th running tim? Hint: How mny tims will no nquu? Aftr th n of th lgorithm, [v] =shortst istn from s to v ) Solvs unwight shortst pths Cn print th pth from s to v y rursivly following [v], [ [v]], t. If [v] ==1, thn v not rhl from s ) Solvs rhility s of s Anothr grph trvrsl lgorithm Unlik BFS, this on follows pth s p s possil for ktrking Whr BFS is quu-lik, DFS is stk-lik Trks oth isovry tim n finishing tim of h no, whih will om in hny ltr s of s for h vrtx u 2 V o 1 olor[u] =whit 2 [u] =nil 3 n 4 tim =0 5 for h vrtx u 2 V o 6 if olor[u] ==whit thn 7 DFS-Visit(u) 8 9 n 13 / 29 14 / 29 15 / 29 DFS-Visit(u) DFS Exmpl DFS Exmpl (2) s of s olor[u] =gry 1 tim = tim +1 2 [u] =tim 3 for h v 2 Aj[u] o 4 if olor[v] ==whit thn 5 [v] =u 6 DFS-Visit(v) 7 8 n 9 olor[u] =lk 10 f[u] =tim = tim +1 s of s s of s 16 / 29 17 / 29 18 / 29
DFS Proprtis DFS Proprtis (2) Applition: s of s 19 / 29 Tim omplxity sm s BFS: ( V + E ) Vrtx u is propr snnt of vrtx v in th DF tr i [v] <[u] <f[u] <f[v] ) Prnthsis strutur: If on prints (u whn isovring u n u) whn finishing u, thn print txt will wll-form prnthsiz sntn s of s 20 / 29 Clssifition of gs into groups A tr g is on in th pth-first forst A k g (u, v) onnts vrtx u to its nstor v in th DF tr (inlus slf-loops) A forwr g is nontr g onnting no to on of its DF tr snnts A ross g gos twn non-nstrl gs within DF tr or twn DF trs S lls in DFS xmpl Exmpl us of this proprty: A grph hs yl i DFS isovrs k g (pplition: lok ttion) Whn DFS first xplors n g (u, v), look t v s olor: olor[v] ==whit implis tr g olor[v] ==gry implis k g olor[v] ==lk implis forwr or ross g s of s 21 / 29 A irt yli grph (g) n rprsnt prns: n g (x, y) implis tht vnt/tivity x must our for y Applition: (2) Algorithm Applition: s of s A topologil sort of g G is n linr orring of its vrtis suh tht if G ontins n g (u, v), thnu pprs for v in th orring s of s 1 Cll DFS lgorithm on g G 2 As h vrtx is finish, insrt it to th front of link list 3 Rturn th link list of vrtis s of s Givn irt grph G =(V,E), strongly onnt omponnt (SCC) of G is mximl st of vrtis C V suh tht for vry pir of vrtis u, v 2 Cuis rhl from v n v is rhl from u Thus topologil sort is sning sort of vrtis s on DFS finishing tims Why os it work? Whn no is finish, it hs no unxplor outgoing gs; i.. ll its snnt nos r lry finish n insrt t ltr spot in finl sort 22 / 29 23 / 29 24 / 29 Wht r th SCCs of th ov grph?
Trnspos SCC Algorithm SCC Algorithm Exmpl s of s Our lgorithm for fining SCCs of G pns on th trnspos of G, not G T G T is simply G with gs rvrs Ft: G T n G hv sm SCCs. Why? s of s 1 Cll DFS lgorithm on G 2 Comput G T 3 Cll DFS lgorithm on G T, looping through vrtis in orr of rsing finishing tims from first DFS ll 4 Eh DFS tr in son DFS run is n SCC in G s of s Aftr first roun of DFS: Whih no is first on to visit in son DFS? 25 / 29 26 / 29 27 / 29 SCC Algorithm Exmpl (2) SCC Algorithm Anlysis s of s Aftr son roun of DFS: s of s Wht is its tim omplxity? How os it work? 1 Lt x no with highst finishing tim in first DFS 2 In G T, x s omponnt C hs no gs to ny othr omponnt (Lmm 22.14), so th son DFS s tr gs fin xtly x s omponnt 3 Now lt x 0 th nxt no xplor in nw omponnt C 0 4 Th only gs from C 0 to nothr omponnt r to nos in C, so th DFS tr gs fin xtly th omponnt for x 0 5 An so on... 28 / 29 29 / 29