Wintr 2016 COMP-250: Introduction to Computr Scinc Lctur 23, April 5, 2016
Commnt out input siz 2) Writ ny lgorithm tht runs in tim Θ(n 2 log 2 n) in wors cs. Explin why this is its running tim. I don t cr wht it dos. I only cr out its running tim WhtEvr(int m) FOR i=1 TO m FOR j=1 TO m x=m; WHILE x>1 DO { x=x/2; y=m; WHILE y>1 DO y=y/2 } n = m ~ log m. Thrfor running tim is Θ(m 2 log 2 m)= Θ(2 2n n 2 )
Commnt out input siz 2) Writ ny lgorithm tht runs in tim Θ(n 2 log 2 n) in wors cs. Explin why this is its running tim. I don t cr wht it dos. I only cr out its running tim WhtEvr(int[] A) n = A.lngth; FOR i=1 TO n FOR j=1 TO n x=n; WHILE x>1 DO { x=x/2; y=n; WHILE y>1 DO y=y/2 }
Pulic Announcmnt
STRINGS AND PATTERN MATCHING Brut Forc,Rin-Krp, Knuth-Morris-Prtt Rgulr Exprssions
Th Knuth-Morris-Prtt Algorithm Th Knuth-Morris-Prtt (KMP) string srching lgorithm diffrs from th rut-forc lgorithm y kping trck of informtion gind from prvious comprisons. Afilur function (f) is computd tht indicts how much of th lst comprison cn rusd if it fils. Spcificlly, f is dfind to th longst prfix of th pttrn P[0,..,j] tht is lso suffix of P[1,..,j] - Not: not suffix of P[0,..,j]
Th Knuth-Morris-Prtt Algorithm Th Spcificlly, Knuth-Morris-Prtt f dfind to (KMP) th string longst srching prfix of lgorithm th pttrn diffrs P[0,..,j] from tht th is lso rut-forc suffix lgorithm of P[1,..,j] y kping - Not: not trck suffix of informtion of P[0,..,j] gind from prvious comprisons. Exmpl: Afilur - vlu of function th KMP (f) filur is computd function: tht indicts how much of th lst comprison cn rusd if it fils. j 0 1 2 3 4 5 Spcificlly, P[j] f is dfind to th longst prfix c of th pttrn P[0,..,j] tht is lso suffix of P[1,..,j] f(j) 0 0 1 2 3 0 - Not: not suffix of P[0,..,j] This shows how much of th ginning of th string mtchs up to th portion immditly prcding fild comprison. - if th comprison fils t (4), w know th, in positions 2,3 is idnticl to positions 0,1
Th KMP Algorithm (contd.) th KMP string mtching lgorithm: Psudo-Cod Algorithm KMPMtch(T,P) Input: Strings T (txt) with n chrctrs nd P (pttrn) with m chrctrs. Output: Strting indx of th first sustring of T mtching P, or n indiction tht P is not sustring of T. f KMPFilurFunction(P) {uild filur function} i 0 j 0 whil i < n do if P[j] = T[i] thn if j = m - 1 thn rturn i - m - 1 { mtch} i i + 1 j j + 1 ls if j > 0 thn {no mtch, ut w hv dvncd} j f(j-1) {j indxs just ftr mtching prfix in P} ls i i + 1 rturn Thr is no sustring of T mtching P
Th KMP Algorithm (contd.) Th KMP filur function: Psudo-Cod Algorithm KMPFilurFunction(P); Input: String P (pttrn) with m chrctrs Ouput: Th fliur function f for P, which mps j to th lngth of th longst prfix of P tht is suffix of P[1,..,j] i 1 j 0 whil i m-1 do if P[j] = P[i] thn {w hv mtchd j + 1 chrctrs} f(i) j + 1 i i + 1 j j + 1 ls if j > 0 thn {j indxs just ftr prfix of P tht mtchs} j f(j-1) ls {thr is no mtch} f(i) 0 i i + 1
Th KMP Algorithm (contd.) A grphicl rprsnttion of th KMP string srching lgorithm c c c c 1 2 3 4 5 6 c 7 c no comprison ndd hr 8 9 10 11 12 c 13 c 14 15 16 17 18 c 19
Th KMP Algorithm (contd.) Tim Complxity Anlysis dfin k = i - j In vry itrtion through th whil loop, on of thr things hppns. - 1) if T[i] = P[j], thn i incrss y 1, s dos j k rmins th sm. - 2) if T[i]!= P[j] nd j > 0, thn i dos not chng nd k incrss y t lst 1, sinc k chngs from i - j to i - f(j-1) - 3) if T[i]!= P[j] nd j = 0, thn i incrss y 1 nd k incrss y 1 sinc j rmins th sm.
Th KMP Algorithm (contd.) Tim Complxity Anlysis Thus, ch tim through th loop, ithr i or k incrss y t lst 1, so th grtst possil numr of loops is 2n This of cours ssums tht f hs lrdy n computd. Howvr, f is computd in much th sm mnnr s KMPMtch so th tim complxity rgumnt is nlogous. KMPFilurFunction is O(m) Totl Tim Complxity: O(n + m)
Rgulr Exprssions nottion for dscriing st of strings, possily of infinit siz ε dnots th mpty string + c dnots th st {, c} * dnots th st {ε,,,,...} Exmpls - (+)* ll th strings from th lpht {,} - *(*)** strings with n vn numr of s - (+)*sun(+)* strings contining th pttrn sun - (+)(+)(+) 4-lttr strings nding in
Finit Stt Automton mchin for procssing strings 0 1 3 2 1 0 6 4 2 3 5 1, Composition of FSA s
Wintr 2016 COMP-250: Introduction to Computr Scinc Lctur 23, April 5, 2016