What is the key idea behind the Boyer-Moore Algorithm?
The goal is to skip scanning all the characters. The idea is to start matching the characters from right of the pattern to left of the pattern. You encounter a mismatch between text and pattern at a searchIndex. You go backwards and check where you could have a possible match in the pattern. If there is none then you skip all the characters and shift the searchIndex by the full length of the pattern = m. You encounter a match you shift forward by the length of the pattern and shift backward by the position of the matched character in order to align that character with the text.
Boyer-Moore functional code
**public** **static** bool BoyerMooreMatch(string text, string pattern) **{****if**(pattern.Length \> text.Length)**{****return** **false**; **}** var j = pattern.Length - 1; var i = j; var n = pattern.Length; **while** (i \< text.Length && n \> 0) **{****if**(pattern[j] == text[i])**{**j--; i--; n--;**}****else****{**// we want to align the pattern to the text // after failing to match the character we search for the occurence of the // last character in the pattern matching the text**int**k = SearchCharBackwards(pattern, j, text[i]); // we move forward the whole pattern length and move backward by the position of the found character i += pattern.Length - k - 1; j = pattern.Length - 1; n = pattern.Length;**}****}****return**n == 0;**}**Was ist die Idee hinter Knuth-Morris-Prat Algorithm?
Bei einer naiven Suche muss der Text an jeder neu gescannt werden, sobald ein Mismatch zwischen Muster und Text festgestellt wird.
Bei KMP Algorithmus möchten wir verhindern, dass wir jedes Mal den gesamten Text absuchen müssen. Dafür suchen wir nach Wiederholungen in dem Muster. An jeder Stelle im Muster möchten wir wissen, was die Länge des längsten Präfixes ist, was als Suffix vorhanden ist. Mit diesem Wissen, wissen wir, was schon gematched worden ist und können an der richtigen Stelle im Muster unsere Suche fortstetzen. Somit müssen wir wie in der naiven Suche nicht mit der Text-Position permanent zurückgehen
KMP Matching Table functional code
int[] computeLspTable(String pattern) **{** int[] lsp = **new** int[pattern.length()]; lsp[0] = 0; // Base case **for** (**int** i = 1; i \< pattern.length(); i++) **{**// Start by assuming we're extending the previous LSP // the previous lsp tells us how many characters have been matched up to the last position in the pattern [i-1] // this is also the position of the character where a potential next match within the pattern // could start **int** j = lsp[i - 1]; **while** (j \> 0 && pattern.charAt(i) != pattern.charAt(j)) j = lsp[j - 1]; **if** (pattern.charAt(i) == pattern.charAt(j)) j++; lsp[i] = j; **}****return**lsp;**}**Naive Search Compact Code
**def** **NaiveStringMatch**(s, p): m = len(p) n = len(s) i = **0** j = **0****if**(n \< m):**return**False**while**(i \< n):**if**(p[j] == s[i]): j = j +**1**i = i +**1****else**: # we substract the number of matched characters and move to the next# index in the string i = i - j + **1** j = **0****if**(j == (len(p) -**1**)):**return**True**return** False
KMP Search Algorithm
**int** search(String pattern, String text) **{** int[] lsp = computeLspTable(pattern); **int** j = 0; // Number of chars matched in pattern **for** (**int** i = 0; i \< text.length(); i++) **{****while**(j \> 0 && text.charAt(i) != pattern.charAt(j))**{**// Fall back in the pattern j = lsp[j - 1]; // Strictly decreasing**}****if** (text.charAt(i) == pattern.charAt(j)) **{**// Next char matched, increment position j++; **if** (j == pattern.length()) **return** i - (j - 1); **}****}****return** -1; // Not found **}**Was sind mögliche Präfixes und Suffixes im Sinne vom KMP Algorithmus und was ist die Idee dahinter?
Idea Boyer-Moore matching Algorithm
Was markiert j = lsp[i-1] an der Position i?
Was ist die Idee hinter dem Nearest Neighbour Algorithmus?
Nearest Neighbor functional Code
Eingabe: Sortiertes Feld natürlicher Zahlen
Ausgabe: Minimaler Abstand r
NearestNeighbor(A)
Wie kann ich eine Liste rekursiv in Teillisten splitten?