November 19, 2013 note: In the following algorithm, the combine algorithm is not implemented correctly and should be generated from the existing frequency. Further modifications are required
=================================================================================
Apriori algorithm principle:
if a set of items is frequent, then all subsets of it are also frequent. If a set of items is infrequent, then all of its superset is also not frequent.
Diagram
Figure I:
Figure II:
Package Cn.ffr.frequent.apriori;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.net.URL;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set; /** * Apriori Core Code Implementation * @author neu_fufengrui@163.com/public class Apriori {public static final String STRING_SPL
IT = ","; /** * Main calculation method * @param data DataSet * @param minsupport Minimum support * @param maxloop maximum number of executions, set NULL to obtain the final result * @param ContainS Et results must contain a subset * @return/public map<string, double> compute (list<string[]> data, Double Minsupport, Int
Eger Maxloop, string[] Containset {//checksum if (data = null | | | data.size () <= 0) {return null;
}//Initialize map<string, double> result = new hashmap<string, double> ();
object[] Itemset = getdataunitset (data);
int loop = 0; Core loop process while (true) {//Important step one: merging, generating new frequent sets set<string> keys = Combine (Result.keyset (), itemset); Result.clear ()///Remove previous result for (String Key:keys) {result.put (key, Computesupport (data, Key.split (String_split))
;
}//Important step Two: trim, remove support less than the condition.
Cut (result, minsupport, containset);
loop++;
The output calculation process System.out.println ("loop [" +loop+ "], Result:" +result);
Loop End Condition if (result.size () <= 0) {break;
} if (Maxloop!= null && maxloop > 0 && loop >= maxloop) {//can control loop execution times break;
} return result;
/** * Compute the support of a subset * * Support degree = subset in a DataSet data item/Total DataSet data Item * * Data item meaning is a piece of data. * @param data DataSet * @param subset Subset * @return/public Double computesupport (list<string[]> data, string[)
subset) {Integer value = 0;
for (int i = 0; i < data.size (); i++) {if (Contain (Data.get (i), subset)) {value + +;
} return Value*1.0/data.size ();
/** * Obtains initialized unique dataset for initialization * @param data * @return/public object[] Getdataunitset (list<string[]> data) { List<string> UniqUekeys = new arraylist<string> ();
For (string[] dat:data) {for (String Da:dat) {if (!uniquekeys.contains (DA)) {uniquekeys.add (DA);
}} return Uniquekeys.toarray (); /** * Merge src and target to obtain frequent sets * compute dimension of increasing frequent sets * @param src * @param target * @return/public set<string> C
Ombine (set<string> src, object[] target) {set<string> dest = new hashset<string> ();
if (src = null | | src.size () <= 0) {for (Object t:target) {Dest.add (t.tostring ());
return dest; for (String s:src) {for (Object t:target) {if (S.indexof (t.tostring ()) <0) {string key = S+string_split
+t;
if (!contain (dest, key)) {Dest.add (key);
}}} return dest; The/** * dest set contains key * @param dest * @param key * @return/public boolean contain (Set<string> dest,
String key) {for (string d:dest) {if (Equal (D.split (String_split), Key.split (String_split))} {return true;return false;
/** * Removal result, the support degree is less than the required support degree results. * @param result * @param minsupport * @return/public map<string, Double> cut (map<string, double> Res Ult, Double Minsupport, string[] containset) {for (Object Key:result.keySet (). ToArray ()) {//Prevent Java.util.ConcurrentModi Ficationexception, using Keyset (). ToArray () if (Minsupport!= null && minsupport > 0 && minsupport < 1
&& Result.get (Key) < Minsupport) {//Comparison support Result.remove (key); } if (Containset!= null && containset.length > 0 &&!contain key.tostring (). Split (String_split), con
Tainset)) {result.remove (key);
} return result; /** * src contains dest, you need to iterate through the query * @param src * @param dest * @return * * public static Boolean contain (string[) s
RC, string[] dest {for (int i = 0; i < dest.length; i++) {int j = 0;
for (; J < Src.length; J + +) {if (Src[j].equals (dest[i))) {break;
} if (j = = Src.length) { Return False;//can ' not find} ' return true; /** * src is equal to dest * @param src * @param dest * @return * * Public boolean equal (string[) src, string[] dest)
{if (src.length = = Dest.length && contain (SRC, dest)) {return true;
return false;
/** * Main test method * Test method, remove annotations, and test. */public static void main (string[] args) throws exception{//test 1//list<string[]> data = Loadsmalldata ();
/Long start = System.currenttimemillis (); map<string, double> result = new Apriori (). Compute (data, 0.5, 3, NULL);/seek support greater than specified value//Long end = System.current
Timemillis ();
System.out.println ("Apriori result [costs:" + (End-start) + "Ms]:"); For (String Key:result.keySet ()) {//System.out.println ("\tfrequent set=[" +key+ "] & support=[" +result.get (key) +
"];");
}//test 2//list<string[]> data = Loadmushroomdata ();
Long start = System.currenttimemillis (); map<string, double> result = new Apriori (). COMPUTE (Data, 0.3, 4, New string[]{"2"})//seek support greater than specified value//Long end = System.currenttimemillis ();
System.out.println ("Apriori result [costs:" + (End-start) + "Ms]:"); For (String Key:result.keySet ()) {//System.out.println ("\tfrequent set=[" +key+ "] & support=[" +result.get (key) +
"];");
}//test 3 List<string[]> data = Loadchessdata ();
Long start = System.currenttimemillis (); map<string, double> result = new Apriori (). Compute (data, 0.95, 3, NULL);/seek support greater than specified value Long end = System.currenttimem
Illis ();
System.out.println ("Apriori result [costs:" + (End-start) + "Ms]:"); For (String Key:result.keySet ()) {System.out.println ("\tfrequent set=[" +key+ "] & support=[" +result.get (key) + "];")
; }/* * Smalldata:minsupport 0.5, Maxloop 3, Containset null, [costs:16ms] * mushroomdata:minsupport 0.3, Maxloo
P 4, Containset {"2"}, [costs:103250ms] * chessdata:minsupport 0.95, maxloop, containset {null, [costs:9718ms] *//test DataSet-1 public staticList<string[]> Loadsmalldata () throws exception{list<string[]> data = new arraylist<string[]> ();
Data.add (New string[]{"D1", "D3", "D4"});
Data.add (New string[]{"D2", "D3", "D5"});
Data.add (New string[]{"D1", "D2", "D3", "D5"});
Data.add (New string[]{"D2", "D5"});
return data; }//Test DataSet-2 public static list<string[]> Loadmushroomdata () throws exception{String link = "http://fimi.ua.ac
. Be/data/mushroom.dat ";
URL url = new url (link);
BufferedReader reader = new BufferedReader (New InputStreamReader (Url.openstream ()));
String temp = Reader.readline ();
list<string[]> result = new arraylist<string[]> ();
int linenumber = 0; while (temp!= null) {SYSTEM.OUT.PRINTLN ("reading data ...) [No.]
+ (++linenumber) + "]");
string[] Item = Temp.split ("");
Result.add (item);
temp = Reader.readline ();
} reader.close ();
return result; }//test DataSet-3 public static list<string[]> Loadchessdata () throws exception{String link ="Http://fimi.ua.ac.be/data/chess.dat";
URL url = new url (link);
BufferedReader reader = new BufferedReader (New InputStreamReader (Url.openstream ()));
String temp = Reader.readline ();
list<string[]> result = new arraylist<string[]> ();
int linenumber = 0; while (temp!= null) {SYSTEM.OUT.PRINTLN ("reading data ...) [No.]
+ (++linenumber) + "]");
string[] Item = Temp.split ("");
Result.add (item);
temp = Reader.readline ();
} reader.close ();
return result;
}
}
Algorithm principle: