Design and Analysis of Algorithms – Chapter 7
1
Space-Time Tradeoffs:
String Matching Algorithms*
Dr. Ying Lu
ylu@cse.unl.edu
RAIK 283: Data Structures AlgorithmsRAIK 283: Data Structures Algorithms
*slides referred to
http://www.aw-bc.com/info/levitinhttp://www.aw-bc.com/info/levitin
Motivation exampleMotivation example
bHow to sort array a[1], a[2], …., a[n], whose valuesare known to come from set, e.g., {a, b, c, d, e, f,…. x, y, z}?bHow to sort array a[1], a[2], …., a[n], whose valuesare known to come from set, e.g., {a, b, c, d, e, f,…. x, y, z}?
Design and Analysis of Algorithms – Chapter 7
2
Design and Analysis of Algorithms – Chapter 7
3
Space-time tradeoffsSpace-time tradeoffs
For many problems some extra space really pays off:For many problems some extra space really pays off:
bExtra space in tablesbExtra space in tables
hashinghashing
non comparison-based sorting (e.g., sorting by countingthe array a[1], a[2], …., a[n], whose values are known tocome from set, e.g., {a, b, c, d, e, f, …. x, y, z})non comparison-based sorting (e.g., sorting by countingthe array a[1], a[2], …., a[n], whose values are known tocome from set, e.g., {a, b, c, d, e, f, …. x, y, z})
bInput enhancementbInput enhancement
auxiliary tables (shift tables for pattern matching)auxiliary tables (shift tables for pattern matching)
indexing schemes (e.g., B-trees)indexing schemes (e.g., B-trees)
bTables of solutions for overlapping subproblemsbTables of solutions for overlapping subproblems
dynamic programmingdynamic programming
Design and Analysis of Algorithms – Chapter 7
4
String matchingString matching
bpatternstring of m characters to search forbpatternstring of m characters to search for
btext(long) string of n characters to search inbtext(long) string of n characters to search in
bBrute force algorithm?bBrute force algorithm?
Design and Analysis of Algorithms – Chapter 7
5
String matchingString matching
bpatternstring of m characters to search forbpatternstring of m characters to search for
btext(long) string of n characters to search inbtext(long) string of n characters to search in
bBrute force algorithm:bBrute force algorithm:
1.Align pattern at beginning of text1.Align pattern at beginning of text
2.moving from left to right, compare each character of pattern to thecorresponding character in text until2.moving from left to right, compare each character of pattern to thecorresponding character in text until
all characters are found to match (successful search); orall characters are found to match (successful search); or
mismatch is detectedmismatch is detected
3.while pattern is not found and the text is not yet exhausted, realignpattern one position to the right and repeat step 2.3.while pattern is not found and the text is not yet exhausted, realignpattern one position to the right and repeat step 2.
bTime efficiency:bTime efficiency:
Design and Analysis of Algorithms – Chapter 7
6
String matchingString matching
bpatternstring of m characters to search forbpatternstring of m characters to search for
btext(long) string of n characters to search inbtext(long) string of n characters to search in
bBrute force algorithm:bBrute force algorithm:
1.Align pattern at beginning of text1.Align pattern at beginning of text
2.moving from left to right, compare each character of pattern to thecorresponding character in text until2.moving from left to right, compare each character of pattern to thecorresponding character in text until
all characters are found to match (successful search); orall characters are found to match (successful search); or
mismatch is detectedmismatch is detected
3.while pattern is not found and the text is not yet exhausted, realignpattern one position to the right and repeat step 2.3.while pattern is not found and the text is not yet exhausted, realignpattern one position to the right and repeat step 2.
bTime efficiency: (nm)bTime efficiency: (nm)
Design and Analysis of Algorithms – Chapter 7
7
HorspoolAlgorithmHorspoolAlgorithm
bsimplified version of Boyer-Moore algorithm that retainskey insights:bsimplified version of Boyer-Moore algorithm that retainskey insights:
compare pattern characters to text from right to leftcompare pattern characters to text from right to left
given pattern, create shift table that determines howmuch to shift the pattern when mismatch occurs (inputenhancement)given pattern, create shift table that determines howmuch to shift the pattern when mismatch occurs (inputenhancement)
Design and Analysis of Algorithms – Chapter 7
8
How far to shift when mismatch occurs ?How far to shift when mismatch occurs ?
Look at first (rightmost) character in text that was compared. Three cases:Look at first (rightmost) character in text that was compared. Three cases:
The character is not in the patternThe character is not in the pattern
    .....c...................... (c not in pattern)    .....c...................... (c not in pattern)
    BAOBAB    BAOBAB
The character is in the pattern (but not at rightmost position)The character is in the pattern (but not at rightmost position)
    .....O...................... (O occurs once in pattern)    .....O...................... (O occurs once in pattern)
    BAOBAB    BAOBAB
    .....A...................... (A occurs twice in pattern)    .....A...................... (A occurs twice in pattern)
    BAOBAB    BAOBAB
The rightmost characters produced matchThe rightmost characters produced match
    .....B......................    .....B......................
    BAOBAB    BAOBAB
    .....B......................    .....B......................
    AAOAAB    AAOAAB
