-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHyperLogLog.java
More file actions
248 lines (183 loc) · 5.56 KB
/
HyperLogLog.java
File metadata and controls
248 lines (183 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package ProbablisticCounting;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NavigableMap;
import java.util.TreeMap;
import org.jfree.ui.RefineryUtilities;
public class HyperLogLog {
final static String inputFile = "C:\\sem 1\\ITM\\Project\\FlowTraffic.txt";
final static int b = 5;
final static int m= (int)Math.pow(2,b);
final static double Alpha16 = 0.673, Alpha32 = 0.697, Alpha64 = 0.709, AlphaM = 0.7213/(1+1.079/m);
NavigableMap<String, HashSet<String>> flowList = new TreeMap<String, HashSet<String>>();
HashSet<String> binarySet = new HashSet<String>();
int M[] = new int[m];
String line = "";
String sourceIP = "";
String destIP = "";
double E = 0.0;
double Eprime = 0.0;
int actualCardinality = 0;
double estimatedCardinality = 0;
private static HashMap<Integer,Integer> resultGraph = new HashMap<Integer,Integer>();
HyperLogLog(){
try{
buildFlowHashMap();
initializeM();
hashToBinary();
}
catch(Exception e){
}
}
public void buildFlowHashMap() throws IOException{
int start = 1;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
//flowList.put("0", "0");
while((line = br.readLine()) != null) {
/* Split each line of the file on " " */
String[] columns = line.split(" ");
/* Assign first column of the file to source IP */
sourceIP = columns[0];
/* Assign Destination IP */
for(int k=1; k<columns.length;k++){
if(!(columns[k].equalsIgnoreCase(""))){
destIP = columns[k];
break;
}
}
if(start ==1){
HashSet<String> newFlow = new HashSet<String>();
newFlow.add(destIP);
flowList.put(sourceIP, newFlow);
start = 0;
}
else if(flowList.lastKey().equals(sourceIP)){
flowList.get(sourceIP).add(destIP);
}
else{
HashSet<String> newFlow = new HashSet<String>();
newFlow.add(destIP);
flowList.put(sourceIP, newFlow);
}
}
br.close();
}
public void initializeM(){
for(int i=0; i<m;i++){
M[i] = 0;
}
}
/*public void findFirstBBits(){
for(String s : binarySet){
String lastBBits = s.substring((s.length()-b), s.length());
int j = Integer.parseInt(lastBBits, 2);
//String newString = s-lastBBits;
String w = s.substring(0,(s.length()-b));
//int M[] = new int[m];
int W = calP(w);
if(M[j]>=W){
M[j] = M[j];
}
else{
M[j] = W;
}
}
}
*/
public int calP(String s){
int count = 0;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='0'){
count++;
}
else{
break;
}
}
return count+1;
}
public void hashToBinary(){
for (String flow: flowList.keySet()){
actualCardinality = flowList.get(flow).size();
for (String eachFlow : flowList.get(flow)) {
try{
int hash = (getUUID(eachFlow)& 0x7fffffff);
String padding = "%"+m+"s";
String binary = String.format(padding, Integer.toBinaryString(hash)).replace(' ', '0');
System.out.println("Binary = "+binary);
//String binary = String.valueOf(Integer.toBinaryString(e)eachFlow.hashCode());
//int j = eachFlow.hashCode() & 0x3F;
String lastBBits = binary.substring((binary.length()-b), binary.length());
//String firstBBits = binary.substring(0, b);
int j = Integer.parseInt(lastBBits, 2);
//String newString = s-lastBBits;
String w = binary.substring(0,(binary.length()-b));
// String w1 = binary.substring(b,binary.length());
//int M[] = new int[m];
int W = calP(w);
M[j] = Math.max(M[j], W);
}
catch(Exception e){
System.out.println();
}
}
double sum = 0;
for(int j=0;j<m;j++){
sum+= (double) Math.pow(2,(-M[j]));
}
E = (Alpha32*(Math.pow(m, 2)))/sum;
System.out.println(Math.pow(m, 2));
System.out.println(((Math.pow(m, 2))*(Math.pow(sum, -1))));
estimatedCardinality = rawEstimate(E);
System.out.println("n= "+actualCardinality+"ncap =" +estimatedCardinality);
resultGraph.put(actualCardinality, (int)estimatedCardinality);
initializeM();
}
}
public int calculateZeroRegisters(){
int count = 0;
for(int i=0;i<m;i++){
if(M[i] == 0){
count ++;
}
}
return count;
}
public double rawEstimate(Double E){
if(E<=m*(5/2)){
double V = calculateZeroRegisters();
if(V!=0){
Eprime = m*Math.log(m/V);
System.out.println(Math.log(m/V));
}
else{
Eprime = E;
}
}
else if (E<=(Math.pow(2,32)/30)){
Eprime = E;
}
else if (E>(Math.pow(2,32)/30)){
Eprime =((-1*Math.pow(2,32))*(Math.log(1-E/(Math.pow(2,32)))));
}
return Eprime;
}
static int getUUID(String name) throws NoSuchAlgorithmException {
SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
srA.setSeed(name.getBytes());
return new Integer(srA.nextInt());
}
public static void main (String x[]){
HyperLogLog hll = new HyperLogLog();
/* Chart Plotting*/
ScatterPlot chart = new ScatterPlot("ITM Project 4", "HyperLogLog", resultGraph);
chart.pack( );
RefineryUtilities.centerFrameOnScreen( chart );
chart.setVisible( true );
}
}