Best Algorithm Homework Help
Best Algorithm Homework Help
(a) Insert integer keys A = [47, 61, 36, 52, 56, 33, 92] in
order into a hash table of size 7 using the hash
function h(k) = (10k + 4) mod 7. Each slot of the hash
table stores a linked list of the keys hashing to that
slot, with later insertions being appended to the end
of the list. Draw a picture of the hash table after all
keys have been inserted.
Solution:
(b) Suppose the hash function were instead h(k) = ((10k + 4) mod c)
mod 7 for some positive integer c. Find the smallest value of c such
that no collisions occur when inserting the keys from A.
Solution:
k 47 61 36 52 56 33 92
10k + 4 474 614 364 524 564 334 924
10k + 4 mod 7 mod 7 5 5 0 6 4 5 0
10k + 4 mod 8 mod 7 2 6 4 4 4 6 4
10k + 4 mod 9 mod 7 6 2 4 2 6 1 6
10k + 4 mod 10 mod 7 4 4 4 4 4 4 4
10k + 4 mod 11 mod 7 1 2 1 0 3 4 0
10k + 4 mod 12 mod 7 6 2 4 1 0 3 0
10k + 4 mod 13 mod 7 6 3 0 4 5 2 1
The table above was generated using this python code:
Rubric:
• 3 point for c = 13
• Partial credit may be awarded if there is work shown of a
correct approach that does not yield the correct solution.
Problem 2.
MIT wants to find students quickly given their IDs, so will assign
students to rooms by
hashing their IDs to a room number. So as not to appear
biased, MIT will publish a family H of hash functions online
before the start of term (before new students choose their
IDs), and then after students choose IDs, MIT will choose a
rooming hash function uniformly at random from H.
Solution:
Rony and Tiri can choose any two IDs k1, k2 such that k1 ≡
k2 mod n, which are not difficult to find. For example, Rony
could choose k1 = 3 and Tiri could choose k2 = 2n + 3. Then,
ak1 + b ≡ ak2 + b mod n for all a, b, so Rony and Tiri are
guaranteed to hash to the same room.
(b) H = ha(k) = kn + a mod n | a ∈ {0, . . . , u - 1} u
Solution:
Solution:
For any two keys, the probability that they collide given a
random function from a universal hash family is at most m 1
where m is the number of possible hash outputs. Hence, in
this case n 1 is the highest probability that Rony and Tiri can
achieve, and cannot guarantee that they will be roommates.
Rubric:
Ice cores are long cylindrical plugs drilled out of deep glaciers,
which are accumulations of snow piled on top of each other and
compressed into ice. Scientists can divide an ice core into distinct
slices, each representing one year of deposits. For each of the
following scenerios, describe an efficient1 algorithm to sort n
slices collected from multiple ice cores. Justify your answers.
Solution:
Solution:
Solution:
Rubric:
Solution:
Solution:
Replace each bi in B with the tuple (bi, i), to keep track of the
index of the box in the original order. We do not know whether
every bi is polynomially bounded in n; but we do know that r is. If
some bi ≥ r, it can certainly not be part of a pair from B that fulfills
order r. So perform a linear scan of B and remove all (bi, i) for
which bi ≥ r, to construct set B0 . Now the ream count integers bi
in B0 are each upper bounded by O(n2), so we can sort the tuples
in B0 by their ream counts bi in worst-case O(n + n logn n2) = O(n)
time using radix-sort, and store the output in an array A.
• A[i].b + A[j].b = r: a pair that fulfills the order has been found.
Check whether |A[i].x � A[j].x| < n/10 and return True if so; or
• A[i].b + A[j].b < r: A[i].b cannot be part of a pair that fulfills the
order with any A[k].b for k ∈ {i + 1, . . . , j}, so increase i; or
• A[i].b + A[j].b > r: A[j].b cannot be part of a pair that fulfills the
order with any A[k] for k ∈ {i, . . . , j - 1}, so decrease j.
This loop maintains the invariant that at the start of each loop, we
have confirmed that no pair (A[k].b, A[`].b) is close and fulfills the
order, for all k ≤ i ≤ j ≤ `, so if we reach the end without returning a
valid pair, the algorithm will correctly conclude that there is none.
Since each iteration of the loop takes O(1) time and decreases j -i
decrease by one, and j -i = |B0 |-1 starts positive and ends when j -
i < 0, this procedure takes at most O(n) time in the worst case.
Problem 5.
Solution:
Solution:
1 ORD_A = ord(’a’)
2 def lower_ord(c): # map a lowercase letter to range(26)
3 return ord(c) - ORD_A
4
5 def count_anagram_substrings(T, S):
6 m, n, k = len(T), len(S), len(S[0])
7 D = {} # map from freq tables to occurances
8 F = [0] * 26 # initial freq table
9 for i in range(m): # compute T freq tables
10 F[lower_ord(T[i])] += 1 # add character
11 if i > k - 1:
12 F[lower_ord(T[i - k])] -= 1 # remove character
13 if i >= k - 1:
14 key = tuple(F) 1
5 if key in D: # increment occurance
16 D[key] += 1 17 else: # add freq table to map 1
8 D[key] = 1
19 A = [0] * n # compute anagram substring counts
20 for i in range(n):
21 F = [0] * 26
22 for c in S[i]: # compute S_i freq table
23 F[lower_ord(c)] += 1
24 key = tuple(F)
25 if key in D: # check in dictionary
26 A[i] = D[key]
27 return tuple(A)