Professional Documents
Culture Documents
Algorithm Analysis Design Lecture5 1 PowerPoint Presentation
Algorithm Analysis Design Lecture5 1 PowerPoint Presentation
Algorithm Analysis Design Lecture5 1 PowerPoint Presentation
elements easier
i.e, use extra space to facilitate faster and/or more flexible
access to the data
hashing
Sorting By counting
ALGORITHM ComparisonCountingsort(A[0..n-1])
//Sort an array by comparison counting
//Input: An array A[0..n-1] of A's elements sorted in nondecreasing
order
for i 0 to n-1 do Count[i] 0
for i 0 to n-2 do
for j i+1 to n-1 do
if A[i] <A[j]
Count[j] Count[j]+1
else Count[i] Count[i]+1
for i 0 to n-1 do S[Count[i]] A[i]
return S
Example:
51
20
60
C(n)= ?
70
12
40
10
Horspools Algorithm
A simplified version of Boyer-Moore algorithm:
preprocesses pattern to generate a shift table that determines
BARBER
The rightmost characters do match but there is another R, for this specific example
....AR......................
BARBER
[ shift by three length]
The rightmost characters do match but there is no another R, for this specific example
....AR......................
LEADER
[ shift by entire length]
6
Shift table
Shift sizes can be precomputed by the formula
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
4 2 6 6 1 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6
7
4 2 6 6 1 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6
J I M _ S A W _ M E _ I N _ A _BARBERS H O P
BARB ER
BA R BER
BAR BER
BARBER
BAR BE R
BARBER
Boyer-Moore algorithm
Based on same two ideas:
comparing pattern characters to text from right to left
precomputing shift sizes in two tables
k matches
matched
d2(k) = the distance between matched suffix of size k and its
rightmost occurrence in the pattern that is not preceded by the
same character as the suffix
Example: CABABA d2(1) = 4
If there is no such occurrence, match the longest part of the k-
12
13
1 2 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6 6 6 6
B E S S _ K N E W _ A B O U T _ B A O B A B S
B A O B A B
d1 = t1(K) = 6
B A O B A B
d1 = t1(_)-2 = 4
d2(2) = 5
pattern d2
B A O B A B
BAOBAB 2
d1 = t1(_)-1 = 5
BAOBAB 5
d2(1) = 2
B A O B A B (success)
BAOBAB 5
BAOBAB
BAOBAB
k
1
14
15
Hashing
A very efficient method for implementing a dictionary, i.e., a set
find (Search)
insert
delete
Each element in the set contains a key and satellite data (the
remainder of the record.)
The keys are unique, but the satellite data are not.
ideas
Important applications:
symbol tables
databases (extendible hashing)
16
17
Collisions
should be expected
18
h(K)
6
3
10
5
MONEY
ARE
SOON
PARTED
11
11
12
AND MONEY
10
11
19
12
21
A2
0
B9
0
A3
B2
A5
4
A2
A3
B2
A5
5
B5
6
B5
6
A9
7
C2
7
A9
8
Avoids pointers
Deletions are not straightforward
Number of probes to find/insert/delete a key depends on load
22