Skip to main content

FP-Growth Algorithm

Source: Unit 4 §9

Apriori pays for its candidates: it generates them, then scans the database again to test them. FP-Growth never generates a candidate at all.

Why FP-Growth?

AprioriFP-Growth
ApproachGenerate-and-test with candidatesNo candidate generation
Costcandidate generation is expensive; multiple DB scans (I/O); subset checkingcompresses the DB into a tree; only 2 passes
Data structurehash treeFP-tree (compressed)

FP-Growth uses a compressed representation (the FP-tree) and a recursive divide-and-conquer approach.

StepsThe two halves of the algorithm
  1. Build the FP-tree - 2 passes over the data.
  2. Extract frequent itemsets directly from the FP-tree by traversal.

Step 1: building the FP-tree

StepsTwo passes, and that is all the I/O there is
  1. Pass 1: count item frequencies, then order the items by descending frequency. That ordering is the header table.
  2. Pass 2: for each transaction, sort its items into the header order, insert it down the tree incrementing the counts of any shared prefix nodes, and maintain node-link pointers between nodes of the same item (the dotted lists).
Exam cueWhy the descending order matters

The more the paths overlap, the higher the compression, and putting the most frequent items nearest the root is what maximises overlap. A well compressed FP-tree may fit in memory, which is what removes the repeated database scans. Node-link (chain) pointers then let you find all the paths containing a given item quickly.

Worked example

10 transactions. Pass 1 gives the header table, in descending frequency:

ItemCount
B8
A7
C7
D5
E3

Each transaction is re-sorted into the order B, A, C, D, E before it is inserted. The ten transactions after reordering:

{B,A} {B,C,D} {A,C,D,E} {A,D,E} {B,A,C}
{B,A,C,D} {B,C} {B,A,C} {B,A,D} {B,C,E}

Inserting the first two of them shows the mechanic:

Insert TID 1 = {B, A}Insert TID 2 = {B, C, D}nullB:1A:1nullB:2A:1C:1… then D:1count 1 → 2
Transaction 2 shares the prefix B, so B is incremented rather than duplicated. That sharing is the whole compression story.

Carrying on for all ten gives the full FP-tree, with its node-link pointers:

Header tableitemcountB8A7C7D5E3nullB:8A:2A:5C:3C:3D:1D:1E:1C:1D:1D:1E:1D:1E:1dotted = node links betweencopies of the same itemorder B, A, C, D, E is fixedby descending pass-1 counts
The finished tree after all 10 transactions. Every root-to-node path is a prefix, and the dotted links chain the copies of one item together.
GotchaThis full tree is reconstructed, not copied

The slides draw only the first two insertions and then say "continue for all 10 transactions". The tree above was built from the notes' own reordered transaction list, and it checks out against their numbers twice over: the per-item node counts sum to B:8, A:7, C:7, D:5, E:3, exactly the header table, and the conditional pattern base for suffix E recounts to A:2, C:2, D:2 with B at 1 and therefore dropped, exactly as the notes state.

Step 2: mining via conditional FP-trees

Mine bottom-up from the least frequent item, using it as the suffix. For each suffix, build a conditional FP-tree: the tree of itemsets ending in that suffix.

StepsBuilding a conditional FP-tree for one suffix
  1. Find all paths containing the focus item - this is the conditional pattern base.
  2. Re-count item frequencies along those paths and build a new header. The item order can change, and items below min support are dropped.
  3. Re-insert the paths in the new order, truncating the suffix.
  4. Base of the recursion: when the tree is a single path, output all of its subsets plus the suffix.
StepsSuffix E, worked
  1. Take the paths ending in E and recount them: A:2, C:2, D:2, while B has support 1, which is below minsup 2, so B is dropped.
  2. Recurse on suffix DE - a base case - giving FI: DE, ADE.
  3. Recurse on suffix CE, giving FI: CE.
  4. Recurse on suffix AE, giving FI: AE.
GotchaThe item order is not fixed once and for all

Each conditional FP-tree gets its own header, recounted from its own pattern base. Items can change order, and items that were frequent globally can fall below minsup locally and be dropped - B does exactly this under suffix E.

Final frequent itemsets

FI: DE, ADE (from suffix E)
FI: CE
FI: AE
FI: CD, BCD, ACD (from suffix D)
FI: C, AC, BAC, BC (from suffix C)
FI: A, BA (from suffix A)
FI: B (from suffix B)

All of these were found without generating a single candidate.

GotchaThe suffix-D line in the slides is incomplete

Recompute it and you get more than three itemsets. D's conditional pattern base is the five prefixes {B,C}, {A,C}, {A}, {B,A,C}, {B,A}, in which A appears 4 times, B 3 times and C 3 times - all at or above the minimum support of 2. So AD (4), BD (3) and ABD (2) are frequent too, alongside the CD, BCD and ACD the slides list. The list above is reproduced as the notes have it; if a question asks you to derive the suffix-D itemsets yourself, derive all six.