Word Search

A path from u to v is

Introdution to Graphs: Bradth-First, Dpth-First Sarh, Topologial Sort Chaptr 23 Graphs So ar w hav xamind trs in dtail. Trs ar a spii instan o a onstrut alld a graph. In gnral, a graph is omposd o dgs
of 11
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Introdution to Graphs: Bradth-First, Dpth-First Sarh, Topologial Sort Chaptr 23 Graphs So ar w hav xamind trs in dtail. Trs ar a spii instan o a onstrut alld a graph. In gnral, a graph is omposd o dgs E and vrtis V that link th nods togthr. A graph G is otn dnotd G=(V,E) whr V is th st o vrtis and E th st o dgs. Two typs o graphs: 1. Dirtd graphs: G=(V,E) whr E is omposd o ordrd pairs o vrtis; i.. th dgs hav dirtion and point rom on vrtx to anothr. 2. Undirtd graphs: G=(V,E) whr E is omposd o unordrd pairs o vrtis; i.. th dgs ar idirtional. Dirtd Graph: 1 2 Undirtd Graph: 1 2 Th dgr o a vrtx in an undirtd graph is th numr o dgs that lav/ntr th vrtx. Th dgr o a vrtx in a dirtd graph is th sam,ut w distinguish twn indgr and out-dgr. Dgr = in-dgr + out-dgr. A path rom u to v is u, w1, v and (u,w1)(w1,w2)(w2,w3) (w n,v) Th running tim o a graph algorithm xprssd in trms o E and V, whr E = E and V= V ;.g. G=O(EV) is E * V Implmnt a graph in thr ways: 1. Pointrs/mmory or ah nod 2. Adjany List 3. Adjany-Matrix Using an Adjany List: List o pointrs or ah vrtx 1 2 A A Th sum o th lngths o th adjany lists is 2 E in an undirtd graph, and E in a dirtd graph. Th amount o mmory to stor th array or th adjany list is O(max(V,E))=O(V+E). Using an Adjany Matrix: Whn is using an adjany matrix a good ida? A ad ida? Th matrix always uss Θ( v 2 ) mmory. Usually asir to implmnt and prorm lookup than an adjany list. Spars graph: vry w dgs. Dns graph: lots o dgs. Up to v 2 dgs i ully onntd. Th adjany matrix is a good way to rprsnt a wightd graph. In a wightd graph, th dgs hav wights assoiatd with thm. Updat matrix ntry to ontain th wight. Wights ould indiat distan, ost, t. Sarh: Th goal is to mthodially xplor vry vrtx and vry dg; prhaps to do som prossing on ah. Will assum adjany-list rprsntation o th input graph. Bradth-First-Sarh (BFS) : Exampl 1: Binary Tr. This is a spial as o a graph. Th ordr o sarh is aross lvls. Th root is xamind irst; thn oth hildrn o th root; thn th hildrn o thos nods, t. (draw tr and show sarh path) Somtims w an stop th algorithm i w ar looking or a partiular lmnt, ut th gnral BFS algorithm runs through vry nod. Exampl 2: dirtd graph: 1. Pik a sour vrtx S to start. 2. Find (or disovr) th vrtis that ar adjant to S. 3. Pik ah hild o S in turn and disovr thir vrtis adjant to that hild. 4. Don whn all hildrn hav n disovrd and xamind. This rsults in a tr that is rootd at th sour vrtx S. Th ida is to ind th distan rom som Sour vrtx y xpanding th rontir o what w hav visitd. Psudood: Uss FIFO Quu Q BFS(s) ; s is our sour vrtx or ah u V - {s} ; Initializ unvisitd vrtis to do d[u] d[s] 0 ; distan to sour vrtx is 0 Q {s} ; Quu o vrtis to visit whil Q 0 do rmov u rom Q or ah v Adj[u] do ; Gt adjant vrtis i d[v]= thn d[v] d[u]+1 ; Inrmnt dpth put v onto Q ; Add to nods to xplor Dirns rom ook: Not traking prdssor s via π Uss ininity instad o olors (whit, gray) Not using nquu, dquu; omin dquu with had o Q Exampl: (this is th inal stat, start with 0 and ininity as valus) 0 3 a 1 d i 3 g h 3 3 Initially, d[a] is st to 0 and th rst to. Q [a]. Rmov had: Q [] hildrn o a ar, d[]=, d[]= so d[] d[a]+1=1, d[] d[a]+1=1 Q [ ] Rmov had: Q [] hildrn o ar, d[]=, d[]= so d[] d[]+1=2, d[] d[]+1=2 Q [ ] Rmov had: Q [ ] hildrn o is d[] , nothing don with it Rmov had: Q [] hildrn o is d, i, h d[d]=, d[i]=, d[h]= so d[d] d[i] d[h] d[]+1=3 Q [ d i h] Rmov had: Q [d i h] hildrn o d is g d[g]=, so d[g] d[d]+1 = 3 Q [d i h g] Eah o ths has hildrn that ar alrady has a valu lss than, so ths will not st any urthr valus and w ar don with th BFS. Can rat a tr out o th ordr w visit th nods: a d h i g 3 Mmory rquird: Nd to maintain Q, whih ontains a list o all ring vrtis w nd to xplor. Runtim: O(V+E) ; O(E) to san through adjany list and O(V) to visit ah vrtx. This is onsidrd linar tim in th siz o G. Claim: BFS always omputs th shortst path distan in d[i] twn S and vrtx I. W will skip th proo or now. What i som nods ar unrahal rom th sour? (rvrs -,-h dgs). What valus do ths nods gt? Dpth First Sarh: Anothr mthod to sarh graphs. Exampl 1: DFS on inary tr. Spializd as o mor gnral graph. Th ordr o th sarh is down paths and rom lt to right. Th root is xamind irst; thn th lt hild o th root; thn th lt hild o this nod, t. until a la is ound. At a la, aktrak to th lowst right hild and rpat. (Show xampl inary tr DFS) Exampl 2: DFS on dirtd graph. 1. Start at som sour vrtx S. 2. Find (or xplor) th irst vrtx that is adjant to S. 3. Rpat with this vrtx and xplor th irst vrtx that is adjant to it. 4. Whn a vrtx is ound that has no unxplord vrtis adjant to it thn aktrak up on lvl. Don whn all hildrn hav n disovrd and xamind. Rsults in a orst o trs. Psudood: DFS(s) or ah vrtx u V do olor[u] Whit tim 1 or ah vrtx u V do i olor[u]=whit thn DFS-Visit(u,tim) DFS-Visit(u,tim) olor[u] Gray d[u] tim tim tim+1 or ah v Adj[u] do i olor[u]=whit thn DFS-Visit(v,tim) olor[u] Blak [u] tim tim+1 ; not visitd ; tim stamp ; in progrss nods ; d=disovr tim ; =inish tim Exampl: 1/18 a 2/17 d 10/11 /6 3/16 9/14 12/13 i g 4/7 8/1 h Numrs ar Disovr/Finish tims. W ould hav dirnt visit tims dpnding on whih dgs w pik to travrs during th DFS. Th tr uilt y this sarh looks lik: a g h d i What i som nods ar unrahal? W still visit thos nods in DFS. Considr i -, -h links wr rvrsd. Thn w nd up with two sparat trs: 1/10 a 2/9 d 11/12 /6 3/8 13/18 1/16 i g 4/7 14/17 h Still visit all vrtis and gt a orst: a st o unonntd graphs without yls (a tr is a onntd graph without yls). Tim or DFS: O(V 2 ) - DFS loop gos O(V) tims on or ah vrtx (an t mor than on, aus a vrtx dos not stay whit), and th loop ovr Adj runs up to V tims. But Th or loop in DFS-Visit looks at vry lmnt in Adj on. It is hargd on pr dg or a dirtd graph, or twi i undirtd. A small part o Adj is lookd at during ah rursiv all ut ovr th ntir tim th or loop is xutd only th sam numr o tims as th siz o th adjany list whih is Θ (E). Sin th initial loop taks Θ (V) tim, th total runtim is Θ (V+E). This is onsidrd linar in trms o th siz o th input adjany-list rprsntation. So i thr ar lots o dgs thn E dominats th runtim, othrwis V dos. Not: Don t hav to trak th aktraking/ring as in BFS sin this is don or us in th rursiv alls and th stak. Th stak maks th nods ordrd LIFO. Th amount o storag ndd is linar in trms o th dpth o th tr. Typs o Edgs: Thr ar 4 typs. Ths will usul latr. DFS an modiid to lassiy dgs as ing o th orrt typ: 1. Tr Edg: An dg in a dpth-irst orst. Edg(u,v) is a tr dg i v was irst disovrd rom u. 2. Bak Edg: An dg that onnts som vrtx to an anstor in a dpth-irst tr. Slloops ar ak dgs. 3. Forward Edg: An dg that onnts som vrtx to a dsndant in a dpth-irst tr. 4. Cross Edg: Any othr dg. DAG s Nothing to do with shp! A DAG is a Dirtd Ayli Graph. This is a dirtd graph that ontains no yls. Exampls: a d A dirtd graph D is ayli i a DFS o G yilds no ak dgs. Proo: Trivial. Ayli mans no ak dg aus a ak dg maks a yl. Suppos w hav a ak dg (u,v). Thn v is an anstor o u in th dpth-irst orst. But thn thr is alrady a path rom v to u and th ak dg maks a yl. Suppos G has a yl. But thn DFS o G will hav a ak dg. Lt v th irst vrtx in ound y DFS and lt u a vrtx in that is onntd ak to v. Thn whn DFS xpands th hildrn o u, th vrtx v is ound. Sin v is an anstor o u th dg (u,v) is a ak dg. v u Topologial Sort o a dag A topologial sort o a dag is an ordring o all th vrtis o G so that i (u,v) is an dg thn u is listd (sortd) or v. This is a dirnt notion o sorting than w ar usd to. a,,,,d, and,a,,,,d ar oth topologial sorts o th aov dag. Thr may multipl sorts; this is okay sin a is not rlatd to, ithr vrtx an om irst. Main us: Indiat ordr o vnts, what should happn irst Algorithm or Topologial-Sort: 1. Call DFS(G) to omput (v), th inish tim or ah vrtx. 2. As ah vrtx is inishd insrt it onto th ront o th list. 3. Rturn th list. Tim is Θ (V+E), tim or DFS. Exampl: Pizza dirtd graph 1/12 sau 2/3 ak 4/ hs 6/11 sausag 7/10 8/9 organo olivs rust 13/14 DFS: Start with sau. Th numrs indiat start/inish tim. W insrt into th list in rvrs ordr o inish tim. Crust, Sau, Sausag, Olivs, Organo, Chs, Bak Why dos this work? Baus w don t hav any ak dgs in a dag, so w won t rturn to pross a parnt until atr prossing th hildrn. W an ordr y inish tims aus a vrtx that inishs arlir will dpndnt on a vrtx that inishs latr.
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks