Agoritm Dign nd Anyi Victor Admcik CS 15-451 Spring 2014 Lctur 40 Apr 28, 2014 Crngi Mon Univrity String Mtcing - II T KMP Agoritm Torm: At mot 2N comprion in tot Morri Knut T Ao-Corick Agoritm (1986) Pttrn {,, i, r trt 0 1 r 2 8 * 9 i T goritm prproc t t of pttrn. 3 6 4 n 7 n 5 T Ao-Corick Agoritm W ti u t ongt uffix ru. If w fi on mking trnition from nod N to it cid, w trnition to nod M, wr t tring tt dfin M i t frtt nod (ongt prfix) from t root wic i o uffix of t tring w d mtcd wn w fid (rmoving t firt trnition). T ony diffrnc i tt intd of trvring ing tring ft-to-rigt w now v to trvr tri. T Rbin-Krp Agoritm (1981) T goritm u t id of ing 1
pttrn = 4848 T min id txt = 16180339887498948482045 W do not mtc tring gint givn pttrn, but rtr compr tir cod. T min id pttrn = 4848 % 71 = 20 1 6 1 8 0 3 3 9 8 8 7 4 9 8 9 4 8 4 8 2 0 4 5 1 6 1 8 1618 % 71 = 56 6 1 8 0 6180 % 71 = 3 1 8 0 3 1803 % 71 = 28 W rd t txt in t numbr of crctr qu to t ngt of t pttrn, comput it cod nd compr wit t pttrn cod. Wt i it compxity? M = pttrn.ngt() N = txt.ngt(); Computing cod How cn w gt from 145 to 456? W wi do ti by crting cin of oprtion Simir to brut-forc mtcing T ky id of improving t goritm i in computing cod in O(1). 145-45 - 450-456 Rmov t ding digit, mutipy by b, dd ing digit. It tk O(1) to comput cod from t prviou vu. Exmp Exmp Givn: cod for 31729 Givn: cod for 31729 31729 mod 41 = 36 31729 mod 41 = 36 Tk: comput cod for 17295. Tk: comput cod for 17295. Obrv, 17295 = (31729-3*10 4 ) * 10 + 5 2
Exmp 17295%41 = [(31729%41-3*10 4 %41) *10 + 5]%41 31729%41 i rdy computd. 3*10 4 % 41 wi b prcomputd 17295%41 = [(36-29) * 10 + 5]%41 = 75 % 41 = 34 Rbin-Krp formizd Lt P[1... m] b pttrn nd T[1... n] b txt. W dfin pttrn P = 10 m-1 P[1] + 10 P[m-1] + + P[m] nd ift in t txt: t = 10 m-1 T[+1] + 10 T[+m-1] + + T[+m] T vu t +1 cn b obtind from t by t +1 = (t - 10 m-1 T[+1]) 10 + T[+m+1] Exrci Hornr Ru W id 31729%41 i rdy computd How woud you comput it ft? x 4 + b x 3 + c x 2 + d x + = + x (d + x (c + x (b + x) Impmnttion pubic int rc(string T, String P){ int M = P.ngt(), N = T.ngt(); Impmnttion (cont.) if(1 == 2) rturn 0; int dm = 1, 1 = 0, 2 = 0; int q = 3355439; /*pick it t rndom */ int d = 256; /* rdix */ for(int j = 1; j < M; j++) dm = (d*dm) % q; for(int j = 0; j < M; j++){ 1 = (1*d + P.crAt(j)) % q; 2 = (2*d + T.crAt(j)) % q; for(int i = M; i < N; i++) { 2 = 2 - T.crAt(i - M) * dm % q; 2 = (2*d + T.crAt(i)) % q; if(1 == 2) rturn i - M + 1; rturn -1; 3
F mtc TRIES = "rtriv" T == P mod q Wt do w do in c of f mtc? Wn w found mtc w cn cck t mtc by cr comprion. Frdkin (1960) Min id: bd on t digit of t ky! TRIES by t or Ec nod (or dg) i bd wit crctr Cidrn of nod r ordrd (pbticy) Pt from root to v yid input tring b y $ o r t ntin Appiction Auto comption Sp cckr Dt comprion Computtion bioogy Goog invrtd tb Nod Structur Oftn wtfu of pc bcu mny of t cid fid r nu. Poib nod rprnttion: Arry H Tb Linkd Lit Binry Tr 4
Src pubic boon find (TriNod nod, String ky) { if (ky.ngt()==0) rturn nod.iword(); cr c = ky.gtcr(0); String rt = ky.ubtring(1); TriNod cid = nod.gtcid(c); if(cid == nu) rturn f; Runtim rturn find (cid, rt); compxity -? Inrt pubic void inrt (TriNod nod, String ky) { if (ky.ngt()==0) nod.tword(tru); cr c = ky.gtcr(0); String rt = ky.ubtring(1); TriNod cid = nod.gtcid(c); if(cid == nu) { nod.tcid(nw TriNod(c), c); inrt (nwcid, rt); Runtim compxity -? inrt (cid, rt); Prfix Mtc Find tring trting wit b t y $ o r Advntg, rtiv to BST Advntg, rtiv to ing Src i ftr! It do not dpnd on t numbr of mnt in t tr. Tri p wit prfix-mtcing. No coiion. No function. Apbtic orting. How? 5
Comprd Tri Comprd Tri by t or Ec non-f nod (xcpt root) t t two cidrn Rpc cin of on-cid nod wit ing nod bd wit tring by t $ or Compct Tri (PATRICIA) A mor compct rprnttion of comprd tri Compct Tri (PATRICIA) A mor compct rprnttion of comprd tri by t by t Intgr indx $ or t $ or t o r (i, j, k) o r 6