DAA - Unit IV - Space and Time Tradeoffs - Lecture Slides
DAA - Unit IV - Space and Time Tradeoffs - Lecture Slides
Algorithms (UE18CS251)
● Input Enhancement
○ preprocess the problem’s input, in whole or in part, and
store the additional information obtained to accelerate
solving the problem afterward.
○ Eg: Counting methods of sorting, Horspool’s algorithm,
Boyer-Moore’s algorithm.
● Pre-structuring
○ use of extra space to facilitate faster and/or more
flexible access to the data.
○ Eg: Hashing, B-Trees
● Dynamic Programming
Sorting by Counting
Eg: 62 31 84 96 19 47
# lesser elements: 3 1 4 5 0 2
19 31 47 62 84 96
0 1 2 3 4 5
1. Optimal number of key moves.
2. Does it sort when there are duplicate elements?
3. If so, is the sort stable?
4. What happens if the condition A[i] < A[j] is replaced with
A[i] <= A[j] ?
5. Stable, but not in-place. Takes O(n) space.
Q: Write an efficient algorithm to sort a list of n 0s and 1s.
Can it be done faster than O(n log n)?
Q: Write an efficient algorithm to sort a list of n 0s and 1s.
Can it be done faster than O(n log n)?
1. Horspool’s algorithm
2. Boyer-Moore algorithm
3. Robin-Karp algorithm
4. Finite State Automaton
5. Knuth-Morris-Pratt algorithm
Consider the problem of searching for genes in DNA
sequences.
A DNA sequence is represented by a text of the alphabet
{A, C, G, T} and the gene segment is the pattern.
Tbl[ T ] = 5
Tbl[ G ] = 1
Tbl[ C ] = 2
NaiveStringMatch3(T[0..n-1], P[0..m-1])
i ← m-1
while (i < n)
j ← 0
while (j < m and T[i-j] = P[m-1-j])
j ← j + 1
if(j = m) return i-(m-1)
i ← i+1
return -1
---------------------------------------
HorspoolMatching(T[0..n-1], P[0..m-1])
STbl[alphabet size] ← ShiftTbl(P[0..m-1])
i ← m-1
while (i < n)
j ← 0
while (j < m and T[i-j] = P[m-1-j])
j ← j + 1
if(j = m) return i-(m-1)
i ← i + STbl[ T[i] ]
return -1
Boyer-Moore Algorithm:
Bad-symbol shift
Boyer-Moore Algorithm:
Good-suffix shift
Boyer-Moore Algorithm:
Step 1: For a given pattern and the alphabet used in both the
pattern and the text, construct the bad-symbol shift table as
described earlier.
Step 2: Using the pattern, construct the good-suffix shift table
as described earlier.
Step 3: Align the pattern against the beginning of the text.
Step 4: Repeat the following step until either a matching
substring is found or the pattern reaches beyond the last
character of the text.
…
Boyer-Moore Algorithm:
Step 4: Repeat the following step until either a matching
substring is found or the pattern reaches beyond the last
character of the text. Starting with the last character in the
pattern, compare the corresponding characters in the pattern
and the text until either all m character pairs are matched (then
stop) or a mismatching pair is encountered after k ≥ 0 character
pairs are matched successfully. In the latter case, retrieve the
entry t1(c) from the c’s column of the bad-symbol table where c
is the text’s mismatched character. If k > 0, also retrieve the
corresponding d2 entry from the good-suffix table. Shift the
pattern to the right by the number of positions computed by
the formula
BoyerMooreMatching(T[0..n-1], P[0..m-1])
STbl[alphabet size] ← ShiftTbl(P[0..m-1])
GTbl[1..m-1] ← GoodSuffixTbl(P[0..m-1])
i ← m-1
while (i < n)
j ← 0
while (j < m and T[i-j] = P[m-1-j])
j ← j + 1
if(j = m) return i-(m-1)
d ← max{STbl[T[i-j]] - j, 1}
if(j>0)
d2 ← GTbl[j]
if(d2 > d) d ← d2
i ← i + d
return -1
Eg: Pattern AGCGC
Shift table: A C G T
4 2 1 5
Good-suffix table[1..4]: 5, 2, 5, 5
STbl[ T ] = 5
STbl[ G ] = 1
GTbl[2] = 2
Boyer-Moore algorithm:
Create bad-symbol shift table and good-suffix shift table for
the pattern: BAOBABAB
Boyer-Moore algorithm:
Create bad-symbol shift table and good-suffix shift table for
the pattern: BAOBABAB
A B O Others
1 2 5 8
k: d2
1: 4
2: 7
3: 2
4: 7
5,6,7: 7
Space-Time Tradeoffs in Algorithms is not the same as the
Space-Time Continuum theory of Physics.