Aministrivi: CS61B Ltur #33 Autogrr will run this vning. Toy s Rings: Grph Struturs: DSIJ, Chptr 12 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 1
Why Grphs? For xprssing non-hirrhilly rlt itms Exmpls: Ntworks: piplins, ros, ssignmnt prolms Rprsnting prosss: flow hrts, Mrkov mols Rprsnting prtil orrings: PERT hrts, mkfils Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 2
Som Trminology A grph onsists of A st of nos (k vrtis) A st of gs: pirs of nos. Nos with n g twn r jnt. Dpning on prolm, nos or gs my hv lls (or wights) Typilly ll no st V = {v 0,..., n g st E. If th gs hv n orr (first, son), thy r irt gs, n w hv irt grph (igrph), othrwis n unirt grph. Egs r inint to thir nos. Dirt gs xit on no n ntr th nxt. A yl is pth without rpt gs ling from no k to itslf (following rrows if irt). A grph is yli if it hs yl, ls yli. Arvition: Dirt Ayli Grph DAG. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 3
Som Piturs Ayli: Dirt Unirt Cyli: 1 3 2 1 With Eg Lls: 1 3 2 0 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 4
Trs r Grphs A grph is onnt if thr is (possily irt) pth twn vry pir of nos. Tht is, if on no of th pir is rhl from th othr. A DAG is (root) tr iff onnt, n vry no ut th root hs xtly on prnt. A onnt, yli, unirt grph is lso ll fr tr. Fr: w r fr to pik th root;.g., Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 5
Exmpls of Us Eg = Connting ro, with lngth. Dtroit 200 Chigo Eg = Must omplt for; No ll = tim to omplt. Et 1 hr Slp 8 hrs Eg = Bgt Mrtin Gorg Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 6
Eg = som rltionship Mor Exmpls ts lovs potstikrs John Mry Eg = nxt stt might (with proility) 0.9 0.4 0.6 0.4 0.1 ht th t in Eg = nxt stt in stt mhin, ll is triggring input. (Strt t s. Bing in stt 4 mns thr is sustring 001 somwhr in th input.) 1 0 s 0 0 1 2 3 4 0,1 1 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 7
Rprsnttion Oftn usful to numr th nos, n us th numrs in gs. Eg list rprsnttion: h no ontins som kin of list (.g., link list or rry) of its sussors (n possily prssors). 1: (2,3) () 2: (3) (1) 3: () (1,2) Eg sts: Colltion of ll gs. For grph ov: {(1,2),(1,3),(2,3) Ajny mtrix: Rprsnt onntion with mtrix ntry: 1 2 3 1 2 3 0 1 1 0 0 1 0 0 0 Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 8
Trvrsing Grph Mny lgorithms on grphs pn on trvrsing ll or som nos. Cn t quit us rursion us of yls. Evn in yli grphs, n gt omintoril xplosions: 1 4 7 0 3 6... 3N 2 5 8 Trt 0 s th root n o rursiv trvrsl own th two gs out of h no: Θ(2 N ) oprtions! So typilly try to visit h no onstnt # of tims (.g., on). Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 9
Rursiv Dpth-First Trvrsl of Grph Cn fix looping n omintoril prolms using th r-rum mtho us in rlir lturs for mz. Tht is, mrk nos s w trvrs thm n on t trvrs prviously mrk nos. Mks sns to tlk out prorr n postorr, s for trs. voi prorrtrvrs(grph G, No v) { if (v is unmrk) { mrk(v); visit v; for (Eg(v, w) G) trvrs(g, w); voi postorrtrvrs(grph G, No v) { if (v is unmrk) { mrk(v); for (Eg(v, w) G) trvrs(g, w); visit v; Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 10
Rursiv Dpth-First Trvrsl of Grph (II) W r oftn intrst in trvrsing ll nos of grph, not just thos rhl from on no. So w n rpt th prour s long s thr r unmrk nos. voi prorrtrvrs(grph G) { for (v nos of G) { prorrtrvrs(g, v); voi postorrtrvrs(grph G) { for (v nos of G) { postorrtrvrs(g, v); Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 11
Topologil Sorting Prolm: Givn DAG, fin linr orr of nos onsistnt with th gs. Tht is, orr th nos v 0, v 1,... suh tht v k is nvr rhl from v k if k > k. Gmk os this. Also PERT hrts. A B C D E H F G A B D E C F G A C B D F E G C A F D B G E C F G A B D E H H H H Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 12
Sorting n Dpth First Srh Osrvtion: Suppos w rvrs th links on our grph. If w o rursiv DFS on th rvrs grph, strting from no H, for xmpl, w will fin ll nos tht must om for H. Whn th srh rhs no in th rvrs grph n thr r no sussors, w know tht it is sf to put tht no first. In gnrl, postorr trvrsl of th rvrs grph visits nos only ftr ll prssors hv n visit. A B C D E H F G A B D E H C 0 F 1 G 2 Numrs show postorr trvrsl orr strting from G: vrything tht must om for G. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 13
Gnrl Grph Trvrsl Algorithm COLLECTION OF VERTICES fring; fring = INITIAL COLLECTION; whil (!fring.isempty()) { Vrtx v = fring.remove HIGHEST PRIORITY ITEM(); if (!MARKED(v)) { MARK(v); VISIT(v); For h g(v,w) { if (NEEDS PROCESSING(w)) A w to fring; Rpl COLLECTION OF VERTICES, INITIAL COLLECTION, t. with vrious typs, xprssions, or mthos to iffrnt grph lgorithms. Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 14
Exmpl: Dpth-First Trvrsl Prolm: Visit vry no rhl from v on, visiting nos furthr from strt first. Stk<Vrtx> fring; fring = stk ontining {v; whil (!fring.isempty()) { Vrtx v = fring.pop(); if (!mrk(v)) { mrk(v); VISIT(v); For h g(v,w) { if (!mrk(w)) fring.push(w); Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 15
Dpth-First Trvrsl Illustrt [] f [,] f [,,] f [,f,,] f [f,,] f [,,] f [,] f [] f Fring: z Mrk: Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 16
Topologil Sort in Ation C 0 fring C 0 C 0 C 0 A 0 D 2 F 1 A 0 D 1 F 1 A 0 D 0 F 0 A 0 D 0 F 0 B 1 E 3 G 1 B 0 E 3 G 1 B 0 E 3 G 1 B 0 E 2 G 1 H 1 Output: [] H 1 [A] H 1 [A,C] H 1 [A,C,B] C 0 C 0 C 0 A 0 D 0 F 0 A 0 D 0 F 0... A 0 D 0 F 0 B 0 E 1 G 0 B 0 E 0 G 0 B 0 E 0 G 0 H 1 [A,C,B,F] H 1 [A,C,B,F,D] H 0 [A,C,B,F,D,E,G,H] Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 17
Shortst Pths: Dijkstr s Algorithm Prolm: Givn grph (irt or unirt) with non-ngtiv g wights, omput shortst pths from givn sour no, s, to ll nos. Shortst = sum of wights long pth is smllst. For h no, kp stimt istn from s,......n of pring no in shortst pth from s. PriorityQuu<Vrtx> fring; For h no v { v.ist() = ; v.k() = null; s.ist() = 0; fring = priority quu orr y smllst.ist(); ll vrtis to fring; whil (!fring.isempty()) { Vrtx v = fring.rmovfirst(); For h g(v,w) { if (v.ist() + wight(v,w) < w.ist()) { w.ist() = v.ist() + wight(v,w); w.k() = v; Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 18
Exmpl C 5 4 A 0 2 B 2 2 3 5 3 7 D 4 E 1 F 3 6 2 G 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 3 6 2 G 6 1 H 9 C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 1 F 3 6 2 G 7 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 7 3 6 2 G 6 1 H 9 C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 3 6 2 G 7 1 H C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 6 3 6 2 G 6 1 H 7 Finl rsult: C 5 5 4 A 0 2 B 2 2 2 3 5 3 7 D 3 4 E 5 1 F 6 3 6 2 G 6 1 H 7 Shortst-pth tr X pross no t istn Y no in fring t istn Lst moifi: W Nov 8 00:39:28 2017 CS61B: Ltur #33 19