0% found this document useful (0 votes)
171 views

DAA - Unit IV - Space and Time Tradeoffs - Lecture Slides

The document discusses space-time tradeoffs in algorithms. It describes three common techniques: input enhancement, pre-structuring, and dynamic programming. Input enhancement involves preprocessing input to accelerate later problem solving, like counting methods for sorting. Pre-structuring uses extra space to facilitate faster or more flexible data access, like hashing and B-trees. Dynamic programming optimally solves problems by storing prior results. Specific algorithms discussed include sorting by counting and string matching techniques like Boyer-Moore that use input enhancement.

Uploaded by

kennygoyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

DAA - Unit IV - Space and Time Tradeoffs - Lecture Slides

The document discusses space-time tradeoffs in algorithms. It describes three common techniques: input enhancement, pre-structuring, and dynamic programming. Input enhancement involves preprocessing input to accelerate later problem solving, like counting methods for sorting. Pre-structuring uses extra space to facilitate faster or more flexible data access, like hashing and B-trees. Dynamic programming optimally solves problems by storing prior results. Specific algorithms discussed include sorting by counting and string matching techniques like Boyer-Moore that use input enhancement.

Uploaded by

kennygoyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Design and Analysis of

Algorithms (UE18CS251)

Unit IV - Space and Time Tradeoffs


Mr. Channa Bankapur
[email protected]
Space and Time Tradeoffs

● 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

1. Comparison Counting Sorting


○ For each element of the list, count the total number of
elements smaller than this element.
○ These numbers will indicate the positions of the
elements in the sorted list.

2. Distribution Counting Sorting


○ Suppose the elements of the list to be sorted belong to
a finite set (aka domain).
○ Count the frequency of each element of the set in the
list to be sorted.
○ Scan the set in order of sorting and print each element
of the set according to its frequency, which will be the
required sorted list.
Input Enhancement
→ Sorting by Counting
→→ Comparison Counting Sorting
○ For each element of the list, count the total number of
elements smaller than the element.
○ These numbers indicates the positions (0-based) of the
elements in the sorted list.

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)?

Q: Write an efficient algorithm to sort a list of n 0s, 1s and 2s.


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)?

Q: Write an efficient algorithm to sort a list of n 0s, 1s and 2s.


Can it be done faster than O(n log n)?

Q: Write an efficient algorithm to sort a list of n elements


where the elements belong to set of natural numbers [0..100],
and 101 << n. 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)?

Q: Write an efficient algorithm to sort a list of n 0s, 1s and 2s.


Can it be done faster than O(n log n)?

Q: Write an efficient algorithm to sort a list of n elements


where the elements belong to set of natural numbers [0..100],
and 101 << n. Can it be done faster than O(n log n)?

Q: Write an efficient algorithm to sort a list of n elements


where the elements belong to set of m elements, and m << n.
Can it be done faster than O(n log n)?
Input Enhancement
→ Sorting by Counting
→→ Distribution Counting Sorting
Input Enhancement
→ Sorting by Counting
→→ Distribution Counting Sorting
Trace the algorithm for sorting 5-star ratings of 7 people.
(A, 3), (B, 5), (C, 2), (D, 3), (E, 3), (F, 5), (G, 3).
Alphabet: {1, 2, 3, 4, 5} OR {2, 3, 4, 5} with l=2, u=5.
Frequencies: 1, 4, 0, 2. ⇒ Distributions: 1, 5, 5, 7.
Sorted list: (A, 3), (B, 5), (C, 2), (D, 3), (E, 3), (F, 5), (G, 3).
1. Space complexity: O(u-l) distributions and O(n) for final
sorting.
2. Time complexity: O(n + (u-l)).
It is O(n) when u-l ∈ O(n).
Input Enhancement in String Matching:

Input Enhancement to improve the average-case/amortized


efficiency.

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.

Eg: Text with n = 18 and pattern with m = 5.


DNA Sequence: A C G T T A G C A G C G C A G C G C
Gene segment: A G C G C
NaiveStringMatch(T[0..n-1], P[0..m-1])
for i ← 0 to n-m
j ← 0
while (j < m and T[i+j] = P[j])
j ← j + 1
if(j = m) return i
return -1
---------------------------------------
NaiveStringMatch2(T[0..n-1], P[0..m-1])
for i ← m-1 to n-1
j ← 0
while (j < m and T[i-j] = P[m-1-j])
j ← j + 1
if(j = m) return i-(m-1)
return -1
NaiveStringMatch2(T[0..n-1], P[0..m-1])
for i ← m-1 to n-1
j ← 0
while (j < m and T[i-j] = P[m-1-j])
j ← j + 1
if(j = m) return i-(m-1)
return -1
---------------------------------------
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
Eg: Pattern AGCGC
Shift table: A C G T
4 2 1 5

Eg: Gene segment in DNA Sequence using Horspool’s algo.


0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
A C G T T A G C A G C G C A G C G C
A G C G C
A G C G C
A G C G C
A G C G C

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

Gene segment in DNA Sequence using Boyer-Moore’s algo.


0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
A C G T T A G C A G C G C A G C G C
A G C G C
A G C G C
A G C G C
A G C G C

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.

</ Space-Time Tradeoffs >

You might also like