Hash tables Flashcards

1
Q

What is hashing?

A

Mapping an object or number onto a range of integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is collision?

A

When multiple object hash to the same value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are 4 necessary properties of a hash function?

A
  1. Fast, should take O(1)
  2. Hash function must be deterministic
  3. Equal objects should hash to the same slot
  4. If two objects are randomly chosen, there is a 1/2^{32} chance they have the same hash value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of a predetermined hash function.

A

Suppose you have a static member variable (available across all instances) to keep track of the hash value. Every time you create an object, the object gets the current hash value incremented by 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an issue with the predetermined hash function example?

A

The hash value assigned is not tied to the object itself, but to the construction process, i.e. if we save the object to a secondary memory (e.g. write to file or disk), when we reload the data, the constructor will run again and the object will be assigned a different hash value this time, even though it is the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using predetermined hash functions, each object gets a unique hash value. What is a scenario where this is not appropriate?

A
  1. When we have two strings with the same characters
  2. When we have two objects that are conceptually equal, e.g. two rationals 1/2 and 3/6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to solve the issue of two logically equal rationals not hashing to the same value?

A

Divide both numerator and denominator by gcd to reduce the fraction to its simplest form. Then apply the hash function. One example is take the product of the numerator and the denominator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is just adding the characters in a string a bad way to hash a string?

A

It takes a long runtime, hashes “ABC” to the same slot as “BCA” (ignores all ordering of words), poor distribution (long strings will always hash to bigger values and long strings might produce hash values that cluster near certain regions of the hash space)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Suggest a way to hash strings efficiently that also preserves ordering.

A

Represent the string as a polynomial p(x)=c_0x^{n-1}+c_1x^{n-2}+…+c_{n-1} where each coefficient is an individual character. Pick a prime number and let that be x to evaluate p(x). Use Horner’s rule to re-order the equation to compute p(x) more efficiently. The result is a single integer, which is the hash value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is hashing strings this way good?

A
  1. Using a prime number ensures good distribution of hash values
  2. Order-sensitive, preserves information of the string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do we want to study asymptotic?

A

We want a way to analyze an algorithm and compare their performances regardless of the computer it is run on. It helps us learn about the scalability of algorithms, independent of hardware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we think of f(n) ~ g(n) and f(n) < g(n)?

A

f(n)~g(n) means their performances are comparable for large values of n and improving hardware will affect both algorithms similarly. It is always possible to buy a faster computer so one algorithm will run faster than the other.

f(n)<g(n) means f(n) grows asymptotically slower than g(n). No matter how fast a computer becomes, f(n) will always run faster than g(n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is o(.) weaker than O(.)?

A

No, o(.) is stricter than O(.). Small-o requires that f(n) grows strictly slower than g(n) as n tends to infinity.

Big-o is an upper bound indicating that f(n) does not grow faster than g(n) for sufficiently large n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is small-omega stricter than big-omega?

A

For small-omega, f(n) grows strictly faster than g(n), while for big-omega, f(n) grows at least as fast as g(n), up to a constant factor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly