Click here to Skip to main content
15,351,871 members
Articles / Programming Languages / C#
Posted 2 Apr 2010

Tagged as


149 bookmarked

Apriori Algorithm

Rate me:
Please Sign up or sign in to vote.
4.91/5 (84 votes)
10 Aug 2012CPOL2 min read
Implementation of the Apriori algorithm in C#.

Image 1


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
Egypt Egypt
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.

Comments and Discussions

QuestionHi Pin
Shubhangi Sohoni30-Jun-21 1:59
MemberShubhangi Sohoni30-Jun-21 1:59 
QuestionCode of the program Pin
Member 1434841429-Apr-19 20:40
MemberMember 1434841429-Apr-19 20:40 
Questionapriori code Pin
Member 1419967827-Mar-19 4:04
MemberMember 1419967827-Mar-19 4:04 
QuestionPermission to use in a book Pin
Member 135974389-Apr-18 1:03
MemberMember 135974389-Apr-18 1:03 
Questionappriori Pin
Member 135897194-Apr-18 19:06
MemberMember 135897194-Apr-18 19:06 
QuestionApriori Algorithm Pin
ibrahimreyad10-Nov-17 12:16
Memberibrahimreyad10-Nov-17 12:16 
QuestionApriori java code Pin
Member 1312130611-Apr-17 3:58
MemberMember 1312130611-Apr-17 3:58 
Questionapriori with java please Pin
Member 1311902110-Apr-17 9:35
MemberMember 1311902110-Apr-17 9:35 
QuestionHelp me with apriori algorithm Pin
Member 1292966731-Dec-16 18:22
MemberMember 1292966731-Dec-16 18:22 
Questionjava code please Pin
Member 1290059012-Dec-16 8:24
MemberMember 1290059012-Dec-16 8:24 
Member 1273717015-Oct-16 2:39
MemberMember 1273717015-Oct-16 2:39 
Member 1273717015-Oct-16 2:37
MemberMember 1273717015-Oct-16 2:37 
QuestionHelp me! Pin
Fan KARIK23-May-16 0:01
MemberFan KARIK23-May-16 0:01 
QuestionRegarding apriori algorithm in java Pin
Member 1236742222-Mar-16 7:26
MemberMember 1236742222-Mar-16 7:26 
Questionrequest for java code for apriori implementation Pin
Member 123791628-Mar-16 18:29
MemberMember 123791628-Mar-16 18:29 
QuestionCustom built Apriori algorithm Pin
shuga24-Nov-15 8:29
Membershuga24-Nov-15 8:29 
AnswerRe: Custom built Apriori algorithm Pin
Omar Gameel Salem24-Nov-15 13:03
professionalOmar Gameel Salem24-Nov-15 13:03 
GeneralRe: Custom built Apriori algorithm Pin
shuga24-Nov-15 22:07
Membershuga24-Nov-15 22:07 
Questioncan you please send me the apriori algorithm implemented in c++ Pin
Member 1213767613-Nov-15 7:22
MemberMember 1213767613-Nov-15 7:22 
Questioncan i get c# full code Pin
Prabasini3-Nov-15 10:03
MemberPrabasini3-Nov-15 10:03 
QuestionApriori in C# Pin
Member 1199820920-Sep-15 10:42
MemberMember 1199820920-Sep-15 10:42 
QuestionApriori Algorithm Pin
Member 1199820920-Sep-15 10:34
MemberMember 1199820920-Sep-15 10:34 
QuestionEfficient Data Mining Software Pin
Member 1051770411-Jul-15 2:21
MemberMember 1051770411-Jul-15 2:21 
Questionrequest for your Aprori java code Pin
Member 1175977411-Jun-15 11:00
MemberMember 1175977411-Jun-15 11:00 
Questionrequest for apriori code in php Pin
mau_lida6-Jun-15 0:23
Membermau_lida6-Jun-15 0:23 

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

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