CMPS 2200 Fll 2017 Grps Crol Wnk Sls ourtsy o Crls Lsrson wt ns n tons y Crol Wnk 10/23/17 CMPS 2200 Intro. to Alortms 1
Grps Dnton. A rt rp (rp) G = (V, E) s n orr pr onsstn o st V o vrts (snulr: vrtx), st E V V o s. In n unrt rp G = (V, E), t st E onssts o unorr prs o vrts. rt rp 2 1 unrt rp 2 1 3 4 3 4 In tr s, w v E O( V 2 ). Morovr, G s onnt, tn E V 1. 10/23/17 CMPS 2200 Intro. to Alortms 2
Ajny-mtrx rprsntton T jny mtrx o rp G = (V, E), wr V = {1, 2,, n}, s t mtrx A[1.. n, 1.. n] vn y 1 (, j) E, A[, j] = 0 (, j) E. 2 1 3 4 A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 1 ( V 2 ) stor ns rprsntton. 10/23/17 CMPS 2200 Intro. to Alortms 3
Ajny-lst rprsntton An jny lst o vrtx v V s t lst Aj[v] o vrts jnt to v. 2 1 3 4 Aj[1] = {2, 3} Aj[2] = {3} Aj[3] = {} Aj[4] = {3, 4} For unrt rps, Aj[v] = r(v). For rps, Aj[v] = out-r(v). 10/23/17 CMPS 2200 Intro. to Alortms 4
Ajny-lst rprsntton Hnskn Lmm: Evry s ount tw For unrt rps: v V r(v) = 2 E For rps: v V n-r(v) = v V out-r(v) = E jny lsts us ( V + E ) stor sprs rprsntton W usully us ts rprsntton, unlss stt otrws 10/23/17 CMPS 2200 Intro. to Alortms 5
Grp Trvrsl Lt G=(V,E) (rt or unrt) rp, vn n jny lst rprsntton. V = n, E = m A rp trvrsl vsts vry vrtx: Brt-rst sr (BFS) Dpt-rst sr (DFS) 10/23/17 CMPS 2200 Intro. to Alortms 6
Brt-Frst Sr (BFS) BFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 Intlz mpty quu Q or vrtx v V o v s unvst vst v // tm++ Q.nquu(v) BFS_tr(G) BFS_tr(G) wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 7
Exmpl o rt-rst sr wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) Q: 10/23/17 CMPS 2200 Intro. to Alortms 8
Exmpl o rt-rst 0 sr 0 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 9
Exmpl o rt-rst 0 1 sr 2 1 2 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 10
Exmpl o rt-rst 0 1 sr 2 3 4 2 3 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 11
Exmpl o rt-rst 0 1 sr 2 3 4 3 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 12
Exmpl o rt-rst 0 1 sr 2 3 4 4 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 13
Exmpl o rt-rst 0 1 sr 2 3 4 5 5 6 Q: wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 14
Exmpl o rt-rst 0 1 sr 2 3 4 5 7 Q: 6 7 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 15
0 1 Exmpl o rt-rst sr 2 3 4 Q: 5 7 8 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o 6 w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 16
0 1 Exmpl o rt-rst sr 2 3 4 Q: 5 7 8 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o 6 w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 10/23/17 CMPS 2200 Intro. to Alortms 17
Exmpl o rt-rst 0 1 sr 2 3 4 5 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 Q: 10/23/17 CMPS 2200 Intro. to Alortms 18
Exmpl o rt-rst Dstn to : 0 1 2 3 4 0 1 sr 2 3 4 Q: 5 7 8 wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) 6 10/23/17 CMPS 2200 Intro. to Alortms 19
Brt-Frst Sr (BFS) O(n) O(1) O(n) wtout BFS_tr BFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 Intlz mpty quu Q or vrtx v V o v s unvst vst v // tm++ Q.nquu(v) BFS_tr(G) O(m) BFS_tr(G) wl Q s non-mpty o v = Q.quu() or w jnt to v o w s unvst vst w // tm++ A (v,w) to T Q.nquu(w) O((v)) 10/23/17 CMPS 2200 Intro. to Alortms 20
BFS runtm E vrtx s mrk s unvst n t nnn O(n) tm E vrtx s mrk t most on, nquu t most on, n tror quu t most on T tm to pross vrtx s proportonl to t sz o ts jny lst (ts r), sn t rp s vn n jny lst rprsntton O(m) tm Totl runtm s O(n+m) = O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 21
Dpt-Frst Sr (DFS) DFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 or vrtx v V o v s unvst DFS_r(G,v) DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm 10/23/17 CMPS 2200 Intro. to Alortms 22
Exmpl o pt-rst sr / 0/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 23 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 1/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 24 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 1/- 2/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 25 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 1/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 26 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - 1/- 4/- Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 27 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 28 1/- 4/- 5/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 29 1/- 4/- 5/- 6/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 30 6/- 7/- 7/8 1/- 4/- 5/- DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 31 7/8 1/- 4/- 5/- 6/- 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 32 1/- 4/- 5/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 33 5/- 10/- 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 10/- 11/12 11/- : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 34 5/- 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 11/12 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 35 5/- 10/- 10/13 1/- 4/- 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 11/12 : - Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 36 10/13 1/- 4/- 5/- 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 2/3 11/12 : - 1/- 4/15 4/- Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 37 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/- 1/16 1/- 2/3 11/12 : - 4/15 Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 38 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
Exmpl o pt-rst sr / 0/17 0/- 1/16 2/3 11/12 : - 4/15 Stor s n prssor rry 10/23/17 CMPS 2200 Intro. to Alortms 39 10/13 5/14 7/8 6/9 DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm
O(1) Dpt-Frst Sr (DFS) O(n) O(n) wtout DFS_r O((v)) wtout rursv ll DFS(G=(V,E)) Mrk ll vrts n G s unvst // tm=0 or vrtx v V o v s unvst DFS_r(G,v) DFS_r(G, v) mrk v s vst // [v]=++tm or w jnt to v o w s unvst A (v,w) to tr T DFS_r(G,w) mrk v s ns // [v]=++tm Wt Hnskn Lmm, ll rursv lls r O(m), or totl o O(n + m) runtm 10/23/17 CMPS 2200 Intro. to Alortms 40
DFS lsston / 0/17 1/16 2/3 11/12 10/23/17 CMPS 2200 Intro. to Alortms 41 10/13 5/14 7/8 6/9 4/15 E u v s : tr, t s prt o t pt-rst orst. k, u onnts to n nstor v n ptrst tr. It ols (u)>(v) n (u)<(v). orwr, t onnts u to snnt v n pt-rst tr. It ols (u)<(v). ross, t s ny otr. It ols (u)>(v) n (u)>(v).
Pts, Cyls, Conntvty Lt G=(V,E) rt (or unrt) rp A pt rom v 1 to v k n G s squn o vrts v 1, v 2,,v k su tt (v,v {+1} ) E (or {v,v {+1} } E G s unrt) or ll {1,,k-1}. A pt s smpl ll vrts n t pt r stnt. A pt v 1, v 2,,v k orms yl v 1 =v k. A rp wt no yls s yl. An unrt yl rp s ll tr. (Trs o not v to v root vrtx sp.) A rt yl rp s DAG. (A DAG n v unrt yls t rton o t s s not onsr.) An unrt rp s onnt vry pr o vrts s onnt y pt. A rt rp s stronly onnt or vry pr u,v V tr s pt rom u to v n tr s pt rom v to u. T (stronly) onnt omponnts o rp r t quvln lsss o vrts unr ts rlty rlton. 10/23/17 CMPS 2200 Intro. to Alortms 42
DAG Torm Torm: A rt rp G s yl pt-rst sr o G yls no k s. Proo: : Suppos tr s k (u,v). Tn y nton o k tr woul yl. : Suppos G ontns yl. Lt v t rst vrtx to sovr n, n lt u t prn vrtx n. v s n nstor o u n t pt-rst orst, n (u,v) s k. v u 10/23/17 CMPS 2200 Intro. to Alortms 43
Topolol Sort Topololly sort t vrts o rt yl rp (DAG): Dtrmn : V {1, 2,, V } su tt (u, v) E (u) < (v). 1 4 7 8 2 3 5 6 9 1 2 3 4 5 6 7 8 9 10/23/17 CMPS 2200 Intro. to Alortms 44
Topolol Sort Alortm Stor vrts wt n-r 0 n quu Q. Wl Q s not mpty Dquu vrtx v, n v t t nxt numr Drs n-r o ll jnt vrts y 1 Enquu ll vrts wt n-r 0 Q: 0 0 2 1 10,,,,,,, 3 5 0 7 10/23/17 CMPS 2200 Intro. to Alortms 45 4, 0 21 6 10 9 20 1 1 1 0 8 3 1 20
Topolol Sort Runtm Runtm: O( V + E ) us vry s tou on, n vry vrtx s nquu n quu xtly on 10/23/17 CMPS 2200 Intro. to Alortms 46
DFS-Bs Topolol Sort Alortm Cll DFS on t rt yl rp G=(V,E) Fns tm or vry vrtx Rvrs t ns tms (st ns tm oms t lowst ns tm, ) Vl unton : V {1, 2,, V } su tt (u, v) E (u) < (v) Runtm: O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 47
DFS-Bs Topolol Sort Run DFS: 1/12 2/11 3 /6 4 /5 13/18 14/17 15/16 7/10 8 /9 Rvrs ns tms: 4 5 8 9 1 2 3 6 7 10/23/17 CMPS 2200 Intro. to Alortms 48
DFS-Bs Top. Sort Corrtnss N to sow tt or ny (u, v) E ols (v) < (u). (sn w onsr rvrs ns tms) Consr xplorn (u, v) n DFS: v nnot vst n unns (n n n nstor n t pt rst tr), sn tn (u,v) woul k (w y t DAG lmm nnot ppn). I v s not n vst yt, t oms snnt o u, n n (v)<(u). (tr ) I v s n ns, (v) s n st, n u s stll n xplor, n (u)>(v) (orwr, ross ). 10/23/17 CMPS 2200 Intro. to Alortms 49
Topolol Sort Runtm Runtm: O( V + E ) us vry s tou on, n vry vrtx s nquu n quu xtly on DFS-s lortm: O( V + E ) 10/23/17 CMPS 2200 Intro. to Alortms 50