Click here to Skip to main content
Click here to Skip to main content

Apriori Algorithm

, 10 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Implementation of the Apriori algorithm in C#.


In data mining, Apriori is a classic algorithm for learning association rules. Apriori is designed to operate on databases containing transactions (for example, collections of items bought by customers, or details of a website frequentation).

Other algorithms are designed for finding association rules in data having no transactions (Winepi and Minepi), or having no timestamps (DNA sequencing).


The whole point of the algorithm (and data mining, in general) is to extract useful information from large amounts of data. For example, the information that a customer who purchases a keyboard also tends to buy a mouse at the same time is acquired from the association rule below:

Support: The percentage of task-relevant data transactions for which the pattern is true.

Support (Keyboard -> Mouse) = AprioriAlgorithm/eq_1.JPG

Confidence: The measure of certainty or trustworthiness associated with each discovered pattern.

Confidence (Keyboard -> Mouse) = AprioriAlgorithm/eq_2.JPG

The algorithm aims to find the rules which satisfy both a minimum support threshold and a minimum confidence threshold (Strong Rules).

  • Item: article in the basket.
  • Itemset: a group of items purchased together in a single transaction.

How Apriori Works

  1. Find all frequent itemsets:
    • Get frequent items:
      • Items whose occurrence in database is greater than or equal to the threshold.
    • Get frequent itemsets:
      • Generate candidates from frequent items.
      • Prune the results to find the frequent itemsets.
  2. Generate strong association rules from frequent itemsets
    • Rules which satisfy the and min.confidence threshold.

High Level Design


Low Level Design



A database has five transactions. Let the min sup = 50% and min con f = 80%.



Step 1: Find all Frequent Itemsets


Frequent Itemsets

{A}   {B}   {C}   {E}   {A C}   {B C}   {B E}   {C E}   {B C E}

Step 2: Generate strong association rules from the frequent itemsets



Closed Itemset: support of all parents are not equal to the support of the itemset.

Maximal Itemset: all parents of that itemset must be infrequent.

Keep in mind:



Itemset {c} is closed as support of parents (supersets) {A C}:2, {B C}:2, {C D}:1, {C E}:2 not equal support of {c}:3.

And the same for {A C}, {B E} & {B C E}.

Itemset {A C} is maximal as all parents (supersets) {A B C}, {A C D}, {A C E} are infrequent.

And the same for {B C E}.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Omar Gameel Salem
Software Developer
Australia Australia
Enthusiastic programmer/researcher, passionate to learn new technologies, interested in problem solving, data structures, algorithms, AI, machine learning and nlp.
Amateur guitarist/ keyboardist, squash player.
If you have a question\suggestion about one of my articles, or you want an algorithm implemented in C#, feel free to contact me.
Follow on   LinkedIn

Comments and Discussions

QuestionRequest for Aprori code in java PinmemberMember 114197271-Feb-15 22:02 
GeneralNeed code for Closed and maximal frequent item set PinmemberMember 1141126828-Jan-15 23:45 
Questionplease send me the apriori algorith code PinmemberMember 1125434324-Dec-14 20:59 
Questionplease send me the apriori algorith code PinmemberMember 111309425-Oct-14 9:34 
AnswerRe: please send me the apriori algorith code PinprofessionalMember 1030011418-Oct-14 1:00 
QuestionCan I have the C# code PinmemberMember 1087192112-Jun-14 4:53 
AnswerRe: Can I have the C# code PinmemberOmar Gameel Salem12-Jun-14 17:24 
QuestionHelp me PinmemberMember 107997886-May-14 15:04 
QuestionMMAC algorithm PinmemberXuan Dung Phu Yen16-Mar-14 20:15 
Questiondo you know? high-utility rule mining? Pinmemberhothi_vuong14-Mar-14 5:23 
QuestionApriory Algo Implementation PinmemberMember 106110551-Mar-14 1:56 
Questionlimit to your code Pinmemberyogeshwarmisal27-Feb-14 20:32 
AnswerRe: limit to your code PinmemberOmar Gameel Salem27-Feb-14 22:11 
Questionhow this algo will take my sql server table rows as transaction? PingroupMona198618-Jan-14 10:29 
QuestionApriori PinmemberMember 1022814823-Aug-13 7:42 
Questionhelp Pinmembermilad199130-Jun-13 23:14 
QuestionApriori variations PinmemberSaira1119-Mar-13 4:27 
QuestionApriori algorithm PinmemberMember 987030028-Feb-13 5:24 
AnswerRe: Apriori algorithm Pinmemberaspround18-Apr-13 6:49 
GeneralRe: Apriori algorithm PinmemberMember 1065266925-Feb-15 21:22 
Questionplease need help Pinmemberazizhai12-Jan-13 19:24 
please brother jamal, i want to modify in some apiori algo. functions or in another word how i can custmize apriori algo. according my need.

