Shailrshah

Bloom Filter

Mar 29th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.13 KB | None | 0 0
  1. %%% bloom.m %%%
  2. m = input('Array size: ');
  3. n = 0;
  4. total = input('How many numbers will you be inserting?: ');
  5. nice_k = (m/total)*log(2);
  6. sprintf('Recomended hash functions: %d', ceil(nice_k))
  7. k = input('Number of hash functions: ');
  8. h = zeros(1, k);
  9. a = zeros(1, m+1);
  10.  
  11. while n<total
  12.     x = input('Number to insert: ');
  13.     h = hash(m,x,k);
  14.     disp(h)
  15.     for i=1:length(h)
  16.         if h(i)==1
  17.             n=n+1;
  18.             break;
  19.         end
  20.     end
  21.     for i=1:length(h)
  22.         a(h(i)) = 1;
  23.     end
  24.     n=n+1;
  25. end
  26.  
  27. %%% hash.m %%%
  28. function res = hash(m, x, k)
  29.     res = zeros(1,k);
  30.     x_bin = dec2bin(x);
  31.     for i=1:k
  32.         temp = '';
  33.         for j=i:k:length(x_bin)
  34.             temp = strcat(temp, x_bin(j));
  35.         end
  36.         res(i)=mod(bin2dec(temp), m)+1;
  37.     end
  38. end
  39.  
  40. %%% bloomask.m %%%
  41. while true
  42.    x = input('Enter a number: ');
  43.    h = hash(m, x, k);
  44.    ans=0;
  45.    for i=1:length(h)
  46.        if a(h(i))==0
  47.            sprintf('Not found')
  48.            ans = 1;
  49.            break;
  50.        end
  51.    end
  52.    if ans==0
  53.        sprintf('Found with a false positive rate of %d', (1-exp(-k*n/m))^k)
  54.    end
  55. end
Add Comment
Please, Sign In to add comment