大数据处理算法三:分而治之/hash映射 + hash统计 + 堆/快速/归并排序

来源:互联网 发布:网络广告位招商 编辑:程序博客网 时间:2024/05/22 00:50

百度面试题1、海量日志数据,提取出某日访问百度次数最多的那个IP

IP 32位的,最多有个2^32IP。同样可以采用映射的方法,比如模1000,把整个大文件映射为1000个小文件,再找出每个小文中出现频率最大的 IP(可以采用hash_map进行频率统计,然后再找出频率最大的几个)及相应的频率。然后再在这1000个最大的IP中,找出那个频率最大的IP,即 为所求。

 

百度面试题2、搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节。

假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果除去重复后,不超过3百万个。一个查询串的重复度越高,说明查询它的用户越多,也就是越热门。),请你统计最热门的10个查询串,要求使用的内存不能超过1G

第 一步借用hash统计进行预处理: 先对这批海量数据预处理(维护一个KeyQuery字串,Value为该Query出现次数,即Hashmap(QueryValue),每次读取一 个Query,如果该字串不在Table中,那么加入该字串,并且将Value值设为1;如果该字串在Table中,那么将该字串的计数加一即可。最终我 们在O(N)N1千万,因为要遍历整个数组一遍才能统计处每个query出现的次数)的时间复杂度内用Hash表完成了统计;
           第二步借用堆排序找出最热门的10个查询串:时间复杂度为N'*logK。维护一个K(该题目中是10)大小的小根堆,然后遍历3百万个Query,分别和根元素进行对比(对比value的值),找出10value值最大的query
           最终的时间复杂度是:ON) + N'*OlogK),(N1000万,N’为300万)

或者:采用trie树,关键字域存该查询串出现的次数,没有出现为0。最后用10个元素的最小推来对出现频率进行排序。

 

我们先看HashMap 实现

1. HashMap的数据结构

数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端。

      数组

数组存储区间是连续的,占用内存严重,故空间复杂的很大。但数组的二分查找时间复杂度小,为O(1)数组的特点是:寻址容易,插入和删除困难;

链表

链表存储区间离散,占用内存比较宽松,故空间复杂度很小,但时间复杂度很大,达ON)。链表的特点是:寻址困难,插入和删除容易。

哈希表

那么我们能不能综合两者的特性,做出一种寻址容易,插入删除也容易的数据结构?答案是肯定的,这就是我们要提起的哈希表。哈希表((Hash table)既满足了数据的查找方便,同时不占用太多的内容空间,使用也十分方便。

  哈希表有多种不同的实现方法,我接下来解释的是最常用的一种方法—— 拉链法,我们可以理解为“链表的数组”

 

 

 

我用java 自己实现了一个HashMap,当然这比较简点,不过能说明大概原理,改有的功能基本上有了

index=hashCode(key)=key%16    

哈希算法很多,下面我用了java自带的,当然你也可以用别的

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 自定义 HashMap 
  3.  * @author JYC506 
  4.  * 
  5.  * @param <K> 
  6.  * @param <V> 
  7.  */  
  8. public class HashMap<K, V> {  
  9.   
  10.     private static final int CAPACTITY = 16;  
  11.   
  12.     transient Entry<K, V>[] table = null;  
  13.   
  14.     @SuppressWarnings("unchecked")  
  15.     public HashMap() {  
  16.         super();  
  17.         table = new Entry[CAPACTITY];  
  18.     }  
  19.   
  20.     /* 哈希算法 */  
  21.     private final int toHashCode(Object obj) {  
  22.         int h = 0;  
  23.         if (obj instanceof String) {  
  24.             return StringHash.toHashCode((String) obj);  
  25.         }  
  26.         h ^= obj.hashCode();  
  27.         h ^= (h >>> 20) ^ (h >>> 12);  
  28.         return h ^ (h >>> 7) ^ (h >>> 4);  
  29.     }  
  30.    /*放入hashMap*/  
  31.     public void put(K key, V value) {  
  32.         int hashCode = this.toHashCode(key);  
  33.         int index = hashCode % CAPACTITY;  
  34.         if (table[index] == null) {  
  35.             table[index] = new Entry<K, V>(key, value, hashCode);  
  36.         } else {  
  37.             for (Entry<K, V> entry = table[index]; entry != null; entry = entry.nextEntry) {  
  38.                 if (entry.hashCode == hashCode && (entry.key == key || key.equals(entry.key))) {  
  39.                     entry.value = value;  
  40.                     return;  
  41.                 }  
  42.             }  
  43.             Entry<K, V> entry2 = table[index];  
  44.             Entry<K, V> entry3 = new Entry<K, V>(key, value, hashCode);  
  45.             entry3.nextEntry = entry2;  
  46.             table[index] = entry3;  
  47.   
  48.         }  
  49.     }  
  50.    /*获取值*/  
  51.     public V get(K key) {  
  52.         int hashCode = this.toHashCode(key);  
  53.         int index = hashCode % CAPACTITY;  
  54.         if (table[index] == null) {  
  55.             return null;  
  56.         } else {  
  57.             for (Entry<K, V> entry = table[index]; entry != null; entry = entry.nextEntry) {  
  58.                 if (entry.hashCode == hashCode && (entry.key == key || key.equals(entry.key))) {  
  59.                     return entry.value;  
  60.                 }  
  61.             }  
  62.             return null;  
  63.   
  64.         }  
  65.     }  
  66.      /*删除*/  
  67.     public void remove(K key){  
  68.         int hashCode = this.toHashCode(key);  
  69.         int index = hashCode % CAPACTITY;  
  70.         if (table[index] == null) {  
  71.             return ;  
  72.         } else {  
  73.             Entry<K, V> parent=null;  
  74.             for (Entry<K, V> entry = table[index]; entry != null; entry = entry.nextEntry) {  
  75.                 if (entry.hashCode == hashCode && (entry.key == key || key.equals(entry.key))) {  
  76.                     if(parent!=null){  
  77.                         parent.nextEntry=entry.nextEntry;  
  78.                         entry=null;  
  79.                         return ;  
  80.                     }  
  81.                 }  
  82.                 parent=entry;  
  83.             }  
  84.         }  
  85.     }  
  86.     public static void main(String[] args) {  
  87.         HashMap<String,String> map=new HashMap<String,String>();  
  88.         map.put("1""2");  
  89.         map.put("1""3");  
  90.         map.put("3""哈哈哈");  
  91.         System.out.println(map.get("1"));  
  92.         System.out.println(map.get("3"));  
  93.         map.remove("1");  
  94.         System.out.println(map.get("1"));  
  95.     }  
  96.   
  97. }  
  98.   
  99. class Entry<K, V> {  
  100.     K key;  
  101.     V value;  
  102.     int hashCode;  
  103.     Entry<K, V> nextEntry;  
  104.   
  105.     public Entry(K key, V value, int hashCode) {  
  106.         super();  
  107.         this.key = key;  
  108.         this.value = value;  
  109.         this.hashCode = hashCode;  
  110.     }  
  111.   
  112. }  
  113.   
  114. /* 字符串hash算法 */  
  115. class StringHash {  
  116.     public static final int toHashCode(String str) {  
  117.         /* 我用java自带的 */  
  118.         return str.hashCode();  
  119.     }  
  120. }  
0 0
原创粉丝点击