1+ import java .util .*;
2+ import java .io .*;
3+ public class apriori {
4+ static class FastReader
5+ {
6+ BufferedReader br ;
7+ StringTokenizer st ;
8+
9+ public FastReader ()
10+ {
11+ br = new BufferedReader (new
12+ InputStreamReader (System .in ));
13+ }
14+
15+ String next ()
16+ {
17+ while (st == null || !st .hasMoreElements ())
18+ {
19+ try
20+ {
21+ st = new StringTokenizer (br .readLine ());
22+ }
23+ catch (IOException e )
24+ {
25+ e .printStackTrace ();
26+ }
27+ }
28+ return st .nextToken ();
29+ }
30+
31+ int nextInt ()
32+ {
33+ return Integer .parseInt (next ());
34+ }
35+
36+ long nextLong ()
37+ {
38+ return Long .parseLong (next ());
39+ }
40+
41+ double nextDouble ()
42+ {
43+ return Double .parseDouble (next ());
44+ }
45+
46+ String nextLine ()
47+ {
48+ String str = "" ;
49+ try
50+ {
51+ str = br .readLine ();
52+ }
53+ catch (IOException e )
54+ {
55+ e .printStackTrace ();
56+ }
57+ return str ;
58+ }
59+ }
60+
61+ static HashMap <TreeSet <String >,Integer > filtersupport (HashMap <TreeSet <String >,Integer > map ,int support )
62+ {
63+ HashMap <TreeSet <String >,Integer > calsupport =new HashMap <>();
64+ for ( Map .Entry <TreeSet <String >,Integer > entry : map .entrySet () )
65+ {
66+ if (entry .getValue ()>=support )
67+ {
68+ calsupport .put (entry .getKey (),entry .getValue ());
69+ }
70+ //System.out.println( entry.getKey() + " => " + entry.getValue() );
71+ //HashSet<String> set=new HashSet<>();
72+ //set.add(entry.getKey());
73+ //calsupport.put(set,entry.getValue());
74+ }
75+ return calsupport ;
76+ }
77+ static int frequency (HashMap <Integer ,TreeSet <String >> trans ,TreeSet <String > items )
78+ {
79+ int count =0 ;boolean cont =true ;
80+ for ( Map .Entry <Integer , TreeSet <String >> entry : trans .entrySet () )
81+ {
82+ TreeSet <String > itemset =entry .getValue ();
83+
84+ cont =true ;
85+ for ( String item : items )
86+ {
87+ if (itemset .contains (item )==false )
88+ {
89+ cont =false ;
90+ break ;
91+ }
92+ /*if(itemfreq.containsKey(item)==false)
93+ {
94+ itemfreq.put(item,1);
95+ }
96+ else
97+ {
98+ itemfreq.put(item,itemfreq.get(item)+1);
99+ }*/
100+
101+ }
102+ if (cont ==true )
103+ count ++;
104+
105+ //System.out.println( entry.getKey() + " => " + entry.getValue() );
106+ }
107+ return count ;
108+ }
109+
110+ static int factorial (int n )
111+ {
112+ if (n == 0 )
113+ return 1 ;
114+
115+ return n * factorial (n - 1 );
116+ }
117+
118+
119+ public static void main (String args [])
120+ {
121+ FastReader s =new FastReader ();int trans =0 ,items =0 ,support =0 ,freq =0 ,p =0 ,num =1 ,confidence =0 ,supportof =0 ,confi =0 ;String str ="" ,str11 ="" ,str12 ="" ,item1 ="" ;
122+ //Scanner in=new Scanner(System.in);
123+ //if(in.hasNext())
124+ HashMap <Integer ,TreeSet <String >> map =new HashMap <>();
125+ TreeSet <String > totitems =new TreeSet <>();
126+ trans =s .nextInt ();
127+
128+
129+ //input taken-----------------
130+ for (int j =0 ;j <trans ;j ++)
131+ {
132+ // if(in.hasNext())
133+ items =s .nextInt ();
134+ TreeSet <String > set =new TreeSet <>();
135+ for (int i =0 ;i <items ;i ++)
136+ {
137+ str =s .next ();
138+ totitems .add (str );
139+ set .add (str );
140+ }
141+ map .put (j +1 ,set );
142+ }
143+ support =s .nextInt ();
144+ confidence =s .nextInt ();
145+
146+ HashMap <TreeSet <String >,Integer > itemfreq =new HashMap <>();
147+ HashMap <TreeSet <String >,Integer > finalcomb =new HashMap <>();
148+ for (String str1 :totitems )
149+ {
150+ TreeSet <String > temp =new TreeSet <>();
151+ temp .add (str1 );
152+ p =frequency (map ,temp );
153+ itemfreq .put (temp ,p );
154+ }
155+
156+ int common =-1 ;
157+ int counter =1 ;
158+
159+ //for(int i1=1;i1<=4;i1++)
160+ while (itemfreq .size ()!=0 )
161+ {
162+
163+ System .out .println ("Iteration " +(num ));
164+
165+
166+ itemfreq =filtersupport (itemfreq ,support );
167+
168+
169+
170+
171+ /* for( Map.Entry<Integer, HashSet<String>> entry : map.entrySet() )
172+ {
173+ HashSet<String> itemset=entry.getValue();
174+ for( String item : itemset )
175+ {
176+ if(itemfreq.containsKey(item)==false)
177+ {
178+ itemfreq.put(item,1);
179+ }
180+ else
181+ {
182+ itemfreq.put(item,itemfreq.get(item)+1);
183+ }
184+ }
185+ //System.out.println( entry.getKey() + " => " + entry.getValue() );
186+ }*/
187+
188+ /* HashSet<String> itemremove=new HashSet<>();
189+ for( Map.Entry<String,Integer> entry : itemfreq.entrySet() )
190+ {
191+ item1=entry.getKey();
192+ freq=entry.getValue();
193+ // System.out.println(item1+" "+freq);
194+ if(freq<support)
195+ {
196+ itemremove.add(item1);
197+ }
198+ }*/
199+ //System.out.println(itemremove);
200+ /*for( String item : itemremove )
201+ {
202+ String k=(String)itemfreq.remove(item);
203+ }*/
204+
205+ //convert
206+ /* HashMap<HashSet<String>,Integer> calsupport =new HashMap<>();
207+ for( Map.Entry<String, Integer> entry : itemfreq.entrySet() )
208+ {
209+ System.out.println( entry.getKey() + " => " + entry.getValue() );
210+ HashSet<String> set=new HashSet<>();
211+ set.add(entry.getKey());
212+ calsupport.put(set,entry.getValue());
213+ }
214+ calsupport=filtersupport(calsupport,support);*/
215+ List <TreeSet <String >> list = new ArrayList <>();
216+ for ( Map .Entry <TreeSet <String >,Integer > entry : itemfreq .entrySet () )
217+ {
218+ TreeSet <String > temp =entry .getKey ();
219+ System .out .println ( temp + " => " + entry .getValue () );
220+ list .add (temp );
221+ //HashSet<String> set=new HashSet<>();
222+ //set.add(entry.getKey());
223+ //calsupport.put(set,entry.getValue());
224+ }
225+ itemfreq .clear ();
226+
227+ common ++;
228+ num ++;
229+ counter =factorial (num );
230+
231+ HashMap <TreeSet <String >,Integer > setfreq =new HashMap <>();
232+ for (int i =0 ;i <list .size ();i ++)
233+ {
234+ for (int j =0 ;j <list .size ();j ++)
235+ {
236+ if (i !=j )
237+ {
238+ TreeSet <String > hs1 =list .get (i );
239+ TreeSet <String > hs2 =list .get (j );
240+ //System.out.println(hs1+" "+hs2);
241+ int getcommon =findintersection (hs1 ,hs2 );
242+ //System.out.println("common="+getcommon+" ogcomm="+common);
243+ if (getcommon ==common )
244+ {
245+ TreeSet <String > merge =mergeSet (hs1 ,hs2 );
246+ if (setfreq .containsKey (merge )==false )
247+ {
248+ //System.out.println("first time="+merge);
249+ setfreq .put (merge ,1 );
250+ }
251+ else
252+ {
253+ //System.out.println("second time="+merge);
254+ setfreq .put (merge ,setfreq .get (merge )+1 );
255+ }
256+ }
257+ }
258+
259+ }
260+ }
261+
262+ for ( Map .Entry <TreeSet <String >, Integer > entry : setfreq .entrySet () )
263+ {
264+
265+ TreeSet <String > temp =entry .getKey ();
266+ int freq1 =entry .getValue ();
267+ //System.out.println("starter\n"+ temp + " => " + freq1 +" "+counter);
268+ if (freq1 ==counter )
269+ {
270+ itemfreq .put (temp ,frequency (map ,temp ));
271+ }
272+
273+ }
274+
275+ //System.out.println("SIZE after"+itemfreq.size());
276+
277+ if (itemfreq .size ()==0 )
278+ break ;
279+
280+ finalcomb =new HashMap <>(itemfreq );
281+ //finalcomb=itemfreq.clone();
282+
283+
284+
285+ }
286+
287+ System .out .println ("----------------------FINAL----------------------" );
288+
289+ for ( Map .Entry <TreeSet <String >,Integer > entry : finalcomb .entrySet () )
290+ {
291+ TreeSet <String > temp =entry .getKey ();
292+
293+ System .out .println ( temp + " => " + entry .getValue () );
294+
295+ // list.add(temp);
296+ //HashSet<String> set=new HashSet<>();
297+ //set.add(entry.getKey());
298+ //calsupport.put(set,entry.getValue());
299+ }
300+
301+ for ( Map .Entry <TreeSet <String >,Integer > entry : finalcomb .entrySet () )
302+ {
303+ TreeSet <String > temp =entry .getKey ();
304+
305+ supportof =frequency (map ,temp );
306+ List <String > list1 = new ArrayList <>(temp );
307+ System .out .println (list1 );
308+ for (int i =0 ;i <list1 .size ();i ++)
309+ {
310+ for (int j =0 ;j <list1 .size ();j ++)
311+ {
312+ if (i >j )
313+ continue ;
314+ if (i ==j )
315+ {
316+ str11 =list1 .get (i );
317+ TreeSet <String > temp2 =new TreeSet <>();
318+ temp2 .add (str11 );
319+ confi =(supportof /frequency (map ,temp2 ))*100 ;
320+ if (confi >=confidence )
321+ {
322+ System .out .println (temp2 +" confidence=" +confidence );
323+ }
324+ }
325+ else
326+ {
327+ str11 =list1 .get (i );
328+ str12 =list1 .get (j );
329+ TreeSet <String > temp2 =new TreeSet <>();
330+ temp2 .add (str11 );
331+ temp2 .add (str12 );
332+ confi =(supportof /frequency (map ,temp2 ))*100 ;
333+ if (confi >=confidence )
334+ {
335+ System .out .println (temp2 +" confidence=" +confi );
336+ }
337+ }
338+ }
339+
340+
341+ }
342+
343+ //System.out.println( temp + " => " + entry.getValue() );
344+
345+ // list.add(temp);
346+ //HashSet<String> set=new HashSet<>();
347+ //set.add(entry.getKey());
348+ //calsupport.put(set,entry.getValue());
349+ }
350+
351+
352+
353+
354+
355+
356+ }
357+
358+ static int findintersection (TreeSet <String > hs1 ,TreeSet <String > hs2 )
359+ {
360+ int common =0 ;
361+ for (String str :hs1 )
362+ {
363+ if (hs2 .contains (str ))
364+ common ++;
365+ }
366+ return common ;
367+ }
368+
369+ public static TreeSet <String > mergeSet (TreeSet <String > a , TreeSet <String > b )
370+ {
371+ return new TreeSet <String >() {{
372+ addAll (a );
373+ addAll (b );
374+ } };
375+ }
376+ }
0 commit comments