The idea: squeeze the database into a tree, then mine the tree

A frequent itemset is a set of items that at least min support transactions hold. Apriori finds them level by level: it makes candidate sets and reads the whole database once per level to count them. FP-Growth reads the database only twice. The first scan counts the items. The second scan stores every transaction in a compact prefix tree, the FP-tree. All the mining after that works on the tree, and no candidate set is ever made.

Four boxes left to right. Database, 9 transactions. Scan 1 counts the items, drops Tea, and sorts the header table Bread 7, Eggs 6, Milk 6, Cola 2, Jam 2. Scan 2 inserts each sorted transaction into the FP-tree: 10 nodes for 23 items. Mining, which reads only the tree, takes each item from the bottom of the header table: its conditional pattern base, its conditional FP-tree, and the frequent itemsets that end in it. 13 itemsets come out.
Two passes over the data build the tree; everything after that reads only the tree.

The page uses 9 transactions over 6 items: Bread, Cola, Eggs, Jam, Milk and Tea. Min support is 2 transactions (3 in the menu). The top left panel is the database, the header table sits next to it, and the FP-tree is on the right. The bottom row shows the mining of one item: its pattern base, its conditional tree and the itemsets found so far.

Scan 1: count and sort the items

The first scan reads each transaction once and adds 1 to each of its items. Tea is in only 1 transaction, below min support 2. No itemset that holds Tea can be frequent, so Tea leaves every transaction. The other items are sorted by count, most frequent first, with ties in alphabetical order: Bread 7, Eggs 6, Milk 6, Cola 2, Jam 2. That list is the header table. Each transaction is then rewritten as its frequent items in header order: T1 = Milk, Bread, Cola becomes Bread, Milk, Cola. (Demo 1: scan 1, count and sort the items)

Scan 2: the FP-tree

The second scan inserts each sorted transaction as a path from the root. It works like a trie of words, with items in place of letters and a count on each node:

  • If the next item is already a child of the current node, the path is shared: that node gets +1.
  • If not, a new node with count 1 hangs there.
  • Every new node is joined to the previous node of the same item by a node-link. The first node of each item is linked from the header table. Following the links from the header visits every node of one item.

T8 = Bread, Eggs, Milk, Cola walks the path Bread → Eggs that T3 and T6 made and gives +1 to both. Then it hangs two new nodes, Milk:1 and Cola:1, each linked from the earlier node of its item. After 9 transactions the tree has 10 nodes for 23 items. A node's count is the number of transactions whose sorted form starts with the path from the root down to that node. (Demo 2: scan 2, build the FP-tree)

Why the most frequent item goes first

Paths share a node only when they start the same way. Frequent items are in many transactions, so putting them near the root lets many transactions share them. In reverse order the rare items come first and split the paths at once.

Two FP-trees of the same 9 transactions side by side. Left, most frequent first: under the root, Bread 7 with children Milk 2, Jam 1 and Eggs 4, and a second branch Eggs 2 with Milk 2. Ten nodes. Right, least frequent first: the root has four children Jam, Cola, Milk and Eggs, and Bread appears six times as a leaf. Fourteen nodes.
The same 23 items take 10 nodes with the most frequent item first and 14 with the least frequent first.
Header orderMin support 2Min support 3
most frequent first (the rule)10 nodes, 23 items6 nodes, 19 items
least frequent first14 nodes, 23 items7 nodes, 19 items

Both trees give the same itemsets; the order only changes the size of the tree and of the work. (Demo 6: least frequent first, 14 nodes instead of 10)

Mining one item: the conditional pattern base

Mining takes the items from the bottom of the header table up. For an item X:

  1. Follow X's node-links. The sum of their counts is the support of {X}.
  2. For each X node, walk up to the root. The items on the way form a prefix path, and it gets the count of the X node, not the counts of the nodes above it: only that many of those transactions also hold X.
  3. All prefix paths of X together are X's conditional pattern base: a small database of the transactions that hold X, with X taken out.
Mining Cola in four steps left to right. Node-links: two Cola nodes, Cola 1 under Bread, Milk and Cola 1 under Bread, Eggs, Milk; support 2. Pattern base: Bread, Milk with count 1 and Bread, Eggs, Milk with count 1. Counts: Bread 2, Milk 2, Eggs 1 which is below 2 and crossed out. Conditional FP-tree: one path, Bread 2 then Milk 2. Itemsets: Cola 2, Bread Cola 2, Milk Cola 2, Bread Milk Cola 2.
Cola's two prefix paths become a one-path tree, and every combination on that path, plus Cola, is frequent.

Cola has two nodes, each with count 1, so {Cola} has support 2. Its pattern base is {Bread, Milk}: 1 and {Bread, Eggs, Milk}: 1. (Demo 3: mine Cola, a one-path conditional tree)

The conditional FP-tree and recursion

Count each item in the pattern base with the path counts, drop those below min support, and build an FP-tree from the rest, in the same header order. That is X's conditional FP-tree. For Cola, Eggs has 1 and leaves; Bread 2 and Milk 2 make one path, Bread:2 → Milk:2.

  • If the conditional tree is one path, every combination of its items, joined with X, is frequent. Its support is the smallest count among the nodes picked. Cola gives {Bread, Cola} 2, {Milk, Cola} 2 and {Bread, Milk, Cola} 2.
  • If it has branches, mine it the same way: take its own header from the bottom, with X added to every suffix. Milk has 3 nodes and the pattern base {Bread}: 2, {Eggs}: 2, {Bread, Eggs}: 2. Its tree is Bread:4 → Eggs:2 plus a second branch Eggs:2. Mining Eggs inside it gives {Eggs, Milk} 4 and, from Eggs' own base {Bread}: 2, {Bread, Eggs, Milk} 2; then Bread gives {Bread, Milk} 4. (Demo 4: mine Milk, a conditional tree with two branches)

