Algorithm Search⌘K

Definitionadvanced

km-anonymity

Also called transaction k-anonymity.

For set-valued data, requires that any m items an attacker knows of a transaction match at least k transactions.

kmk^m-anonymity is a privacy condition for set-valued data, such as shopping baskets, search logs or lists of diagnoses, where each record is a set of items of any size. An attacker often knows a few items of a person's record, for example some of the things the person bought, and wants to find the whole record. Terrovitis, Mamoulis and Kalnis (PVLDB 2008) defined a database as kmk^m-anonymous if no attacker who knows up to mm items of a transaction can narrow the candidates down to fewer than kk transactions. Every item can be part of the attacker's knowledge, so there is no fixed set of quasi-identifiers, and every item can also be the thing the attacker wants to learn. Both kk and mm are policy choices: mm models how much the attacker is assumed to know. The condition is a property of the published database, and it says nothing about attackers who know more than mm items.

Definition

A transaction is a non-empty subset of a domain I\mathcal I of items, and a database DD is a multiset of transactions. The support of an itemset II is the number of transactions t∈Dt \in D with I⊆tI \subseteq t.

Definition (Terrovitis, Mamoulis and Kalnis, Definition 1). A database DD is kmk^m-anonymous if no attacker with background knowledge of up to mm items of a transaction t∈Dt \in D can use these items to identify fewer than kk transactions of DD. Equivalently, every itemset II with ∣I∣≤m|I| \le m has support 0 or at least kk.

An itemset with support 0 is safe: knowing it matches no transaction. The knowledge is modelled as a subset query, the set of transactions that contain the known items.

To make a database kmk^m-anonymous, the paper uses a generalization hierarchy over I\mathcal I and global recoding: a cut of the hierarchy maps each item to one ancestor in all transactions, such as "skim milk" to "milk". Transactions keep set semantics, so two items that map to the same ancestor become one item (Definition 2 and Figure 2 of the paper). The cost of a cut is measured by the normalized certainty penalty (NCP), which charges each generalized item by the share of the domain it covers.

