Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure and Algorithms - S.S. Deshmukh. Linear Search algorithm 1.[Intialize] Set K:=1, LOC:= 0 2.Repeat Step 3 & 4 while LOC:=0 & K<=N 3.If ITEM.

Similar presentations


Presentation on theme: "Data Structure and Algorithms - S.S. Deshmukh. Linear Search algorithm 1.[Intialize] Set K:=1, LOC:= 0 2.Repeat Step 3 & 4 while LOC:=0 & K<=N 3.If ITEM."— Presentation transcript:

1 Data Structure and Algorithms - S.S. Deshmukh

2 Linear Search algorithm 1.[Intialize] Set K:=1, LOC:= 0 2.Repeat Step 3 & 4 while LOC:=0 & K<=N 3.If ITEM = DATA[K], then: Set LOC:=K 4.Set K:=K+1 [Increment counter] [End of Step 2 loop] 5. [LOC found?] If LOC=0, then: Write: ITEM not found Else Write: LOC is location of ITEM [End of If structure] 6. Exit

3 Largest Element in a array 1.[Initialize] Set K:= 1, LOC:=1, MAX:= DATA[1] 2. Repeat Step 3 and 4 while K<=N 3. If MAX< DATA[K], then: Set LOC:=K, MAX:= DATA[K] [End of if structure] 4. Set K:=K+1 [End of Step 2 loop] 5. Write: LOC, MAX 6. Exit

4 Types of string storage Fixed length storage: SGBAU,AM AMRAVATI, MAHARASHTR T, A. 200 280 360 Suppose input contains sting as, ‘SGBAU, AMT, ’ ‘AMRAVATI,’ ‘MAHARASHTRA.’ And first character address is 200 and record length is 80 Record are stored in memory sequentially as above

5 Types of string storage Fixed length storage using the pointers 200 280 360 SGBAU,AM MAHARASHTR AMRAVATI, T, A. POINTER 1 2 3 4 5 200 280 360 Suppose input contains sting as, ‘SGBAU, AMT, ’ ‘AMRAVATI,’ ‘MAHARASHTRA.’

6 Types of string storage Variable length storage: 1) Use of marker to signal end of string 200 280 360 SGBAU,AM MAHARASHTR AMRAVATI,$$ T, A. POINTER 1 2 3 4 5 200 280 360 Suppose input contains sting as, ‘SGBAU, AMT, ’ ‘AMRAVATI,’ ‘MAHARASHTRA.’

7 Types of string storage Variable length storage: 2) Record length is listed 200 280 360 SGBAU,AM MAHARASHTR AMRAVATI, T, A. 200 280 360 Suppose input contains sting as, ‘SGBAU, AMT, ’ ‘AMRAVATI,’ ‘MAHARASHTRA.’ 12 9 LGH PTR

8 Types of string storage Linked list storage Suppose the string is ‘TO BE OR NOT’ Using 1 character per node T OBEOR NOT0

9 Types of string storage Linked list storage Suppose the string is ‘TO BE OR NOT’ Using 4 character per node TOB EORNOT0

10 String operation Substring Finding part of the string is done in substring SUBSTRING(String, Initial, Length) String- Name of string Initial- Position of the first character Length- Length of the substring For ex: In string ‘PRMCEAM Badnera’ we find the substring ‘Badnera’ by using SUBSTRING(‘PRMCEAM Badnera’, 9,7)  Badnera 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7

11 String operation Indexing Finding position where the pattern P first appears in the string T INDEX(String,Pattern) String- Name of string Pattern – Pattern which we want to find If the pattern does not found then INDEX is assigned the value 0 For ex: In string ‘PRMCEAM Badnera’ we find the INDEX of ‘Bad’ by using INDEX(‘PRMCEAM Badnera’, ‘Bad’)  9 1 2 3 4 5 6 7 8 9 10 11

12 String operation Concatenation For string S1 & S2, finding new string consisting of char of S1 followed by char of S2. Denoted by S1//S2 For ex: S1= ‘PRMCEAM’, S2= ‘Badnera’ we find the S1//S2  ‘PRMCEAM Badnera’

