loglog信息处理算法C++语言具体实现

Loglog算法
1:Initialize M[1],M[2],…M[m] to 0;
2: for each element x read from the file “stream_for_fm.txt”, do the following 3-6:
3:Let h(x) represent the hash value (in binary form) of the element x, let p(y) be the rank of first 1-bit from the right in y(for example, if y=1100 then p(y)=3, if y=1111, then p(y)=1);
4: set j = h(x)%m + 1; //treat h(x) as an integer, then get the bucket id//
5: set w = floor(h(x)/m); //remove the log2(m) least significant bits in h(x)//
6: set M[j] = max(M[j],p(w));
7: set s = (M[1]+M[2]+…+M[m])/m; and return E = 0.39701m2s as the estimate for the number of distinct elements in the file;

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <math.h>
#include <time.h>

#define N (int)10000+1

using namespace std;

map<int,int>::iterator iter;
map<int,int> total;

int a,b;

int Tail_first_one(int n);
void Select_max(int *M,int x,int i);

int main()
{
    int data;
    int M[10000]={0};
    int m,i,j,p,h,w;
    double sum=0;
    double estimate;
    printf("输入m大小:  ");
    scanf("%d",&m);

    FILE *fp;
    fp = fopen("stream_for_fm.txt","r");
    if(fp == NULL)
    {
        printf("open file falled!");
    }
    while(!feof(fp))
    {
        fscanf(fp,"%d",&data);
        total[data]++;
    }
    srand((unsigned)time(NULL));

    for(iter = total.begin();iter != total.end();iter++)
    {
        h = iter->first;
        j = h%m+1;
        w = h/m;
        p = Tail_first_one(w);
        Select_max(M,p,j);
    }
    for(i=1;i<=m;i++)
    {
        sum += M[i];
    }

    estimate = 0.39701*m*pow(2,sum/m);
    printf("Loglog算法估计值:%f",estimate);

    free(fp);
    return 0;
}

void Select_max(int *M,int x,int i)
{
    M[i] = M[i]>x?M[i]:x;
}

int Tail_first_one(int n)
{
    int first_one=0;
    while(n != 0)
    {
        if(n%2 == 0)
        {
            first_one++;
        }
        else
        {
            first_one++;
            break;
        }
        n = n/2;
    }
    return first_one;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风华绝代飞入鬓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值