Facts, each a theorem:

  • Support is anti-monotone. If I⊆JI \subseteq J, the support of II is at least that of JJ. So kmk^m-anonymity implies km′k^{m'}-anonymity for every m′≤mm' \le m, and kk-anonymity with a larger kk implies it with a smaller one.
  • Whole-transaction anonymity is stronger. If every transaction occurs at least kk times in DD, then DD is kmk^m-anonymous for every mm, because an itemset I⊆tI \subseteq t is contained in all the copies of tt. The converse is false, as the example below shows.
  • Coarser cuts stay anonymous (Property 1 of the paper, monotonicity of cuts). If a cut gives a kmk^m-anonymous database, so does every more general cut. The reason: an itemset I′I' of size at most mm over the coarser items that some transaction contains comes from an itemset JJ of size at most ∣I′∣|I'| over the finer items in that transaction, picking one item below each element of I′I', and every transaction that contains JJ contains I′I' after the coarser recoding. So the support of I′I' is at least the support of JJ, which is 0 or at least kk, and it is not 0.

The anti-monotone support gives a direct test: count every itemset of size at most mm that occurs in some transaction, and check the counts. proofs/correctness.md proves that the test is exact.

  1. KM-ANONYMOUS(D, k, m)
  2. 1C ← an empty map from itemsets to counts
  3. 2for each transaction t in D do
  4. 3for each subset I of t with 1 ≤ |I| ≤ m do
  5. 4C[I] ← C[I] + 1
  6. 5for each itemset I in C do
  7. 6if C[I] < k then return false
  8. 7return true

If each transaction has at most τ\tau items, lines L2 to L4 do ∑j=1m(τj)\sum_{j=1}^{m} \binom{\tau}{j} map updates per transaction, which is O(∣D∣ τm)O(|D|\, \tau^m) in total for a fixed mm, with hashing in the word-RAM model. The paper stores the same counts in a count-tree, a prefix tree like the FP-tree, and builds it in O(∣D∣(τm))O(|D| \binom{\tau}{m}) steps (Section 4.1). The cost grows fast with mm, which is one reason mm is small in practice.

Finding the cut with the least information loss is hard in general. The paper gives an optimal algorithm (OA) that searches the lattice of cuts bottom-up in the manner of Incognito, and two heuristics: direct anonymization (DA), which fixes the itemsets of size mm that break the condition, and apriori anonymization (AA), which fixes itemsets of size 1,2,…,m1, 2, \ldots, m in turn, so that each level has fewer combinations to check.

Examples and non-examples

The database is the one of Figure 2 of the paper, with four synthetic transactions over I={a1,a2,b1,b2}\mathcal I = \{a_1, a_2, b_1, b_2\} and the hierarchy a1,a2→Aa_1, a_2 \to A, b1,b2→Bb_1, b_2 \to B, A,B→ALLA, B \to ALL. Take k=2k = 2.

id original DD cut {a1,a2}→A\{a_1, a_2\} \to A cut {b1,b2}→B\{b_1, b_2\} \to B
t1t_1 {a1,b1,b2}\{a_1, b_1, b_2\} {A,b1,b2}\{A, b_1, b_2\} {a1,B}\{a_1, B\}
t2t_2 {a2,b1}\{a_2, b_1\} {A,b1}\{A, b_1\} {a2,B}\{a_2, B\}
t3t_3 {a2,b1,b2}\{a_2, b_1, b_2\} {A,b1,b2}\{A, b_1, b_2\} {a2,B}\{a_2, B\}
t4t_4 {a1,a2,b2}\{a_1, a_2, b_2\} {A,b2}\{A, b_2\} {a1,a2,B}\{a_1, a_2, B\}
  • DD is 212^1-anonymous but not 222^2-anonymous. Each single item has support at least 2: a1a_1 has 2, and a2a_2, b1b_1 and b2b_2 have 3. But the pair {a1,b1}\{a_1, b_1\} has support 1: an attacker who knows that a person bought a1a_1 and b1b_1 finds t1t_1 and learns that the person also bought b2b_2.
  • The cut {a1,a2}→A\{a_1, a_2\} \to A gives a 222^2-anonymous database. The supports are {A}\{A\}: 4, {b1}\{b_1\}: 3, {b2}\{b_2\}: 3, {A,b1}\{A, b_1\}: 3, {A,b2}\{A, b_2\}: 3, {b1,b2}\{b_1, b_2\}: 2. It is also 232^3-anonymous, since {A,b1,b2}\{A, b_1, b_2\} has support 2.
  • 222^2-anonymous but not 2-anonymous as whole transactions. In the same recoded database, {A,b1}\{A, b_1\} and {A,b2}\{A, b_2\} each occur once as a whole transaction. An attacker who knew every item of t2t_2 and knew that the basket had no other items would find it. kmk^m-anonymity does not protect against that much knowledge, by design.
  • Non-example: the cut {b1,b2}→B\{b_1, b_2\} \to B. The supports of {a1}\{a_1\}, {a2}\{a_2\} and {B}\{B\} are 2, 3 and 4, and {a1,B}\{a_1, B\} has 2 and {a2,B}\{a_2, B\} has 3, but {a1,a2}\{a_1, a_2\} has support 1, in t4t_4. Generalizing the wrong items does not help.
  • Coarser is safe. The cut that maps all four items to ALLALL gives four copies of {ALL}\{ALL\}, which is 2m2^m-anonymous for every mm and useless, as Property 1 predicts.
  • k-anonymity: the condition for tables with a fixed set of quasi-identifier attributes. kmk^m-anonymity bounds the attacker's knowledge by a number of items instead of a set of attributes.
  • Quasi-identifier: in set-valued data any combination of up to mm items can play this role.
  • Sparse dataset de-anonymization: Narayanan and Shmatikov showed that a few known items re-identify records in large sparse datasets, which is the threat that kmk^m-anonymity bounds when mm is at least the number of items the attacker knows.
  • l-diversity: protects sensitive values once a record is narrowed down to a group. Terrovitis et al. treat every item as both a possible quasi-identifier and possibly sensitive, so kmk^m-anonymity alone does not stop an attacker from learning an item that all matching transactions share.
  • Generalization and suppression: the operations used to reach kmk^m-anonymity; here generalization follows an item hierarchy and is global.
  • Differential privacy: a guarantee that does not depend on a bound mm on the attacker's knowledge.
Proof: The counting test decides km-anonymity exactlyfull

Let DD be a finite multiset of transactions, each a finite set of items, and let k≥1k \ge 1 and m≥0m \ge 0. The support s(I)s(I) of an itemset II is the number of transactions t∈Dt \in D, counted with multiplicity, with I⊆tI \subseteq t. DD is kmk^m-anonymous if every non-empty itemset II with ∣I∣≤m|I| \le m has s(I)=0s(I) = 0 or s(I)≥ks(I) \ge k.

Claim 1 (the counts). After lines L1 to L4, the map CC holds exactly the itemsets II with 1≤∣I∣≤m1 \le |I| \le m and s(I)≥1s(I) \ge 1, and C[I]=s(I)C[I] = s(I) for each of them.

Proof. Line L1 starts with an empty map. Line L3 lists each subset of tt of size 1 to mm exactly once, because the items of tt are distinct, and line L4 adds 1 to its count. So after all transactions, C[I]C[I] is the number of transactions tt for which II is one of the listed subsets, that is, with I⊆tI \subseteq t and 1≤∣I∣≤m1 \le |I| \le m. This number is s(I)s(I). An itemset is a key of CC exactly when it was listed at least once, that is, when s(I)≥1s(I) \ge 1. □\square

Claim 2 (the test). KM-ANONYMOUS returns true if and only if DD is kmk^m-anonymous.

Proof. If DD is not kmk^m-anonymous, some II with 1≤∣I∣≤m1 \le |I| \le m has 1≤s(I)<k1 \le s(I) < k. By Claim 1, II is a key of CC with C[I]=s(I)<kC[I] = s(I) < k, so lines L5 and L6 return false when they reach it, or earlier for another such itemset. If DD is kmk^m-anonymous, every key II of CC has C[I]=s(I)≥1C[I] = s(I) \ge 1, so s(I)≥ks(I) \ge k, line L6 never returns, and line L7 returns true. ■\blacksquare

Anti-monotone support. If I⊆JI \subseteq J, every transaction that contains JJ contains II, so s(I)≥s(J)s(I) \ge s(J). Hence kmk^m-anonymity implies km′k^{m'}-anonymity for m′≤mm' \le m: the itemsets of size at most m′m' are among those of size at most mm.

Coarser cuts. Let gg map items to items, applied with set semantics: g(t)={g(x):x∈t}g(t) = \{g(x) : x \in t\}. Suppose DD is kmk^m-anonymous and I′I' is an itemset with 1≤∣I′∣≤m1 \le |I'| \le m and sg(D)(I′)≥1s_{g(D)}(I') \ge 1. Take a transaction tt with I′⊆g(t)I' \subseteq g(t), and for each y∈I′y \in I' pick one xy∈tx_y \in t with g(xy)=yg(x_y) = y. The set J={xy}J = \{x_y\} has 1≤∣J∣≤∣I′∣≤m1 \le |J| \le |I'| \le m and sD(J)≥1s_D(J) \ge 1, so sD(J)≥ks_D(J) \ge k. Every transaction u⊇Ju \supseteq J has g(u)⊇I′g(u) \supseteq I', so sg(D)(I′)≥sD(J)≥ks_{g(D)}(I') \ge s_D(J) \ge k. So g(D)g(D) is kmk^m-anonymous.

Cost. A transaction with τ\tau items has ∑j=1m(τj)\sum_{j=1}^{m} \binom{\tau}{j} subsets of size 1 to mm, each built and hashed in O(m)O(m) time. Lines L5 to L7 read each key once. For a fixed mm the total is O(∣D∣ τm)O(|D|\, \tau^m) map operations.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

km_anonymity.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/km-anonymity/. The authors are listed in prov/YFDA8G.jsonl.

from collections.abc import Hashable, Iterable, Mapping, Sequence
from itertools import combinations


def is_km_anonymous(transactions: Sequence[Iterable[Hashable]], k: int, m: int) -> bool:
    """Return True if every itemset of at most m items has support 0 or at least k.

    Each transaction is read as a set of items. Precondition: k >= 1, m >= 0.
    Cost: O(sum over transactions t of C(|t|, 1) + ... + C(|t|, m)) hash map
    updates, which is O(|D| tau^m) for transactions of at most tau items.
    """
    counts: dict[frozenset[Hashable], int] = {}  # @line L1
    for t in transactions:  # @line L2
        items = list(set(t))
        for size in range(1, m + 1):
            for subset in combinations(items, size):  # @line L3
                key = frozenset(subset)
                counts[key] = counts.get(key, 0) + 1  # @line L4
    for c in counts.values():  # @line L5
        if c < k:  # @line L6
            return False
    return True  # @line L7


def generalize(transactions: Sequence[Iterable[Hashable]], cut: Mapping[Hashable, Hashable]) -> list[frozenset[Hashable]]:
    """Apply a global recoding: replace each item by cut[item] (or keep it if absent), with set semantics.

    Cost: O(total number of items).
    """
    return [frozenset(cut.get(x, x) for x in t) for t in transactions]
test_km_anonymity.py 84 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/km-anonymity/. The authors are listed in prov/YFDA8G.jsonl.

import random
from collections import Counter
from itertools import combinations

from km_anonymity import generalize, is_km_anonymous

D = [{"a1", "b1", "b2"}, {"a2", "b1"}, {"a2", "b1", "b2"}, {"a1", "a2", "b2"}]
TO_A = {"a1": "A", "a2": "A"}
TO_B = {"b1": "B", "b2": "B"}


def test_example() -> None:
    assert is_km_anonymous(D, 2, 1)
    assert not is_km_anonymous(D, 2, 2)
    da = generalize(D, TO_A)
    assert da == [frozenset("A b1 b2".split()), frozenset({"A", "b1"}), frozenset("A b1 b2".split()), frozenset({"A", "b2"})]
    assert is_km_anonymous(da, 2, 2)
    assert is_km_anonymous(da, 2, 3)
    # not 2-anonymous as whole transactions
    assert min(Counter(da).values()) == 1
    assert not is_km_anonymous(generalize(D, TO_B), 2, 2)
    everything = {x: "ALL" for x in ["a1", "a2", "b1", "b2"]}
    assert generalize(D, everything) == [frozenset({"ALL"})] * 4
    assert is_km_anonymous(generalize(D, everything), 2, 4)


def test_edges() -> None:
    assert is_km_anonymous([], 5, 3)
    assert is_km_anonymous([{"x"}], 1, 2)
    assert not is_km_anonymous([{"x"}], 2, 1)
    assert is_km_anonymous([{"x"}], 2, 0)
    assert is_km_anonymous([["x", "x"], ["x"]], 2, 2)


def oracle(db: list[set[str]], k: int, m: int) -> bool:
    universe = sorted(set().union(*db)) if db else []
    for size in range(1, m + 1):
        for subset in combinations(universe, size):
            s = set(subset)
            support = sum(1 for t in db if s <= t)
            if 0 < support < k:
                return False
    return True


def random_db(rng: random.Random) -> list[set[str]]:
    items = "abcdef"[: rng.randint(1, 6)]
    return [set(rng.sample(items, rng.randint(1, len(items)))) for _ in range(rng.randint(0, 10))]


def test_against_oracle() -> None:
    rng = random.Random(31)
    for _ in range(600):
        db = random_db(rng)
        k = rng.randint(1, 4)
        m = rng.randint(1, 4)
        assert is_km_anonymous(db, k, m) == oracle(db, k, m)


def test_theorems() -> None:
    rng = random.Random(32)
    parent = {"a": "X", "b": "X", "c": "Y", "d": "Y", "e": "Z", "f": "Z"}
    top = {"X": "ALL", "Y": "ALL", "Z": "ALL"}
    for _ in range(500):
        db = random_db(rng)
        k = rng.randint(1, 4)
        m = rng.randint(1, 4)
        ok = is_km_anonymous(db, k, m)
        # anti-monotone in m and in k
        if ok:
            assert is_km_anonymous(db, k, m - 1)
            assert is_km_anonymous(db, k - 1 or 1, m)
        # coarser cuts stay anonymous
        coarse = generalize(db, parent)
        if ok:
            assert is_km_anonymous(coarse, k, m)
        if is_km_anonymous(coarse, k, m):
            assert is_km_anonymous(generalize(coarse, top), k, m)
        # whole-transaction k-anonymity implies km-anonymity for every m
        copies = [t for t in db for _ in range(k)]
        assert is_km_anonymous(copies, k, 6)

References

  1. Manolis Terrovitis, Nikos Mamoulis and Panos Kalnis, Privacy-preserving anonymization of set-valued data, PVLDB 1(1), 2008, pages 115-125, Section 1 (the shopping-bag example), Section 3 (Definition 1, the subset queries), Section 3.1 (the generalization hierarchy of Figure 1, Definition 2 and the example of Figure 2), Section 3.2 (cuts and global recoding), Section 3.3 (NCP), Section 3.4 (Property 1, monotonicity of cuts) and Section 4 (the count-tree and the OA, DA and AA algorithms)
  2. Arvind Narayanan and Vitaly Shmatikov, Robust de-anonymization of large sparse datasets, IEEE Symposium on Security and Privacy 2008, pages 111-125, the re-identification of records in sparse data from a few known items, which motivates protection against partial knowledge

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH2 full proof
  • correctness: no review record reviews/YFDA8G/proof-correctness.yaml
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/YFDA8G/text.yaml