海量数据找缺失的值
来源:互联网 发布:windows安装git详解 编辑:程序博客网 时间:2024/05/20 16:43
一个文件中含有4billion的integers, 试图找出一个不再这个文件中的数。
假如一个integer用4bytes表示,那么文件的大小就是4 * 10 ^9 * 4 = 16 GB。 文件太大了。
(1)办法一: 采用外排序的办法。对这些数据排完序之后, 可以从第一个数开始遍历, 知道出现gap, 表示gap之间缺少一个元素。这样我们输出的是第一个missing的value。
(2)办法二:
假如我们的数据是32位表示的整数, 总共可能的值是2^32个distinct integers, 约为4 billion(4 * 10^9)。
情况1: 我们有1 * 10^9 bytes * 8bit/byte = 8 billion bits 的内存(足以表示出4 billion的所有可能的互异的numbers了), 我们可以使用一个bit代表一个整数, 也就是使用Bitmap的办法。我们就可以不经过排序就可以检索到整数的位置。 我们只需要遍历,将发现的数存在bitmap中, 出现即将相应的index的bitmap的值设为1。 查找到bitmap中为0的位置, 那么这个位置的index就是missing value。
情况2: 我们只有10MB = 80 million bits的内存, 我们可以采用如下的办法求解。
对于所有可能的16位前缀(我们的每一个数字都是32位的, 这里是高16位),每一个对应的前缀有2^16种可能的情况。 如下:
00000000 00000000 : 2^16种可能的数字(低位)
00000000 00000001 : 2^16种可能的数字(低位)
...........................................................................
11111111 11111111 : 2^16种可能的数字(低位)
也就是相当于有2^16个bucket,每一个bucket有2^16个可能的数字(对应着低位)。 然后我们对文件中的4billion个数字进行统计。 具有相同16bit前缀的数字将在同一个bucket, 相当于对这个bucket进行加1。 统计完成后, 只要有缺失的数字, 那么这个缺失的数字的前缀对应的bucket的计数值将小于2^16个。 接下来我们只需要把这这个前缀记录下来。
然后释放小所有的内陈。 对那4 billion个数重新遍历, 此时我们的办法是统计的是后缀。 后缀的bucket的数目也是2^16个。 然后开始统计。 和刚才记录的前缀相同, 且这个数对应的后缀的那个bucket设置为1, 表示存在。 统计完成之后, 我们只需要遍历这些bucket, bucket计数值为0的表示这个后缀不存在。 那么此时就找到了, 将刚才那个前缀(记录下的)和这个为0 的后缀的bucket的数字concanate起来, 最终就得了一个missing value。
介绍完上述的面试题之后, 无意中看到了格雷码的产生办法。 记录如下:
问题: Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.
办法:
n-bit Gray Codes can be generated from list of (n-1)-bit Gray codes using following steps.
1) Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is reverse of L1.
2) Modify the list L1 by prefixing a ‘0’ in all codes of L1.
3) Modify the list L2 by prefixing a ‘1’ in all codes of L2.
4) Concatenate L1 and L2. The concatenated list is required list of n-bit Gray codes.
For example, following are steps for generating the 3-bit Gray code list from the list of 2-bit Gray code list.
L1 = {00, 01, 11, 10} (List of 2-bit Gray Codes)
L2 = {10, 11, 01, 00} (Reverse of L1)
Prefix all entries of L1 with ‘0’, L1 becomes {000, 001, 011, 010}
Prefix all entries of L2 with ‘1’, L2 becomes {110, 111, 101, 100}
Concatenate L1 and L2, we get {000, 001, 011, 010, 110, 111, 101, 100}
程序如下:
// C++ program to generate n-bit Gray codes#include <iostream>#include <string>#include <vector>using namespace std;// This function generates all n bit Gray codes and prints the// generated codesvoid generateGrayarr(int n) { // base case if (n <= 0) return; // vector of strings, store the gray code vector<string> arr; // start with one-bit pattern arr.push_back("0"); // arr[0] = 0 arr.push_back("1"); // arr[1] = 1 // Every iteration of this loop generates 2*i codes from previously // generated i codes. for (int i = 2; i < (1<<n); i = i<<1) { // Enter the prviously generated codes again in arr[] in reverse // order. Nor arr[] has double number of codes. for (int j = i-1 ; j >= 0 ; j--) arr.push_back(arr[j]); // arr[j] 代表第j个gray code // append 0 to the first half for (int j = 0 ; j < i ; j++) arr[j] = "0" + arr[j]; // append 1 to the second half for (int j = i ; j < 2*i ; j++) arr[j] = "1" + arr[j]; } // print contents of arr[] for (int i = 0 ; i < static_cast<int>(arr.size()) ; i++ ) cout << arr[i] << endl;}// Driver program to test above functionint main() { generateGrayarr(4); return 0;}运行结果如下:
- 海量数据找缺失的值
- 海量数据找中位数
- 海量数据找中位数
- 海量数据找中位数
- 海量数据找中位数
- 【数据建模 缺失值处理】缺失值的处理
- 找一个数组中缺失的数字
- missing letters --找缺失的字母
- 数据缺失的处理方法
- 数据缺失的中文字符串
- 【R】数据缺失值处理
- sklearn 数据缺失值处理
- 数据中的缺失值处理
- 数据清洗---缺失值处理
- 数据挖掘-处理缺失值
- 数据缺失值的4种处理方法
- 数据缺失值的4种处理方法
- 数据分析中缺失值的处理方法
- ubuntu下math.h头文件的编译问题
- MinGW
- 线性代数导论28——正定矩阵和最小值
- Nginx 战斗准备:优化指南
- 使用自定义属性报错 No resource identifier found for attribute 'widthFactor' in package
- 海量数据找缺失的值
- Android HardwareComposer中的fence机制
- HDU 5027 Help!
- 杭电1379(DNA Sorting)java面向对象编程
- 线性代数导论29——相似矩阵和若尔当形
- filter()map() reduce()
- High work stress makes you feel anxiety and it can lead you ill
- 线性代数导论30——奇异值分解
- 在Ubuntu中使用eclipse操作HDFS跑wordcount程序