Content deleted Content added
m Replace magic links with templates per local RfC and MediaWiki RfC |
Tom.Reding (talk | contribs) m WP:GenFixes on, typo(s) fixed: … → ... (3), doesn’t → doesn't (4), Horspool’s → Horspool's (7) |
||
Line 46:
The [[brute-force search|brute-force]] algorithm for this problem would perform as follows:
When presented with a string of ''n'' characters, often called the key or pattern, the string would be compared to every single character of a longer string ''m'', often called the text. If a matched character occurs, it checks the second character of the key to see if it matches. If it does, the next character is checked and so on until the string matches or the subsequent character
This algorithm is extremely inefficient. The maximum number of check trials would be ''m''-''n''+1 trials, making the big-O efficiency at worst case O(''mn''). On average case, the maximum number of check trials would never be reached and only a few would be executed, resulting in an average time efficiency of O(''m''+''n'').
Line 53:
===Horspool's Algorithm===
As a demonstration of input enhancement in string matching, one should examine a simplified version of the Boyer-Moore algorithm, [[Boyer–Moore–Horspool algorithm|
'''Case 1''':
Line 62:
'''Case 3''':
The third possible case is that the character ''x'' matches with the last character in the key but the other characters
'''Case 4''':
The fourth and last possible case is that character ''x'' matches the key but the other characters
This may seem like it is not more efficient than the brute-force algorithm since it has to check all of the characters on every check. However, this is not the case.
'''algorithm''' shiftTableGenerator(K[0...''n''-1])
Line 79:
|+ Shift Table for 'POTATO'
! character ''x''
| A || B || C ||
|-
! shift value
Line 85:
|}
After the shift table is constructed in the input enhancement stage, the algorithm lines up the key and starts executing. The algorithm executes until a matching [[substring]] of text ''m'' is found or the key overlaps the last characters of text ''m''. If the algorithm encounters a pair of characters that do not match, it accesses the table for the
'''algorithm''' HorspoolsAlgorithm(K[0...''n''-1]), M[0...''m''-1])
Line 100:
'''return''' “Key not found”
Although it may not be evident, the worst case runtime efficiency of this algorithm is O(''mn''). Fortunately, on texts that are random, the runtime efficiency is linear, O(''n/m''). This places
== Related Concepts ==
|