0% found this document useful (0 votes)
2 views

4 module algorithms

Scanned copy of Module 4 algorithms of DSA, MSC103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

4 module algorithms

Scanned copy of Module 4 algorithms of DSA, MSC103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 28
crams frequently need to find all occurrences of a pattern in the sical ie text is a document being edited, and the pattern searched for is a ext. DP supplied by the user. Efficient algorithms for this problem—called sulasswond. SUPP greatly aid the responsiveness of the text-editing program. f many other applications, string-matching algorithms search for par- ‘DNA sequences. Internet search engines also use them to find partes trerns in Z riculet Wes relevant to queries. Web page” Tiize the string-matching problem as follows. We assume. that the We formay T[L.-7] of length »_and that the pattem is an aay P(I..m] text is > az We further assume that the elements of P and T are char- =n a finite alphabet ©. For example, we may have © = {0,1} of length acters drawn “The character P and : oe fa.b,-.- 2} The character arrays P an T are often called strings of or ing to Figure 32.1, we say that pattem P occurs with shift s in text T (or equivalently, that pattern P occurs beginning at position s +1 in text Tit o Ih then y 3 x. [x] =|yhthenr ay TSP Proof See Figure 32.3 for a graphical proof. . For brevity of notation, we denote the k-character prefix P[I ..k] of the patter P[1..m] by Px. Thus, Py = ¢ and Py = P = P[1..m}. Similarly, we denote the k-character prefix of the text T by 7. Using this notation, we can state the string-matching problem as that of finding all shifts s in the range 0 1000n and randomly select get from (0.1,....q ~ 1}. Then, Alice evaluates : at Aw) = (E-") bite Bob similarly evaluates B(x). Prove that if A # B, there is at most one - in 1000 that A(x) = B(x), whereas if the two files are the same, A(x) is pecessrly the same as B(x). (Hint: See Exercise 314-4) ite automata matching with fini suring ‘Many string-matching algorithms build a finite automaton—a simple machine for ssing nformationthat scans the text sting T for all occurrences of the pat tem P. This section presents a method for building such an automaton, These string-matching automata are very efficient: they examine each text character ex- cacily once, taking constant time per text character. The matching time used —after sprocessing the pattern to build the automaton—is therefore O(n). The time to build the automaton, however, can be large if 5 is large. Section 32.4 describes a clever way around this problem. ‘We begin this section with the definition ofa finite automaton, We then examine a special string-matching automaton and show how to use it to find occurrences of a pattern in a text. Finally, we shall show how to construct the string-matching ‘automaton for a given input pattern, Finite automata A finite automaton M, illustrated in Figure 32.6, is a 5-tuple (2,40, 4, 5,8), where + Qisa finite set of states, * go € Qs the start state, * AG Q isa distinguished set of accepting states, + Disa finite input alphabet, * Sis function from Q x ¥ into Q, called the transition function of M. | | a & Scanned with OKEN Scanner on eprom eee 2 Oe” A ee eae: Coenen meee cae nae Bio oad were is ape 1 fnite automaton bepins in state go and reads the characters of oo Te ins in state o and reads the characte of int qematching, 2 one i muon isin se ganda np cha sion dager OF he aa sate and Cesc weston) fom aa a2) “Whenever is canent su" 1 one ampapact SHE ret wate] Woe ‘2 member of A, the machine “accepted the string read 0 far. Aninpact’, 201 Oe sine ed ee HOTS Seaham HE ‘is not accepted is rejected. Pt fi as ss Ss Fr ane astO TA tte automaton M induces a function ¢, called the final-state func rt in So an A ape ‘from E" to O such that 6(w) isthe state M ends upin after Scanning the sing y eat A meee ging 1 NINE matches fom Ho Q such at 0) ay GU) eA. We ate be ne Se te are = 8.8 recursively, using the transition function: + eg SO ese OO pm sbaDACN Te o@) = 4 St Se aes Sete ae SHON ones () = a. an 20 op CAEN ‘cent ehiaracter TP BPE g(a) = 8(w).a) forwed*aek swe ase on tbo. Under caCN, Srornavon {NAS ONE ven alone ee pretn Ti THe at eat = a ecessins ‘String-matching automata wos an Fora given pattem P, we construct a string-matching automaton ina preprocs, ing step before using it to search the text sting, Figure 22:7 iluststes bow we construct the automaton for the pattern P = ababaca. From now on, we sl tesume that P isa given fixed pattern string; for brevity, we shell not indicate te assume that r > 0. Tr ® scanned with OKEN Scanner weith flee sito sp west 7 ont yt A 28 Anillsration forthe proof of Lemms 322, The fi eee "ure shows that r < a(x) 4 1, ify the operation of a string-matching auto . | ro cla gram for simulating the behavior of nc °° 20 sive a simple, jent prog . . 1 Of such an automat ox fic transition function 4) in finding occurrences of 2 pattern 'on (represented i ; P of length mi oye ext T[1-1]. As for any string-matching automaton f ith min an ie 21 (0 r-- the Sat sited a EM state > yite-AUTOMATON-MATCHER (T, 6, mt) n= Tlength q=0 6 fori = 1ton aq, TED Ope 28h q if ausens q sete print “Pattern occurs with shift” i — mp the simple loop structure of FINITE-AUTOMATON-MATCHER, we can easi eehat its matching time on a text string of length n is O(n). This matching time, howevel, does not include the Preprocessing time required to compute the transition function 5. We seinen this problem later, after first Proving that the jure FINITE-AUTOMATON-MATCHER operates correctly, Consider how the automaton operates on an input text 7... We shall prove that the automaton is in state o(Ti) after scanning character Ti]. Since o(T)) =m ifand only if P = T;, the machine is in the accepting state m if and only if it has just scanned the pattern P., To prove this result, we make use of the following two Jemmas about the suffix function o, Lemma 32.2 (Suffix-function inequality) For any string x and character a, we have o(xa) < o(x) +1. Proof Referring to Figure 32.8, let r = o(xa). If r = 0, then the conclusion o(xa) = r < a(x) + 1 is trivially satisfied, by the nonnegativity of o(x). Now assume that r > 0. Then, P, =I xa, by the definition of 6. Thus, P, x, by & Scanned with OKEN Scanner

You might also like