13 String operation Length The number of character in string is called its length Denoted by LENGTH(String) For ex: LENGTH(‘PRMCEAM Badnera’)  15

14 Word Processing Types of word processing: 1)Insertion: Inserting string in the middle of the given string is called the insertion operation. Given text T we want to insert string S so that S begins from postion K. Denoted by: INSERT(TEXT,POSITION,STRING) Ex: INSERT(‘ABCDEFG’, 3, ’XYZ’)  ‘ABXYZCDEFG’ INSERT(T,K,S) = SUBSTRING(T,1,K-1) // S // SUBSTRING(T,K,(LENGTH(T)-K+1))

15 Word Processing Types of word processing: 2) Deletion: Deleting substring from the given string which begins in position K & has length L. Denoted by: DELETE(TEXT,POSITION,LENGTH) Ex: DELETE(‘ABCDEFG’,4,2)  ‘ABCFG’ DELETE (T,K,S) = SUBSTRING(T,1,K-1) // SUBSTRING(T, K+L, LENGTH(T)-K-L+1)

16 Word Processing Types of word processing: 3) Replacement: In given text T we want to replace the first occurrence of pattern P1 by pattern P2 Denoted by: REPLACE(TEXT, PATTERN1, PATTERN2) Ex: REPLACE(‘XABYABZ’, ’AB’, ’C’)  ‘XCYABZ’ No replacement if P1 not found in the given string K := INDEX(T, P1) T :=DELETE(T,K,LENGTH(P1)) INSERT (T,K,P2)

17 Algorithm: A text T & pattern P & Q are in memory. This algorithm replaces every occurrence of P in T by Q Step 1: [Find index of P] Set K:= INDEX(T,P) Step 2: Repeat while K = 0 [Replace P by Q] Set T:= REPLACE(T,P,Q) [Update Index] Set K:= INDEX(T,P) [End of loop] Step 3: Write: T Step 4: Exit

18 Pattern Matching Algorithm Pattern matching algorithm is the problem of deciding whether or not the given string pattern P appears in the string text T. Characters are denoted by lowercase, can also use exponents to show repetition. ex:a 2 b 3 == aabbb Empty string is denoted by ^, lambda

19 Pattern Matching Algorithm First pattern matching We compare given pattern P with each of substring of T, moving from left to right until we get a match or dont reach end of the T. W k = substring of T having same length as P MAX = LENGTH(T) – LENGTH(P) + 1

20 Pattern Matching Algorithm First pattern matching Ex: If P is 4 char string, T is 20 char string P = P[1]P[2]P[3]P[4] T = T[1]T[2]T[3]T[4]T[5]T[6]….T[18]T[19]T[20] MAX= 20- 4 + 1 = 17 ie total 17 substring of T W1=T[1]T[2]T[3]T[4] W2=T[2]T[3]T[4]T[5]. W17=T[17]T[18]T[19]T[20] Example

21 First pattern matching algorithm Step 1. [Initialize] Set K:=1, MAX := S – R + 1 Step 2. Repeat Step 3 to 5 while K<= MAX Step 3. Repeat for L = 1 to R [Test each character of P] If P[L] = T [K+L-1], then Goto Step 5 [End of inner loop] Step 4. [Success] Set INDEX:= K and Exit Step 5. Set K:=K+1 [End of Step 2 outer loop] Step 6. [Failure] Set INDEX :=0 Step 7. Exit

22 First pattern matching algorithm Ex 3.9, Pg no 3.17 Use slow pattern matching algorithm to find number of comparisons C and also find INDEX of P in text T. a) P = aaba and T = cdcd..cd = (cd) 10 b) P = aaba and T = abababab…. c) P = aaab and T = aa….aaa= a 20

23 Second pattern matching algorithm


Download ppt "Data Structure and Algorithms - S.S. Deshmukh. Linear Search algorithm 1.[Intialize] Set K:=1, LOC:= 0 2.Repeat Step 3 & 4 while LOC:=0 & K<=N 3.If ITEM."

Similar presentations


Ads by Google