<p>This answer is a bit hand-wavey.</p>

<p>Select a function $h$ uniformly at random from the set of all functions from w-bit words to w-bit words. For each set, maintain a w-bit fingerprint as follows:</p>

<ol>
<li>The empty set has the fingerprint 0.</li>
<li>add(x) and remove(x) update a fingerprint $f$ to $f \oplus h(x)$, where $\oplus$ is xor.</li>
</ol>

<p>Let $S \neq T$ be two sets of w-bit integers. If their fingerprints are the same, then the fingerprint of $S \triangle T$, the symmetric difference of $S$ and $T$, is 0, which happens with probability $2^{-w}$.</p>

CC BY-SA 3.0
2014-12-27T02:32:42.847Z
And in practice you could instantiate $h$ with a pseudorandom function (PRF) from cryptography -- e.g., something based on AES.  Should be very efficient, and you get strong promises that it will work (unless the crypto is broken).
D.W.
CC BY-SA 3.0
2014-12-24T15:45:45.083Z
2014-12-24T15:45:45.083Z
jbapple
<p>Does there exist a w-bit word-RAM data structure with O(1) time per operation for the following problem?:  Maintain a set of w-bit non-negative integers that supports the operations</p>

<ul>
<li>add(x) : add x to the set</li>
<li>remove(x) : remove x from the set</li>
<li>fingerprint() : return a fingerprint of the set.  This w-bit fingerprint has  the property that two sets that are identical have the same fingerprint while two sets that are different probably have different fingerprints</li>
</ul>

<p>All operations should run in constant time.</p>

<p>The Rabin-Karp fingerprinting scheme, where $$f(S) = \left(\sum_{x\in S} 2^x\right) \bmod p$$ where p is a random w-bit prime almost works.  The problem is with the update time, since computing $$2^x \bmod p$$ takes more than constant time.  Using repeated squaring, this can be done in O(log w) time. The fastest modular exponentiation algorithm I could find does something like (log w)/(loglog w) arithmetic operations.</p>

CC BY-SA 3.0
2011-06-27T14:26:31.163Z
I see that a similar question was [posted here already](http://cstheory.stackexchange.com/questions/3390/is-there-a-hash-function-for-a-collection-i-e-multi-set-of-integers-that-has/), but no constant-time solution was given.
Pat Morin
<ds.data-structures>
Fingerprinting for dynamic sets
Pat Morin
# Fingerprinting for dynamic sets

(Asked by: username_0 on 2011-06-27)

Does there exist a w-bit word-RAM data structure with O(1) time per operation for the following problem?: Maintain a set of w-bit non-negative integers that supports the operations

* add(x) : add x to the set
* remove(x) : remove x from the set
* fingerprint() : return a fingerprint of the set. This w-bit fingerprint has the property that two sets that are identical have the same fingerprint while two sets that are different probably have different fingerprints

All operations should run in constant time.

The Rabin-Karp fingerprinting scheme, where $$f(S) = \left(\sum\_{x\in S} 2^x\right) \bmod p$$ where p is a random w-bit prime almost works. The problem is with the update time, since computing $$2^x \bmod p$$ takes more than constant time. Using repeated squaring, this can be done in O(log w) time. The fastest modular exponentiation algorithm I could find does something like (log w)/(loglog w) arithmetic operations.

**Tags:** ds.data-structures

> **Comments:**
> **username_0**: I see that a similar question was [posted here already](http://cstheory.stackexchange.com/questions/3390/is-there-a-hash-function-for-a-collection-i-e-multi-set-of-integers-that-has/), but no constant-time solution was given.  

---

## 1 answers

### Answer by username_1 on 2014-12-24. Score: 1

This answer is a bit hand-wavey.

Select a function $h$ uniformly at random from the set of all functions from w-bit words to w-bit words. For each set, maintain a w-bit fingerprint as follows:

1. The empty set has the fingerprint 0.
2. add(x) and remove(x) update a fingerprint $f$ to $f \oplus h(x)$, where $\oplus$ is xor.

Let $S \neq T$ be two sets of w-bit integers. If their fingerprints are the same, then the fingerprint of $S \triangle T$, the symmetric difference of $S$ and $T$, is 0, which happens with probability $2^{-w}$.

> **Comments:**
> **username_2**: And in practice you could instantiate $h$ with a pseudorandom function (PRF) from cryptography -- e.g., something based on AES.  Should be very efficient, and you get strong promises that it will work (unless the crypto is broken).  

---

