Ada Chapt7 Space and Time Trade Offs
Ada Chapt7 Space and Time Trade Offs
ALGORITHMS
UNIT-III
CHAPTER 7:
SPACE AND TIME TRADEOFFS
1
OUTLINE
Space and Time Tradeoffs
Sorting by Counting
Hashing
Open Hashing (Separate Chaining)
Closed Hashing (Open Addressing)
2
Space and Time Tradeoffs
The two techniques based on pre-processing of data thereby
increasing the speed of the algorithm are:
• Input enhancement
• Pre-structuring
These numbers will indicate the positions of the elements in sorted list:
Example: If the count is 3 for some element, it should be in the 4th
position in the sorted array.
Thus, we will be able to sort the list by simply copying its elements to
their appropriate positions in a new, sorted list.
0 1 2 3 4 5 4
S: sorted Elements 10 15 18 20 35 40
Sorting by Counting
ALGORITHM ComparisonCountingSort(A[0 . . . n-1])
//Sorts an array by comparison counting
//Input: An array A[0 . . . n-1] of orderable elements
//Output: Array S[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 5
Tracing of Comparison counting Sort
Let us illustrate the working of this algorithm by taking the elements 20, 35,10, 18,
40, 15. The outermost loop varies from 0 to n-2 and thus total number of passes
required will be n-1. In this example, since there are 6 elements, we require 5
passes. The figure below provides the number of elements less than the
corresponding item at each pass.
0 1 2 3 4 5
Array A[0 . . . 5] 20 35 10 18 40 15
Initially Count[ ] 0 0 0 0 0 0
After pass i = 0 Count[ ] 3 1 0 0 1 0
After pass i = 1 Count[ ] 4 0 0 2 0
After pass i = 2 Count[ ] 0 1 3 1
After pass i = 3 Count[ ] 2 4 1
After pass i = 4 Count[ ] 5 1
Final state Count[ ] 3 4 0 2 5 1
0 1 2 3 4 5 6
The basic operation is comparison statement “if A[i] < A[j]” in the
innermost for loop.
7
Sorting by Distribution counting
Store the frequency of occurrence of each element in an array.
Then we can copy elements into new array S[0. . . n-1] to hold sorted list as
follows:
Array A elements whose values are equal to lowest value l are copied into the
first F[0] elements of S, i.e., positions 0 through F[0]-1, next higher elements
are copied to positions from F[0] to (F[0]+F[1])-1, and so on.
whose values are known to come from the set {11, 12, 13} and should not
be overwritten in the process of sorting.
The frequency and distribution arrays are as follows:
Array values 11 12 13
Frequencies 1 3 2
Distribution values 1 4 6 8
Sorting by Distribution counting
Note that the distribution values indicate the proper positions for the
last occurrences of their elements in the final sorted array. If we index
array positions from 0 to n-1, the distribution values must be reduced
by 1 to get corresponding element positions.
Input array 13 11 12 13 12 12
S[0 . . . 5]
D[0 . . . 2]
Index value 0 1 2 0 1 2 3 4 5
A[5] = 12 1 4 6 12
A[4] = 12 1 3 6 12
A[3] = 13 1 2 6 13
A[2] = 12 1 2 5 12
A[1] = 11 1 1 5 11
A[0] = 13 0 1 5 13
11
Analysis of sorting by distribution counting
The input size metric for this algorithm is n.
12
Input Enhancement in String Matching
The pattern matching algorithm using Brute-force method had the
worst-case efficiency of Θ(mn) where m is the length of the pattern
and n is the length of the text. In the average-case, its efficiency turns
out to be in Θ(n).
Moore algorithm. 13
Horspool’s Algorithm
A simplified version of Boyer-Moore algorithm:
14
How far to shift?
In general, the following four possibilities can occur :
Case 1: Look at first (rightmost) character in text that was compared:
The character is not in the pattern
s0...…....S...............................................sn-1 (S not in pattern)
BAOBAB
BAOBAB
If there are no c’s in the pattern – eg., c is letter S in above example,
we can safely shift the pattern by its entire length.
Case 4: Finally, if c happens to be the last character in the pattern and there are other
c’s among its first m-1 characters, the shift should be similar to that of
Case 2: the rightmost occurrence of c among the first m-1 characters in the
pattern should be aligned with the text’s c.
s0...………………..OR...............................................sn-1
REORDER
REORDER
Example: For the pattern BAOBAB, all the table’s entries will be equal to 6,
except for the entries A, B, O, which will be 1, 2 and 3 respectively.
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
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
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
Important applications:
symbol tables – a table of computer program symbols
generated during compilation.
databases –hashing is useful for storing very large
dictionaries on disks; this variation of hashing is called
extendible hashing. 23
Hash tables and hash functions
Here, we assume that we have to implement a dictionary of n records
with keys K1, K2, . . . , Kn.
Hashing is based on the idea of distributing keys among a one-
dimensional array H[0…m-1] called hash table.
The distribution is done by computing, for each of the keys, the value
of some predefined function h called the hash function. This function
assigns an integer between 0 and m-1, called the hash address.
Example: student records, key = SSN.
Hash function: h(K) = K mod m where m is some integer
(typically, prime)
If m = 37, where is record with SSN= 314159265 stored?
24
Collisions
If we choose a hash table’s size m to be smaller than the number of keys n,
we will get collisions – a phenomenon of two ( or more) keys being hashed
into the same cell of the hash table.
Ki Kj
. . . . . .
0 b m-1
0 1 2 3 4 5 6 7 8 9 10 11 12
separate chaining
Open Hashing( Separate chaining)
To search for a specific key:
Example: If we want to search for the key KID in the hash table, we first
compute the value of the hash function for the key: h(KID)=11.
Since the list attached to cell 11 is not empty, its linked list may
contain the search key. After comparing the string KID first
with the string ARE and then with the string SOON, we end
up
with an unsuccessful search.
Load α is typically kept small (ideally, about 1). Having it too small
would imply a lot of empty lists and hence inefficient use of space;
having it too large would mean longer linked lists and hence longer
search times.
28
Closed hashing (Open addressing)
All keys are stored inside a hash table without the use of linked lists.
(This implies that the table size m must be atleast as large as the number of keys n.)
0 1 2 3 4 5 6 7 8 9 10 11 12
A
A FOOL
A AND FOOL
A AND FOOL HIS
A AND MONEY FOOL HIS
A AND MONEY FOOL HIS ARE
A AND MONEY FOOL HIS ARE SOON
PARTED A AND MONEY FOOL HIS ARE SOON
29
Figure: Example of a hash table construction with linear probing.
Closed Hashing (Open Addressing)
Different strategies can be employed for collision resolution.
Linear probing: This strategy checks the cell following the one
where the collision occurs. If that cell is empty, the new key is
installed there; if the next cell is already occupied, the availability of
that cell’s immediate successor is checked, and so on. Note that if
the end of the hash table is reached, the search is wrapped to the
beginning of the table; i.e, it is treated as a circular array.
o To search for a given key K, we start by computing h(k) where h is
the hash function used in the table’s construction. If the cell h(k) is
empty, the search is unsuccessful. If the cell is not empty, we must
compare K with the cell’s occupant: if they are equal, we have
found a matching key; if they are not, we compare K with a key in
the next cell and continue in this manner until we encounter either a
matching key (a successful search) or an empty cell (unsuccessful
search).
30
Closed hashing (cont.)
Does not work if n > m
Avoids pointers
Deletions are not straightforward
Number of probes to find/insert/delete a key depends on load
factor α = n/m (hash table density) and collision resolution strategy.
For linear probing:
S ≈ (½) (1+ 1/(1- α)) and U ≈ (½) (1+ 1/(1- α)²)
As the table gets filled (α approaches 1), number of probes in linear
probing increases dramatically:
31
End of Chapter 7
32