CS 241 Anlysis o Algorithms Prossor Eri Aron Ltur T Th 9:00m Ltur Mting Lotion: OLB 205 Businss HW6 u lry HW7 out tr Thnksgiving Ring: Ch. 22.1-22.3 1
Grphs (S S. B.4) Grphs ommonly rprsnt onntions mong rlt lmnts An unirt grph G = (V,E) is A st V o vrtis (nos), n A st E o gs (link pirs o nos), whih r iirtionl A irt grph (or igrph) G = (V,E) is A st V o vrtis (nos), n () Dirt grph () Unirt grph () Sugrph o () inu y vrtx st {1,2,3,6} So, in unirt grphs, gs r unorr pirs o vrtis, whrs in igrphs, gs r orr pirs. By onvntion, in n unirt grph G = (V,E), w onsir (u,v) n (v,u) to th sm g, so t most on o thos pirs will in E. A st E o uniirtionl gs (typilly rprsnt s rrows). Not tht sl-loops gs rom no to itsl r possil Convntions: For lgorithm nlysis, w my us V or V n E or E V is otn rrr to y n n E y m Grph Voulry Now showing on PBS: Mr. Rogrs, Grph Thortiin Lots o voulry: I = (u,v) \in E, thn is inint on oth u n v I = (u,v) \in E, thn v is jnt to u. Ajnt vrtis r nighors Th gr o vrtx is th numr o gs inint on it In igrphs, h vrtx hs n in-gr (th numr o gs pointing in to it) n n out-gr (th numr o gs pointing out rom it) A pth o lngth k rom vrtx u to vrtx u' is squn (v 0, v 1,..., v k ) o vrtis suh tht u = v 0, u' = v k n thr is n g twn h v i, i = 1,2,...,k. In igrph, pth xists twn vrtis n only i thr is squn o outgoing gs rom to A simpl pth is pth with ll istint vrtis I thr is pth p twn vrtis u n v, w sy v is rhl rom u vi p Importnt Thnil Osrvtion: I w r gonn tlk out grphs, w n wors or grph stu othrwis, it s rlly hr to tlk out. 2
Mor Grph Voulry So w n tlk out mor grphs! Grphs with th sm numr o vrtis n hv irnt numrs o gs. Wht s th most gs grph n hv, in trms o n? A sprs grph is on in whih E is muh lss thn V 2 A ns grph is on in whih E is los to V 2 (S CLRS, pg. 589) In igrph, pth (v 0, v 1,..., v k ) orms yl i v 0 = v k n th pth ontins t lst on g A yl is simpl i v 1, v 2,..., v k r istint. (Why not inlu v 0 in this?) g h A irt grph with no yls is irt yli grph, or DAG (R CLRS S. 22.1-22.3) Ajny List Rprsnttions A grph n rprsnt s n jny list For h vrtx v, thr s list o ll nos jnt to v Rprsnt s n rry o V == n lists Complxity: Storg sp: O(V + E) Exmpl: An unirt grph (For wht kins o grphs is this n iint storg rprsnttion?) Wht s th tim omplxity to in i n g is in grph? 3
Ajny List Rprsnttions A grph n rprsnt s n jny list For h vrtx v, thr s list o ll nos jnt to v Rprsnt s n rry o V == n lists Exmpl: igrph Ajny Mtrix Rprsnttions A grph oul lso rprsnt s n jny mtrix A V-y-V mtrix : For h pir (i,j) o vrtis, ntry (i,j) in th mtrix is 1 i (i,j) \in E, n 0 othrwis Complxity: Storg sp: O(V 2 ) g Exmpl: An unirt grph Thr is no spoon nxt to nything rom Th Ajny Mtrix 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 (For wht kins o grphs is this n iint storg rprsnttion?) Wht s th tim omplxity to in i n g is in grph? g g Rll: For igrphs, only outgoing gs r in E (thus in th mtrix) 4
Brth-First Srh (BFS) n Dpth-First Srh (DFS) Two ommon lgorithms or srhing (or trvrsing) grph Brth-First Srh: inormlly, isovr ll nos rhl within k stps (i.., gs) or isovring ny with k+1 stps Cn thought o s FIFO, or irst-sn, irst-isovr Dpth-First Srh: inormlly, isovring ll nos own on pth or xploring ny othr pth Cn thought o s LIFO, or most rntly sn, irstisovr Brth-First Srh (BFS) BFS: Mintins quu Q to mng vrtis; strts with sour vrtx s Initilly, ll vrtis xpt s r olor whit, n s is gry. BFS lgorithm mintins inormtion or h vrtx u: - u.olor (whit, gry, or lk) : inits sttus whit = not isovr yt gry = isovr, ut not inish lk = inish - u. : istn rom s to u; initilly or ll ut s=0 - u.π : prssor o u in BF tr Fins ll vrtis v rhl rom s y uiling rth-irst tr, whr th pth in th tr rom s to v hs th wst numr o gs o ll pths rom s to v 5
Brth-First Srh (BFS) Psuoo or BFS: Enquu(Q,s) s s to th rr o Q Dquu(Q) rmovs n rturns th itm t th h o Q Not: I G is not onnt, thn BFS will not visit th ntir grph (without som xtr provisions) Tim omplxity o BFS With j list rprsnttion? With j mtrix rprsnttion? BFS (G, s) 1. Enquu (Q,s) 2. whil Q 3. u = Dquu(Q) 4. or h v jnt to u 5. i v.olor == whit 6. v.olor = gry 7. v. = u. + 1 8. v.π = u 9. Enquu(Q,v) 10. u.olor = lk Dpth-First Srh (DFS) DFS grph xplortion: ollows gs out o most rntly isovr vrtx v tht still hs unxplor gs. Whn ll o v s gs r xplor, DFS ktrks to xplor gs inint on th vrtx rom whih v ws isovr Whn no u is irst isovr uring DFS, it is tim-stmp with isovry tim u. Whn ll u s jnt nos hv n visit, (long with ll th nos rhl rom u tht r whit), u is tim-stmp with inishing tim u. Explors nos s i mintining stk o visit nos Rsults in spnning tr o G i.., tr ompos o ll th nos o G n som (prhps ll) o th gs o G 6
Dpth-First Srh (DFS) DFS grph xplortion: ollows gs out o most rntly isovr vrtx v tht still hs unxplor gs. Whn ll o v s gs r xplor, DFS ktrks to xplor gs inint on th vrtx rom whih v ws isovr DFS mintins th ollowing inormtion or h vrtx u: - u.olor (whit, gry, or lk) : inits sttus whit = not isovr yt gry = isovr, ut not inish lk = inish - u. : isovry tim o no u - u. : inishing tim o no u - u.π : prssor o u in D-F tr Initilly, u.olor=whit, with u.= u.=, u.π = NIL or ll u, n tim = 0 Dpth-First Srh (DFS) Psuoo or rursiv DFS: Itrtiv vrsion oul us stk Nots: I G is not onnt, DFS will still visit th ntir grph Atr xution, or h vrtx u, u. < u. (s xpt!) No no will inish until ll its snnts r inish (s xpt!) Tim omplxity o DFS With j list rprsnttion? With j mtrix rprsnttion? DFS (G) 1. or h v G 2. i v.olor == whit 3. DFS-Visit (G,v) DFS-Visit (G,u) 1. u.olor = gry 2. u. = tim 3. tim = tim + 1 4. or h v jnt to u 5. i v.olor == whit 6. v.π = u 7. DFS-Visit(G,v) 8. u.olor = lk 9. u. = tim 10. tim = tim + 1 7
Wight Grphs n Shortst Pths oul th lum titl or ollortion twn xk n Dth C For Cuti. Wight Grphs n Shortst Pths Wll, it oul. Grphs n wight: Givn G=(V,E), thr is wight untion w tht mps h g in E to rl-vlu wight For wight grphs, n jny mtrix n stor th wight o n g in E (rthr thn just 0 or 1) Givn suh w, w sy th wight o pth w(p) or p = <v 0, v 1,, v k > is th sum o th wights o th gs on tht pth (i.., (v 0, v 1 ), (v 1, v 2 ), (v k-1, v k )) W thn sy th shortst pth wightδ(u,v) rom vrtx u to vrtx v is th lst wight o ny pth in G rom u to v A shortst pth rom u to v is ny pth rom u to v in G with tht wight Shortst Pth Prolms Kins o grph prolms s on ining shortst pths (y onvntion, prsum wight, irt grphs): Singl-sour shortst pths Vrious lgorithms or ss o it (Bllmn-For, Dijkstr) Singl-stintion shortst pths I w hv singl-sour shortst pths lgorithm, how oul w solv this? Singl-pir shortst pth How os this rlt to th singl-sour vrint? All-pirs shortst pths W ll tlk mor out this soon (Not: To rprsnt (shortst) pth in solving suh prolm, h vrtx is prsum to hv prssor il, whih stors its prssor on th pth ing onsir.) 8