1. Write a pseudocode function R1(key, a, b, A, B, N) that takes non-negative integers key, a and b where a and b come from to the hashing function used to store indices in B; the arrays A and B of length N are also inputs: the function should return an index i where the value key is stored in array A if found; it should return -1 if the value cannot be found; otherwise it should return -2 if there was an error in the data storage, i.e. one of the values was altered unintentionally in at most one of the arrays.  2. Briefly explain how the method above could be adapted to allow for repeated integers in array A.

icon
Related questions
Question

THE THIRD PART: THE R1 SEARCH ALGORITHM

In this next scenario, inspired by RAID 1, we will just duplicate all the given data using hashing. Given an array A of non-negative integers of length N, a second array B of length N is created: for each element i of A, the value A[i] is hashed to give an index j of B, and the value A[i] is stored in B[j]. For the hashing function, given constants a>0 and b, for each i, the value A[i] is hashed by the function (a*A[i] + b) mod N, which is equal to the index of B into which we store i. The following example demonstrates this: (attached in add image)

In this example the hash function is (3*A[i] + 1) mod 4. For instance, the value 5 stored at index1inAis hashed to the value(3*5 + 1) mod 4=0, and thus5is stored atB[0]. In this example there were no collisions in the hashed values since all values in A are distinct.

For this task we assume there are no repeated values in A, and a hash function is chosen so there are no collisions for distinct values.

To look for errors we hash all the values in array A to see if there is a match in the corresponding element of array B. If there is a mismatch between the two elements then an error is returned; if no errors are found, if a value is found then its index in A is returned.

Task 4: Complete the following:

1. Write a pseudocode function R1(key, a, b, A, B, N) that takes non-negative integers key, a and b where a and b come from to the hashing function used to store indices in B; the arrays A and B of length N are also inputs: the function should return an index i where the value key is stored in array A if found; it should return -1 if the value cannot be found; otherwise it should return -2 if there was an error in the data storage, i.e. one of the values was altered unintentionally in at most one of the arrays. 

2. Briefly explain how the method above could be adapted to allow for repeated integers in array A. 

A: 7
564
2
0 1
B: 5 47
0 1 2
3
6
3
Transcribed Image Text:A: 7 564 2 0 1 B: 5 47 0 1 2 3 6 3
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer