Space and Time Trade Off
Space and Time Trade Off
Space and Time Trade Off
1) Hashing
2) Indexing With B-trees
*Efficiency Of Algorithm.
Text book Pdf Page no.255
See only c(n) = part
Assume, for example, that we have to sort a list whose values can
be either 1 or 2. Rather than applying a general sorting algorithm,
we should be able to take advantage of this additional information
about values to be sorted.
Indeed, we can scan the list to compute the number of 1’s and the
number of 2’s in it and then, on the second pass, simply make the
appropriate number of the first elements equal to 1 and the
remaining elements equal to 2.
More generally, if element values are integers between some lower
bound l and upper bound u, we can compute the frequency
of each of those values and store them in array F [0..u − l].
Then the first F [0] positions in the sorted list must be filled with l,
the next F [1] positions with l + 1, and so on.
All this can be done, of course, only if we can overwrite the given
elements.
Then we can copy elements into a new array S[0..n − 1]to hold the
sorted list as follows.
for j ← 0 to u − l do D[j ] ← 0
//initialize frequencies
for i ← 0 to n − 1 do D[A[i] − l] ← D[A[i] − l] + 1 //compute
frequencies
for j ← 1 to u − l do D[j ] ← D[j − 1] + D[j ]
//reuse for distribution
for i ← n − 1 downto 0 do
j ← A[i] − l
S[D[j ] − 1] ← A[i]
D[j ] ← D[j ] − 1
return S
*Horspool’s Algorithm :-
We can precompute shift sizes and store them in a table. The table
will be indexed by all possible characters that can be encountered
in a text, including, for natural language texts, the space,
punctuation symbols, and
other special characters.
The table’s entries will indicate the shift sizes computed by the
formula.
*Horspool’s algorithm :-
Step 1 For a given pattern of length m and the alphabet used in
both the
pattern and text, construct the shift table as described above.
Step 2 Align the pattern against the beginning of the text.
Step 3 Repeat the following 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 text until either all m characters are matched (then
stop) or a mismatching pair is encountered. In the latter case,
retrieve the entry t (c) from the c’s column of the shift table where c
is the text’s character currently aligned against the last character of
the pattern, and shift the pattern by t (c) characters to the right
along the text.
**Boyer-Moore Algorithm:-