bShift Table: Stores number of characters to shift depending on firstcharacter comparedbShift Table: Stores number of characters to shift depending on firstcharacter compared
Shift tableShift table
bIndexed by text and pattern alphabetbIndexed by text and pattern alphabet
bIf character is not among the first m-1 characters of thepattern, then t(c) the patternlength mbIf character is not among the first m-1 characters of thepattern, then t(c) the patternlength m
bOtherwise,  t(c) the distance from the rightmost amongthe first m-1 characters of the pattern to its last characterbOtherwise,  t(c) the distance from the rightmost amongthe first m-1 characters of the pattern to its last character
Design and Analysis of Algorithms – Chapter 7
9
Design and Analysis of Algorithms – Chapter 7
10
Shift tableShift table
bConstructed by scanning pattern before search beginsbConstructed by scanning pattern before search begins
bIndexed by text and pattern alphabetbIndexed by text and pattern alphabet
All entries are initialized to length of pattern. E.g.,BAOBAB:All entries are initialized to length of pattern. E.g.,BAOBAB:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
For c occurring in the first m-1 characters of the pattern,update table entry to distance of rightmost c among thefirst m-1 characters of the pattern to its last characterFor c occurring in the first m-1 characters of the pattern,update table entry to distance of rightmost c among thefirst m-1 characters of the pattern to its last character
Design and Analysis of Algorithms – Chapter 7
11
Shift tableShift table
bConstructed by scanning pattern before search beginsbConstructed by scanning pattern before search begins
bIndexed by text and pattern alphabetbIndexed by text and pattern alphabet
All entries are initialized to length of pattern. E.g.,BAOBAB:All entries are initialized to length of pattern. E.g.,BAOBAB:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
For c occurring in the first m-1 characters of the pattern,update table entry to distance of rightmost c among thefirst m-1 characters of the pattern to its last characterFor c occurring in the first m-1 characters of the pattern,update table entry to distance of rightmost c among thefirst m-1 characters of the pattern to its last character
Shift tableShift table
bWe can create the shift table by processing pattern fromL→R:bWe can create the shift table by processing pattern fromL→R:
Algorithm ShiftTable(P[0…m-1]) {Algorithm ShiftTable(P[0…m-1]) {
initialize all the elements of Table with minitialize all the elements of Table with m
for to m-2 do Table[P[j]] m-1-jfor to m-2 do Table[P[j]] m-1-j
return Tablereturn Table
}}
Design and Analysis of Algorithms – Chapter 7
12
Design and Analysis of Algorithms – Chapter 7
13
ExampleExample
BARD LOVED BANANASBARD LOVED BANANAS
BAOBABBAOBAB
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Design and Analysis of Algorithms – Chapter 7
14
In-class exercisesIn-class exercises
bP267: 7.2.1--- Apply Horspoolalgorithm to search for thepattern BAOBAB in the textBESS_KNEW_ABOUT_BAOBABSbP267: 7.2.1--- Apply Horspoolalgorithm to search for thepattern BAOBAB in the textBESS_KNEW_ABOUT_BAOBABS
bP268: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP268: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?
0000100001
1000010000
Design and Analysis of Algorithms – Chapter 7
15
In-class exercisesIn-class exercises
bP264: 7.2.1--- Apply Horspoolalgorithm to search for thepattern BAOBAB in the textBESS_KNEW_ABOUT_BAOBABSbP264: 7.2.1--- Apply Horspoolalgorithm to search for thepattern BAOBAB in the textBESS_KNEW_ABOUT_BAOBABS
bP264: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP264: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?
00001 (1*996 996 comparisons)00001 (1*996 996 comparisons)
10000 (5*996 4980 comparisons)10000 (5*996 4980 comparisons)
Time EfficiencyTime Efficiency
bHorspoolalgorithmbHorspoolalgorithm
(nm) in the worst-case.(nm) in the worst-case.
(n) for random texts(n) for random texts
Design and Analysis of Algorithms – Chapter 7
16
In-Class ExercisesIn-Class Exercises
bYou have an array of positive integers likear[]={1,3,2,4,5,4,2}. You need to create another arrayar_low[] such that ar_low[i] number of elements lowerthan or equal to ar[i]. So the output of above should be{1,4,3,6,7,6,3}.bYou have an array of positive integers likear[]={1,3,2,4,5,4,2}. You need to create another arrayar_low[] such that ar_low[i] number of elements lowerthan or equal to ar[i]. So the output of above should be{1,4,3,6,7,6,3}.
bAlgorithm time complexity should be O(n) and use of extraspace is allowed.bAlgorithm time complexity should be O(n) and use of extraspace is allowed.
Design and Analysis of Algorithms – Chapter 7
18
Boyer-Moore algorithmBoyer-Moore algorithm
bBased on same two ideas:bBased on same two ideas:
compare pattern characters to text from right toleftcompare pattern characters to text from right toleft
given pattern, create shift table thatdetermines how much to shift the pattern whenmismatch occurs (input enhancement)given pattern, create shift table thatdetermines how much to shift the pattern whenmismatch occurs (input enhancement)
bUses additional shift table with same idea appliedto the number of matched charactersbUses additional shift table with same idea appliedto the number of matched characters
Design and Analysis of Algorithms – Chapter 7
19
In-class exercisesIn-class exercises
bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?
0000100001
1000010000
Design and Analysis of Algorithms – Chapter 7
20
In-class exercisesIn-class exercises
bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?
00001 (1*996 996 comparisons)00001 (1*996 996 comparisons)
10000 (5*200 1000 comparisons)10000 (5*200 1000 comparisons)
In-class exercisesIn-class exercises
bP264: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP264: 7.2.3--- How many character comparisons will bemade by Horspoolalgorithm in searching for each of thefollowing patterns in the binary text of 1000 zeros?
0101001010
bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?bP264: 7.2.7 How many character comparisons will  Boyer-Moore algorithm make in searching for each of thefollowing patterns in the binary text of 1000 zeros?
0101001010
Design and Analysis of Algorithms – Chapter 7
21