当前位置:   article > 正文

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

大数据分而志之

百度面试题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自带的,当然你也可以用别的

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

运行结果


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/830095
推荐阅读
相关标签
  

闽ICP备14008679号