1 / 58

Chapter 2.8

Chapter 2.8. Search Algorithms. Array Search An array contains a certain number of records Each record is identified by a certain key One searches the record with a given key String Search A text is represented by an array of characters

cynara
Download Presentation

Chapter 2.8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2.8 Search Algorithms

  2. Array Search An array contains a certain number of records Each record is identified by a certain key One searches the record with a given key String Search A text is represented by an array of characters One searches one or all occurrences of a certain string Search Algorithms

  3. Array Search An array contains a certain number of records Each record is identified by a certain key One searches the record with a given key String Search A text is represented by an array of characters One searches one or all occurrences of a certain string Search Algorithms

  4. PROCEDURE Search Two parameters: The Array to be searched Contains n Items Index in the range 0..n Item with index 0 is not used The Key of the Item to be found Search returns a Cardinal value the index of the Item where the key has been found if the key has not been found , 0. Array Search

  5. TYPE ArrayOfItems = ARRAY[0..n] OF Item; (* By convention, the element with index 0 is not used *) Item = RECORD Key : KeyType (* any ordinal type *); Other record fields ; END; Array Search

  6. Search ( PROCEDURE VAR A: ARRAYOF Item, Key: KeyType) : CARDINAL; VAR i : CARDINAL; BEGIN i := HIGH(A); DEC(i) WHILE A[i].Key # Key AND i # 0 DO END; RETURN i END Search; Straight Search

  7. Key: KeyType) : CARDINAL; VAR i : CARDINAL; WHILE A[i].Key # Key AND i # 0 DO RETURN i END Search; Straight Search Search ( PROCEDURE VAR A: ARRAYOF Item, BEGIN i := HIGH(A); DEC(i) END;

  8. PROCEDURE Search ( VAR A: ARRAY OF Item, Key: KeyType) : CARDINAL; VAR i : CARDINAL; BEGIN i := HIGH(A); A[0].Key := Key; WHILE A[i].Key # Key DO DEC(i) END; RETURN i END Search; Sentinel Search

  9. Binary Search # elements > 1 Yes No Key < Keymiddle Key = Keyelement Yes Yes No No Binary Search right half Binary Search left half Not Found Found # elements > 1 Binary Search

  10. Binary Search (1) PROCEDURE Search(VAR a: ARRAY OF Item, Key:KeyType):CARDINAL; VAR Min,Max,m: CARDINAL; PROCEDURE src(Min,Max: CARDINAL); … END src; BEGIN Min := 1; Max := HIGH(a); src(Min,Max); IF a[m].Key = Key THEN RETURN m ELSE RETURN 0 END END Search;

  11. Binary Search (2) PROCEDURE Src(Min,Max : CARDINAL); BEGIN m := (Min+Max) DIV 2; IF Min # Max THEN IF a[m].Key >= Key THEN src(Min,m) ELSE src(m+1,Max) END; END END Src;

  12. Iterative Binary Search PROCEDURE Search(VAR a: ARRAY OF Item, Key:KeyType):CARDINAL; VAR Min,Max,m: CARDINAL; BEGIN Min := 1; Max := HIGH(a); WHILE Min < Max DO m := (Min+Max) DIV 2; IF a[m].Key >= Key THEN Max := m ELSE Min := m+1 END; (* IF *) END; (* WHILE *) IF a[m].Key = Key THENRETURN m ELSERETURN 0 END END Search;

  13. Unordered Array Straight search : 2n Sentinel search : n Ordered Array Binary search : log 2 n Array Search Performance(Number of comparisons)

  14. Array Search An array contains a certain number of records Each record is identified by a certain key One searches the record with a given key String Search A text is represented by an array of characters One searches one or all occurrences of a certain string Search Algorithms

  15. The problem: Find a given string in a text. Data structures: Text : ARRAY[1..TextSize] OF CHAR; String : ARRAY[1..StringSize] OF CHAR; The Algorithms: Brute Force Knuth, Morris & Pratt (KPM - 1974) Boyer & Moore (BM - 1977) String Search

  16. WHILE current char.in Text # String[1] Move to next character in Text End of Text reached ? No WHILE char. in Text = char. in String Move to next character pair String matched OR Text exhausted String Searchby Brute Force this algorithm tries to find a string string string string

  17. Brute Force String Search(Straightforward coding) PROCEDURE Search (VAR Text: TextType; TLength:CARDINAL; VAR String: StringType; SLength:CARDINAL):CARDINAL; VAR j, jmax : CARDINAL; BEGIN j := 0; jmax := TLength - SLength; REPEAT WHILE (Text[j] # String[1]) AND (j <= jmax) DO j := j+1 END; IF j <= jmax THEN i := 2; WHILE (Text[j+i] = String[i]) AND (i < SLength) DO i := i+1 END; END; (* IF *) j := j + 1; UNTIL (i = SLength) OR (j > jmax); RETURN j - 1 END Search;

  18. String Search (1)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in first position of string, Move string 1 position in text

  19. String Search(2)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in fourth position of string, Move string 4 positions in text

  20. String Search(3)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in fifth position of string, Move string 4 positions in text

  21. String Search(4)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in first position of string, Move string 1 position in text

  22. String Search(5)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in first position of string, Move string 1 position in text

  23. String Search(6)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in second position of string, Move string 1 position in text

  24. String Search(7)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT Mismatch in eight position of string, Move string 3 positions in text

  25. String Search(8)by the KMP algorithm GATCGATCAGCAATCATCATCACATC ATCATCACAT String found !

  26. Step: 1 ? ? ? ? ? ? ? ? ? The KMP algorithmThe Next function x x x A x x x x x x x x x x x x x / A T C A T C A C A T A T C A T C A C A T

  27. Step: 1 1 ? ? ? ? ? ? ? ? The KMP algorithmThe Next function / x x x A T x x x x x x x x x x x x A T C A T C A C A T A T C A T C A C A T

  28. Step: 1 1 2 ? ? ? ? ? ? ? The KMP algorithmThe Next function / x x x A T C x x x x x x x x x x x A T C A T C A C A T A T C A T C A C A T

  29. Step: 1 1 2 4 ? ? ? ? ? ? The KMP algorithmThe Next function / x x x A T C A x x x x x x x x x x A T C A T C A C A T A T C A T C A C A T

  30. Step: 1 1 2 4 4 ? ? ? ? ? The KMP algorithmThe Next function / x x x A T C A T x x x x x x x x x A T C A T C A C A T A T C A T C A C A T

  31. Step: 1 1 2 4 4 5 ? ? ? ? The KMP algorithmThe Next function / x x x A T C A T C x x x x x x x x A T C A T C A C A T A T C A T C A C A T

  32. Step: 1 1 2 4 4 5 7 ? ? ? The KMP algorithmThe Next function / x x x A T C A T C A x x x x x x x A T C A T C A C A T A T C A T C A

  33. Step: 1 1 2 4 4 5 7 3 ? ? The KMP algorithmThe Next function / x x x A T C A T C A C x x x x x x A T C A T C A C A T A T C A T C A C A T

  34. Step: 1 1 2 4 4 5 7 3 9 ? The KMP algorithmThe Next function / x x x A T C A T C A C A x x x x x A T C A T C A C A T A T C A T

  35. Step: 1 1 2 4 4 5 7 3 9 9 The KMP algorithmThe Next function / x x x A T C A T C A C A T x x x x A T C A T C A C A T A T C A T

  36. String: A T C A T C A C A T Step: 1 1 2 4 4 5 7 3 9 9 Next: 0 1 1 0 1 1 0 5 0 1 The KMP algorithmThe Next function i = 1 2 3 4 5 6 7 8 9 10 Next[i] = i – Step[i]

  37. Computation of the Next table i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *)

  38. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 1 String A T C A T C A C A T Next 0 k : 0

  39. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 1 String A T C A T C A C A T Next 0 k : 0

  40. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 1 > 2 String A T C A T C A C A T Next 0 1 k : 0 > 1

  41. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 2 String A T C A T C A C A T Next 0 1 k : 1 > 0

  42. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 2 > 3 String A T C A T C A C A T Next 0 1 1 k : 0 > 1

  43. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 3 String A T C A T C A C A T Next 0 1 1 k : 1 > 0

  44. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 3 > 4 String A T C A T C A C A T Next 0 1 1 0 k : 0 > 1

  45. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 4 String A T C A T C A C A T Next 0 1 1 0 k : 1

  46. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 4 > 5 String A T C A T C A C A T Next 0 1 1 0 k : 1 > 2

  47. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 5 String A T C A T C A C A T Next 0 1 1 0 1 k : 2

  48. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 5 > 6 String A T C A T C A C A T Next 0 1 1 0 1 1 k : 2 > 3

  49. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 6 String A T C A T C A C A T Next 0 1 1 0 1 1 k : 3

  50. The KMP algorithmBuilding the Next function i := 1; k := 0; Next[1] := 0; WHILE i < SLength DO WHILE (k > 0) AND (String[i]#String[k]) DO k := Next[k] END; (* WHILE *) k := k + 1; i := i + 1; IF String[i] = String[k] THEN Next[i] := Next[k] ELSE Next[i] := k END; (* IF *) END; (* WHILE *) i : 6 > 7 String A T C A T C A C A T Next 0 1 1 0 1 1 0 k : 3 > 4

More Related