Each recursive call works on a smaller database and adds one item to the suffix. This is a divide and conquer split: the itemsets that end in Jam, those that end in Cola but not Jam, and so on, are found apart from each other.

All 13 itemsets, and what it cost

Item minedPattern baseConditional treeItemsets found
Jam{Bread, Milk}: 1, {Bread}: 1Bread:2{Jam} 2, {Bread, Jam} 2
Cola{Bread, Milk}: 1, {Bread, Eggs, Milk}: 1Bread:2 → Milk:2{Cola} 2, {Milk, Cola} 2, {Bread, Milk, Cola} 2, {Bread, Cola} 2
Milk{Bread}: 2, {Eggs}: 2, {Bread, Eggs}: 2Bread:4 → Eggs:2, and Eggs:2{Milk} 6, {Eggs, Milk} 4, {Bread, Eggs, Milk} 2, {Bread, Milk} 4
Eggs{Bread}: 4, { }Bread:4{Eggs} 6, {Bread, Eggs} 4
Bread{ } (it hangs from the root)none{Bread} 7

That is 13 frequent itemsets, the same 13 that Apriori finds. (Demo 5: mine every item, 13 itemsets from 2 scans)

On these 9 transactionsDatabase scansCandidate itemsets countedFrequent itemsets
Apriori, min support 231813
FP-Growth, min support 22013
Apriori, min support 33106
FP-Growth, min support 3206

With min support 3, Cola and Jam drop out in scan 1 and the tree has 6 nodes. Mining gives Bread 7, Eggs 6, Milk 6, {Bread, Eggs} 4, {Bread, Milk} 4 and {Eggs, Milk} 4. (Demo 7: min support 3, a 6-node tree)

FP-Growth always reads the database twice, however long the longest itemset is. Apriori reads it once per level, so a frequent set of 10 items costs it 10 or more scans and a huge number of candidates. The price of FP-Growth is memory: the tree must fit. On a few long, shared transactions it is far smaller than the data; on many short, unrelated ones it can be larger.

Related pages

Once the itemsets and their supports are known, Apriori and association rules shows how they become rules with confidence and lift; that step is the same for both algorithms. The FP-tree is a trie with counts, and Trie vs Hash Map shows why shared prefixes save space. For counting over a stream that never stops, where the data cannot be scanned twice, see DGIM sliding-window counting.

See also DGIM: Counting 1s in a Sliding Window, another mining-massive-datasets topic, about counting in data streams.

What the page leaves out

Real implementations keep the node-links as pointers in each node and do not draw them. When the tree does not fit in memory, the database is first split by item into projected databases that are mined one at a time; Spark's FP-Growth spreads them over machines (PFP). Many variants reuse the idea: closed and maximal itemsets (CLOSET, FPMax), and Eclat, which stores a list of transaction ids per item instead of a tree. Rule generation, ties between items in other orders, and the exact memory layout are not shown.

On the mind map

Open the Machine Learning mind mapMachine LearningLearning setup: what feedback, how to scoreLearning setupLinear models: weighted sum w·x + bLinear modelsOther classic models: trees, margins, neighboursOther classic modelsUnsupervised: find structure without labelsUnsupervisedTraining: loss, gradient, small stepTrainingNeural networks: layers of σ(W·a + b)Neural networksArchitectures: how layers share informationArchitecturesLanguage models: predict the next tokenLanguage modelsLLM speed and tuning: cheaper to run, safer to useLLM speed and tuningRetrieval and agents: give the model data and toolsRetrieval and agentsK-meansK-meansassign to nearest centroid, move centroidSelf-Organizing MapSelf-Organizing MapWinner and grid neighbours move toward xK-Medoids and SilhouetteK-Medoids and SilhouetteReal points as centres; pick k by avg sApriori RulesApriori RulesSubset not frequent → skip the supersetFP-GrowthTwo scans, a prefix tree, no candidatesDBSCANDBSCANgrow clusters from dense core pointsPCA vs t-SNEPCA vs t-SNEPCA keeps variance, t-SNE keeps neighboursk-means vs GMMk-means vs GMMhard circles vs soft covariance ellipsesEM algorithmEM algorithmE: soft labels, M: refit; ℓ never dropsHierarchical clusteringHierarchical clusteringMerge closest pair; cut the dendrogramMatrix factorization recsMatrix factorization recsRatings ≈ user vectors · item vectorsCollaborative FilteringCollaborative FilteringSimilar users' ratings fill the empty cellOpen the Machine Learning mind mapMachine Learningfind structure without labelsUnsupervisedK-meansK-meansassign to nearest centroid, move centroidSelf-Organizing MapSelf-Organizing MapWinner and grid neighbours move toward xK-Medoids and SilhouetteK-Medoids and SilhouetteReal points as centres; pick k by avg sApriori RulesApriori RulesSubset not frequent → skip the supersetFP-GrowthTwo scans, a prefix tree, no candidatesDBSCANDBSCANgrow clusters from dense core pointsPCA vs t-SNEPCA vs t-SNEPCA keeps variance, t-SNE keeps neighboursk-means vs GMMk-means vs GMMhard circles vs soft covariance ellipsesEM algorithmEM algorithmE: soft labels, M: refit; ℓ never dropsHierarchical clusteringHierarchical clusteringMerge closest pair; cut the dendrogramMatrix factorization recsMatrix factorization recsRatings ≈ user vectors · item vectorsCollaborative FilteringCollaborative FilteringSimilar users' ratings fill the empty cell
This page on the Machine Learning mind map · 88 pages in 10 branches · open it to see the whole map and test yourself in recall mode.