Code
Python
import random import math def trailing_zeros(x): """ Counting number of trailing zeros in the binary representation of x.""" if x = = 0 : return 1 count = 0 while x & 1 = = 0 : count + = 1 x >> = 1 return count def flajolet_martin(dataset, k): """Number of distinct elements using the Flajolet-Martin Algorithm.""" max_zeros = 0 for i in range (k): hash_vals = [trailing_zeros(random.choice(dataset)) for _ in range ( len (dataset))] max_zeros = max (max_zeros, max (hash_vals)) return 2 * * max_zeros # Example dataset = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] dist_num = flajolet_martin(dataset, 10 ) print ( "Estimated number of distinct elements:" , dist_num) |
Output :
'Estimated number of distinct elements:', 8
The output of the code will vary from run to run due to the random nature of the Flajolet Martin Algorithm. The output of this example should be close to 10.
Flajolet Martin Algorithm
The Flajolet-Martin algorithm is also known as probabilistic algorithm which is mainly used to count the number of unique elements in a stream or database . This algorithm was invented by Philippe Flajolet and G. Nigel Martin in 1983 and since then it has been used in various applications such as , data mining and database management.
The basic idea to which Flajolet-Martin algorithm is based on is to use a hash function to map the elements in the given dataset to a binary string, and to make use of the length of the longest null sequence in the binary string as an estimator for the number of unique elements to use as a value element.
The steps for the Flajolet-Martin algorithm are:
- First step is to choose a hash function that can be used to map the elements in the database to fixed-length binary strings. The length of the binary string can be chosen based on the accuracy desired.
- Next step is to apply the hash function to each data item in the dataset to get its binary string representation.
- Next step includes determinig the position of the rightmost zero in each binary string.
- Next we compute the maximum position of the rightmost zero for all binary strings.
- Now we estimate the number of distinct elements in the dataset as 2 to the power of the maximum position of the rightmost zero which we calculated in previous step.
The accuracy of Flajolet Martin Algorithm is determined by the length of the binary strings and the number of hash functions it uses. Generally, with increse in the length of the binary strings or using more hash functions in algorithm can often increase the algorithm’s accuracy.
The Flajolet Martin Algorithm is especially used for big datasets that cannot be kept in memory or analysed with regular methods. This algorithm , by using good probabilistic techniques, can provide a precise estimate of the number of unique elements in the data set by using less computing.