Skip to content

How FP-Growth works

This page explains the ideas behind the implementation. For the API, see the reference.

The problem

Given a database of transactions (each a set of items) and a minimum support threshold, frequent itemset mining finds every itemset that appears in at least that many transactions. The classic Apriori algorithm does this by generating candidate itemsets level by level and testing each one, which is expensive when many candidates survive.

FP-Growth takes a different route: it never generates candidates. Instead it compresses the database into a tree and mines the tree directly.

The FP-tree

The FP-tree is a prefix tree over the transactions:

  • The root is empty.
  • Each node is labelled with an item and stores a count: how many transactions passed through it.
  • Transactions that share a prefix share nodes, so the tree is much smaller than the database when transactions overlap.
  • Every node with the same label is threaded onto a linked list (the node links), so the tree can find all occurrences of an item quickly.

Before insertion, items are sorted in a global order — here, descending frequency. Ordering all transactions the same way is what allows prefixes to merge. It is not needed for correctness in a trivial sense, but without it the tree no longer represents shared prefixes and the mining step produces wrong supports. This was the original implementation's bug; the regression test in tests/test_core.py guards against it.

Conditional pattern bases and projected trees

For a chosen item x, look at every prefix path that ends at a node labelled x. Each such path, together with the count of its x node, is the conditional pattern base of x: the transactions that contain x, projected onto the items preceding x. Building an FP-tree from that base gives the conditional FP-tree of x.

Recursion

Mining proceeds recursively:

  1. Build the FP-tree of the (conditional) database.
  2. For each frequent item x, emit the itemset suffix ∪ {x} with support equal to the sum of x's node counts.
  3. Build the conditional FP-tree of x, adding x to the suffix, and recurse.

Because each conditional tree contains only items that precede x in the global order, every frequent itemset is generated exactly once.

Why only occurring itemsets are reported

The algorithm's counts come from the data, so an itemset that never co-occurs has no nodes in any conditional tree and is never reported. In particular, a relative threshold of 0 does not return the mathematically complete set of all subsets; it behaves like an absolute threshold of 1.

Cost

  • Time is roughly linear in the number of transactions times the number of frequent items per transaction, but the constant depends on how dense the conditional trees are.
  • Space is bounded by the compressed tree, which is smaller than the database when transactions share prefixes.
  • Recursion depth equals the length of the longest frequent itemset, which interacts with Python's recursion limit for unusually long patterns.

References