please help me. by the way i am PhD student and i will mentoin your name in my references.
QuestionYou should add rate. Pinmemberjingjingtr9-Dec-12 3:40 
QuestionNew Version is only changed the UI? Pinmemberjingjingtr5-Dec-12 22:47 
AnswerRe: New Version is only changed the UI? PinmemberOmar Gamil5-Dec-12 22:53 
GeneralRe: New Version is only changed the UI? Pinmemberjingjingtr6-Dec-12 0:29 
GeneralRe: New Version is only changed the UI? Pinmemberfriday250f19-Dec-12 14:16 
QuestionError Pinmemberjingjingtr2-Dec-12 21:33 
AnswerRe: Error PinmemberOmar Gamil2-Dec-12 22:00 
GeneralRe: Error Pinmemberjingjingtr3-Dec-12 15:51 
QuestionHow can i add many items? Pinmemberjingjingtr2-Dec-12 21:27 
AnswerRe: How can i add many items? PinmemberOmar Gamil2-Dec-12 22:33 
QuestioncSPADE algorithm PinmemberMarwa M. Ghareeb26-Nov-12 7:42 
AnswerRe: cSPADE algorithm PinmemberOmar Gamil26-Nov-12 7:54 
GeneralRe: cSPADE algorithm PinmemberMarwa M. Ghareeb26-Nov-12 8:43 
GeneralRe: cSPADE algorithm PinmemberMarwa M. Ghareeb20-Dec-12 6:15 
GeneralRe: cSPADE algorithm PinmemberOmar Gamil21-Dec-12 10:43 
Generalremerciment PinmemberLAHCINE ZOZO23-Nov-12 2:12 
QuestionHow to download files? Pinmemberjingjingtr6-Nov-12 22:20 
AnswerRe: How to download files? PinmemberOmar Gamil7-Nov-12 1:38 
GeneralRe: How to download files? Pinmemberjingjingtr7-Nov-12 16:41 
AnswerRe: How to download files? PinmemberOmar Gamil8-Nov-12 0:14 
QuestionError in compilation PinmemberTEITE22-Oct-12 5:15 
AnswerRe: Error in compilation PinmemberOmar Gamil22-Oct-12 6:16 
NewsRe: Error in compilation PinmemberOmar Gamil22-Oct-12 7:33 
GeneralRe: Error in compilation PinmemberTEITE22-Oct-12 23:18 
QuestionQuestion about the empty set as premise PinmemberMr. Fox21-Oct-12 3:04 
AnswerRe: Question about the empty set as premise PinmemberOmar Gamil21-Oct-12 3:15 
GeneralRe: Question about the empty set as premise PinmemberMr. Fox21-Oct-12 6:00 
Questionthanks PinmemberMember 937370921-Aug-12 23:55 
QuestionConfidence PinmemberMember 243269613-Aug-12 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 10 Aug 2012
Article Copyright 2010 by Omar Gameel Salem
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid