CSE 373 Grphs 1: Conpts, Dpth/Brth-First Srh ring: Wiss Ch. 9 slis rt y Mrty Stpp http://www.s.wshington.u/373/ Univrsity o Wshington, ll rights rsrv. 1
Wht is grph? 56 Tokyo Sttl Soul 128 16 30 181 140 Nw York Syny L.A. 2
Grphs grph: A t strutur ontining: st o vrtisv, (somtims ll nos) st o gse, whr n g rprsnts onntion twn 2 vrtis. Grph G= (V, E) n g is pir (v, w) whr v, wr in V th grph t right: V= {,,, } E= {(, ), (, ), (, ), (, )} gr: numr o gs touhing givn vrtx. t right: =1, =2, =3, =2 3
Grph xmpls For h, wht r th vrtis n wht r th gs? W pgs with links Mthos in progrm tht ll h othr Ro mps (.g., Googl mps) Airlin routs Fook rins Cours pr-rquisits Fmily trs Pths through mz 4
Pths pth: A pth rom vrtx to is squn o gs tht n ollow strting rom to rh. n rprsnt s vrtis visit, or gs tkn xmpl, on pth rom Vto Z: {, h} or {V, X, Z} Wht r two pths rom U to Y? pth lngth: Numr o vrtis or gs ontin in th pth. nighor or jnt: Two vrtis onnt irtly y n g. xmpl: V n X U V W X Y g h Z 5
Rhility, onntnss rhl: Vrtx is rhl rom i pth xists rom to. V onnt: A grph is onnti vry vrtx is rhl rom ny othr. Is th grph t top right onnt? U W X g Y h Z strongly onnt: Whn vry vrtx hs n g to vry othr vrtx. 6
Loops n yls yl: A pth tht gins n ns t th sm no. xmpl: {, g,,, } or {V, X, Y, W, U, V}. xmpl: {,, } or {U, W, V, U}. yli grph: On tht os not ontin ny yls. V loop: An g irtly rom no to itsl. Mny grphs on't llow loops. U W X g h Z Y 7
Wight grphs wight: Cost ssoit with givn g. Som grphs hv wight gs, n som r unwight. Egs in n unwight grph n thought o s hving qul wight (.g. ll 0, or ll 1, t.) Most grphs o not llow ngtiv wights. xmpl: grph o irlin lights, wight y mils twn itis: SFO 1843 ORD 849 142 PVD HNL 2555 337 LAX 1743 1233 802 DFW 1387 1120 LGA 1099 MIA 8
Dirt grphs irt grph ("igrph"): On whr gs r on-wy onntions twn vrtis. I grph is irt, vrtx hs sprt in/out gr. A igrph n wight or unwight. Is th grph low onnt? Why or why not? g 9
Digrph xmpl Vrtis = UW CSE ourss (inomplt list) Eg (, ) = is prrquisit or 120 131 142 140 450 143 154 403 331 312 311 344 332 341 351 352 333 373 374 431 446 444 421 440 451 410 415 413 414 417 452 10
Link Lists, Trs, Grphs A inry tris grph with som rstritions: Th tr is n unwight, irt, yli grph (DAG). Eh no's in-gr is t most 1, n out-gr is t most 2. Thr is xtly on pth rom th root to vry no. A link listis lso grph: F Unwight DAG. In/out gr o t most 1 or ll nos. B K A B C D A E H G J 11
Srhing or pths Srhing or pth rom on vrtx to nothr: Somtims, w just wnt ny pth (or wnt to know thr is pth). Somtims, w wnt to minimiz pth lngth(# o gs). Somtims, w wnt to minimiz pth ost(sum o g wights). Wht is th shortst pth rom MIA to SFO? Whih pth hs th minimum ost? HNL $130 $250 SFO $60 LAX $70 $170 $120 $500 $80 DFW ORD $50 $140 $110 MIA PVD $200 LGA $100 12
Dpth-irst srh pth-irst srh(dfs): Fins pth twn two vrtis y xploring h possil pth s r s possil or ktrking. Otn implmnt rursivly. Mny grph lgorithms involv visiting or mrking vrtis. Dpth-irst pths rom to ll vrtis (ssuming ABC g orr): to :{, } to : {,,,, } to :{, } to :{,, } to : {,,, } to g:{,, g} to h: {,, g, h} g h 13
DFS psuoo untion s(v 1, v 2 ): s(v 1, v 2, { }). untion s(v 1, v 2, pth): pth+= v 1. mrk v 1 s visit. i v 1 is v 2 : pth is oun! g h or h unvisit nighor no v 1 : i s(n, v 2, pth) ins pth: pth is oun! pth-= v 1. // pth is not oun. Th pthprm ov is us i you wnt to hv th pth vill s list on you r on. Tr s(, ) in th ov grph. 14
DFS osrvtions isovry: DFS is gurnt to in pth i on xists. rtrivl: It is sy to rtriv xtly wht th pth is (th squn o gs tkn) i w in it g h optimlity: not optiml. DFS is gurnt to in pth, not nssrily th st/shortst pth Exmpl: s(, ) rturns {,,, } rthr thn {,, }. 15
Brth-irst srh rth-irst srh(bfs): Fins pth twn two nos y tking on stp own ll pths n thn immitly ktrking. Otn implmnt y mintining quu o vrtis to visit. BFS lwys rturns th shortst pth (th on with th wst gs) twn th strt n th n vrtis. to :{, } to : {,,, } to :{, } to :{, } to : {,, } to g:{,, g} to h: {,, h} g h 16
BFS psuoo untion s(v 1, v 2 ): quu:= {v 1 }. mrk v 1 s visit. whil quu is not mpty: v:= quu.rmovfirst(). i vis v 2 : pth is oun! g h or h unvisit nighor no v: mrk ns visit. quu.lst(n). // pth is not oun. Tr s(, ) in th ov grph. 17
BFS osrvtions optimlity: lwys ins th shortst pth (wst gs). in unwight grphs, ins optiml ost pth. In wight grphs, not lwys optiml ost. g h rtrivl: hrr to ronstrut th tul squn o vrtis or gs in th pth on you in it onptully, BFS is xploring mny possil pths in prlll, so it's not sy to stor pth rry/list in progrss solution: W n kp trk o th pth y storing prssors or h vrtx (h vrtx n stor rrn to prvious vrtx). DFS uss lss mmory thn BFS, sir to ronstrut th pth on oun; ut DFS os not lwys in shortst pth. BFS os. 18
DFS, BFS runtim Wht is th xpt runtim o DFS n BFS, in trms o th numr o vrtis V n th numr o gs E? Answr: O( V + E ) whr V = numr o vrtis, E = numr o gs Must potntilly visit vry no n/or xmin vry g on. why not O( V * E )? Wht is th sp omplxity o h lgorithm? (How muh mmory os h lgorithm rquir?) 19
BFS tht ins pth untion s(v 1, v 2 ): quu:= {v 1 }. mrk v 1 s visit. whil quu is not mpty: v:= quu.rmovfirst(). g h i vis v 2 : pth is oun! (ronstrut it y ollowing.prvk to v 1.) or h unvisit nighor no v: mrk ns visit. (st n.prv= v.) quu.lst(n). // pth is not oun. prv By storing som kin o "prvious" rrn ssoit with h vrtx, you n ronstrut your pth k on you in v 2. 20