From cced1cbb8924a19dbdabff664e285fc91e3ffab7 Mon Sep 17 00:00:00 2001 From: krahets Date: Wed, 14 Jun 2023 03:58:26 +0800 Subject: [PATCH] deploy --- 404.html | 2 +- chapter_appendix/contribution/index.html | 2 +- chapter_appendix/installation/index.html | 2 +- chapter_array_and_linkedlist/array/index.html | 2 +- chapter_array_and_linkedlist/index.html | 2 +- .../linked_list/index.html | 2 +- chapter_array_and_linkedlist/list/index.html | 2 +- .../summary/index.html | 2 +- .../backtracking_algorithm/index.html | 2 +- chapter_backtracking/index.html | 2 +- .../n_queens_problem/index.html | 2 +- .../permutations_problem/index.html | 2 +- chapter_computational_complexity/index.html | 2 +- .../performance_evaluation/index.html | 2 +- .../space_complexity/index.html | 2 +- .../summary/index.html | 2 +- .../time_complexity/index.html | 2 +- .../basic_data_types/index.html | 2 +- .../character_encoding/index.html | 2 +- .../index.html | 2 +- chapter_data_structure/index.html | 2 +- .../number_encoding/index.html | 2 +- chapter_data_structure/summary/index.html | 2 +- chapter_graph/graph/index.html | 2 +- chapter_graph/graph_operations/index.html | 2 +- chapter_graph/graph_traversal/index.html | 2 +- chapter_graph/index.html | 2 +- chapter_graph/summary/index.html | 2 +- chapter_hashing/hash_collision/index.html | 30 ++++++++++-------- chapter_hashing/hash_map/index.html | 6 ++-- chapter_hashing/index.html | 2 +- chapter_hashing/summary/index.html | 6 ++-- chapter_heap/build_heap/index.html | 2 +- chapter_heap/heap/index.html | 2 +- chapter_heap/index.html | 2 +- chapter_heap/summary/index.html | 2 +- chapter_heap/top_k/index.html | 2 +- .../algorithms_are_everywhere/index.html | 2 +- chapter_introduction/index.html | 2 +- chapter_introduction/summary/index.html | 2 +- chapter_introduction/what_is_dsa/index.html | 2 +- chapter_preface/about_the_book/index.html | 2 +- chapter_preface/index.html | 2 +- chapter_preface/suggestions/index.html | 2 +- chapter_preface/summary/index.html | 2 +- chapter_reference/index.html | 2 +- chapter_searching/binary_search/index.html | 2 +- .../binary_search_edge/index.html | 2 +- chapter_searching/index.html | 2 +- .../replace_linear_by_hashing/index.html | 2 +- .../searching_algorithm_revisited/index.html | 2 +- chapter_searching/summary/index.html | 2 +- chapter_sorting/bubble_sort/index.html | 2 +- chapter_sorting/bucket_sort/index.html | 2 +- chapter_sorting/counting_sort/index.html | 2 +- chapter_sorting/heap_sort/index.html | 2 +- chapter_sorting/index.html | 2 +- chapter_sorting/insertion_sort/index.html | 2 +- chapter_sorting/merge_sort/index.html | 2 +- chapter_sorting/quick_sort/index.html | 2 +- chapter_sorting/radix_sort/index.html | 2 +- chapter_sorting/selection_sort/index.html | 2 +- chapter_sorting/sorting_algorithm/index.html | 2 +- chapter_sorting/summary/index.html | 2 +- chapter_stack_and_queue/deque/index.html | 2 +- chapter_stack_and_queue/index.html | 2 +- chapter_stack_and_queue/queue/index.html | 2 +- chapter_stack_and_queue/stack/index.html | 2 +- chapter_stack_and_queue/summary/index.html | 2 +- .../array_representation_of_tree/index.html | 2 +- chapter_tree/avl_tree/index.html | 2 +- chapter_tree/binary_search_tree/index.html | 2 +- chapter_tree/binary_tree/index.html | 2 +- chapter_tree/binary_tree_traversal/index.html | 2 +- chapter_tree/index.html | 2 +- chapter_tree/summary/index.html | 2 +- index.html | 2 +- search/search_index.json | 2 +- sitemap.xml.gz | Bin 763 -> 763 bytes 79 files changed, 97 insertions(+), 95 deletions(-) diff --git a/404.html b/404.html index 314765e7e..a323ab7bd 100644 --- a/404.html +++ b/404.html @@ -981,7 +981,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_appendix/contribution/index.html b/chapter_appendix/contribution/index.html index 4e655d946..b1ed4d112 100644 --- a/chapter_appendix/contribution/index.html +++ b/chapter_appendix/contribution/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_appendix/installation/index.html b/chapter_appendix/installation/index.html index c2aa2af0f..25bdf16e2 100644 --- a/chapter_appendix/installation/index.html +++ b/chapter_appendix/installation/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_array_and_linkedlist/array/index.html b/chapter_array_and_linkedlist/array/index.html index 79233db83..bd1d8640c 100644 --- a/chapter_array_and_linkedlist/array/index.html +++ b/chapter_array_and_linkedlist/array/index.html @@ -1056,7 +1056,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_array_and_linkedlist/index.html b/chapter_array_and_linkedlist/index.html index c5115fcc9..a53ef95ad 100644 --- a/chapter_array_and_linkedlist/index.html +++ b/chapter_array_and_linkedlist/index.html @@ -994,7 +994,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_array_and_linkedlist/linked_list/index.html b/chapter_array_and_linkedlist/linked_list/index.html index a45025967..3667916d1 100644 --- a/chapter_array_and_linkedlist/linked_list/index.html +++ b/chapter_array_and_linkedlist/linked_list/index.html @@ -1056,7 +1056,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_array_and_linkedlist/list/index.html b/chapter_array_and_linkedlist/list/index.html index 1286fe2f9..a11bf8daf 100644 --- a/chapter_array_and_linkedlist/list/index.html +++ b/chapter_array_and_linkedlist/list/index.html @@ -1042,7 +1042,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_array_and_linkedlist/summary/index.html b/chapter_array_and_linkedlist/summary/index.html index 0082a40c5..d758ca946 100644 --- a/chapter_array_and_linkedlist/summary/index.html +++ b/chapter_array_and_linkedlist/summary/index.html @@ -1035,7 +1035,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_backtracking/backtracking_algorithm/index.html b/chapter_backtracking/backtracking_algorithm/index.html index 806ddf674..7df4b6c31 100644 --- a/chapter_backtracking/backtracking_algorithm/index.html +++ b/chapter_backtracking/backtracking_algorithm/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_backtracking/index.html b/chapter_backtracking/index.html index abe56f0ea..f35117a4e 100644 --- a/chapter_backtracking/index.html +++ b/chapter_backtracking/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_backtracking/n_queens_problem/index.html b/chapter_backtracking/n_queens_problem/index.html index ef66e201d..8f27093d2 100644 --- a/chapter_backtracking/n_queens_problem/index.html +++ b/chapter_backtracking/n_queens_problem/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_backtracking/permutations_problem/index.html b/chapter_backtracking/permutations_problem/index.html index 3154eb729..627d3f0dc 100644 --- a/chapter_backtracking/permutations_problem/index.html +++ b/chapter_backtracking/permutations_problem/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_computational_complexity/index.html b/chapter_computational_complexity/index.html index 8a7d3c2d6..25e2a4008 100644 --- a/chapter_computational_complexity/index.html +++ b/chapter_computational_complexity/index.html @@ -994,7 +994,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_computational_complexity/performance_evaluation/index.html b/chapter_computational_complexity/performance_evaluation/index.html index a4033ca87..a26b2902e 100644 --- a/chapter_computational_complexity/performance_evaluation/index.html +++ b/chapter_computational_complexity/performance_evaluation/index.html @@ -1069,7 +1069,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_computational_complexity/space_complexity/index.html b/chapter_computational_complexity/space_complexity/index.html index 05b921c26..dcebabbc7 100644 --- a/chapter_computational_complexity/space_complexity/index.html +++ b/chapter_computational_complexity/space_complexity/index.html @@ -1097,7 +1097,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_computational_complexity/summary/index.html b/chapter_computational_complexity/summary/index.html index 172063eb7..3fbd869c3 100644 --- a/chapter_computational_complexity/summary/index.html +++ b/chapter_computational_complexity/summary/index.html @@ -1035,7 +1035,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_computational_complexity/time_complexity/index.html b/chapter_computational_complexity/time_complexity/index.html index d472c7b56..614265436 100644 --- a/chapter_computational_complexity/time_complexity/index.html +++ b/chapter_computational_complexity/time_complexity/index.html @@ -1145,7 +1145,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/basic_data_types/index.html b/chapter_data_structure/basic_data_types/index.html index 859862f67..3879ea056 100644 --- a/chapter_data_structure/basic_data_types/index.html +++ b/chapter_data_structure/basic_data_types/index.html @@ -1004,7 +1004,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/character_encoding/index.html b/chapter_data_structure/character_encoding/index.html index fc65c9b83..f43451469 100644 --- a/chapter_data_structure/character_encoding/index.html +++ b/chapter_data_structure/character_encoding/index.html @@ -1063,7 +1063,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/classification_of_data_structure/index.html b/chapter_data_structure/classification_of_data_structure/index.html index 333c5036b..7f1f4ee85 100644 --- a/chapter_data_structure/classification_of_data_structure/index.html +++ b/chapter_data_structure/classification_of_data_structure/index.html @@ -1042,7 +1042,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/index.html b/chapter_data_structure/index.html index 1a23db50f..b44e4662d 100644 --- a/chapter_data_structure/index.html +++ b/chapter_data_structure/index.html @@ -994,7 +994,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/number_encoding/index.html b/chapter_data_structure/number_encoding/index.html index 17944a272..0777be90e 100644 --- a/chapter_data_structure/number_encoding/index.html +++ b/chapter_data_structure/number_encoding/index.html @@ -1042,7 +1042,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_data_structure/summary/index.html b/chapter_data_structure/summary/index.html index 39b822018..99b329b15 100644 --- a/chapter_data_structure/summary/index.html +++ b/chapter_data_structure/summary/index.html @@ -1035,7 +1035,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_graph/graph/index.html b/chapter_graph/graph/index.html index 42e16c17d..f0298d170 100644 --- a/chapter_graph/graph/index.html +++ b/chapter_graph/graph/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_graph/graph_operations/index.html b/chapter_graph/graph_operations/index.html index 27f885a22..a8f9ae894 100644 --- a/chapter_graph/graph_operations/index.html +++ b/chapter_graph/graph_operations/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_graph/graph_traversal/index.html b/chapter_graph/graph_traversal/index.html index b5202e32a..9c4b2bb66 100644 --- a/chapter_graph/graph_traversal/index.html +++ b/chapter_graph/graph_traversal/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_graph/index.html b/chapter_graph/index.html index bfba1365d..460b7c911 100644 --- a/chapter_graph/index.html +++ b/chapter_graph/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_graph/summary/index.html b/chapter_graph/summary/index.html index f37a3fdae..411e80015 100644 --- a/chapter_graph/summary/index.html +++ b/chapter_graph/summary/index.html @@ -992,7 +992,7 @@
  • - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
  • diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html index f872e3e49..1c5ed031d 100644 --- a/chapter_hashing/hash_collision/index.html +++ b/chapter_hashing/hash_collision/index.html @@ -25,7 +25,7 @@ - 6.2.   哈希冲突 - Hello 算法 + 6.2.   哈希冲突(New) - Hello 算法 @@ -113,7 +113,7 @@
    - 6.2.   哈希冲突 + 6.2.   哈希冲突(New)
    @@ -1003,12 +1003,12 @@ - 6.2.   哈希冲突 + 6.2.   哈希冲突(New) @@ -2139,16 +2139,18 @@

    6.2.   哈希冲突

    -

    在理想情况下,哈希函数应为每个输入生成唯一的输出,实现 key 和 value 的一一对应。然而实际上,向哈希函数输入不同的 key 却产生相同输出的情况是存在的,这种现象被称为「哈希冲突 Hash Collision」。哈希冲突可能导致查询结果错误,从而严重影响哈希表的可用性。

    -

    那么,为何会出现哈希冲突呢?从本质上看,由于哈希函数的输入空间通常远大于输出空间,因此多个输入产生相同输出的情况是不可避免的。例如,若输入空间为全体整数,而输出空间为固定大小的数组,则必然有多个整数映射至同一数组索引。

    -

    为了减轻哈希冲突,一方面,可以通过扩大哈希表容量来降低冲突概率。极端情况下,当输入空间和输出空间大小相等时,哈希表等同于数组,每个 key 都对应唯一的数组索引,可谓“大力出奇迹”。

    -

    另一方面,可以考虑优化哈希表的表示以缓解哈希冲突,常用方法包括「链式地址 Separate Chaining」和「开放寻址 Open Addressing」。

    +

    在理想情况下,哈希函数为每个输入生成唯一的输出,实现 key 和数组索引的一一对应。但实际上,哈希函数的输入空间通常远大于输出空间,因此多个输入产生相同输出的情况是不可避免的。例如,输入空间为全体整数,输出空间为数组容量大小,则必然有多个整数映射至同一数组索引。

    +

    这种多个输入对应同一输出索引的现象被称为「哈希冲突 Hash Collision」。哈希冲突会导致查询结果错误,严重影响哈希表的可用性。哈希冲突的解决方法主要有两种:

    +

    6.2.1.   哈希表扩容

    哈希函数的最后一步通常是对桶数量 \(n\) 取余,作用是将哈希值映射到桶索引范围,从而将 key 放入对应的桶中。当哈希表容量越大(即 \(n\) 越大)时,多个 key 被分配到同一个桶中的概率就越低,冲突就越少。

    因此,当哈希表内的冲突总体较为严重时,编程语言通常通过扩容哈希表来缓解冲突。类似于数组扩容,哈希表扩容需将所有键值对从原哈希表迁移至新哈希表,开销较大。

    -

    编程语言通常使用「负载因子 Load Factor」来衡量哈希冲突的严重程度,定义为哈希表中元素数量除以桶数量,常作为哈希表扩容的触发条件。在 Java 中,当负载因子超过 $ 0.75$ 时,系统会将 HashMap 容量扩展为原先的 \(2\) 倍。

    +

    编程语言通常使用「负载因子 Load Factor」来衡量哈希冲突的严重程度,定义为哈希表中元素数量除以桶数量,常作为哈希表扩容的触发条件。在 Java 中,当负载因子超过 \(0.75\) 时,系统会将 HashMap 容量扩展为原先的 \(2\) 倍。

    6.2.2.   链式地址

    -

    在原始哈希表中,每个桶仅能存储一个键值对。链式地址将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中

    +

    在原始哈希表中,每个桶仅能存储一个键值对。「链式地址 Separate Chaining」将单个元素转换为链表,将键值对作为链表节点,将所有发生冲突的键值对都存储在同一链表中。

    链式地址哈希表

    Fig. 链式地址哈希表

    @@ -2549,7 +2551,7 @@

    为了提高效率,我们可以将链表转换为「AVL 树」或「红黑树」,从而将查询操作的时间复杂度优化至 \(O(\log n)\)

    6.2.3.   开放寻址

    -

    开放寻址法不引入额外的数据结构,而是通过“多次探测”来解决哈希冲突,探测方式主要包括线性探测、平方探测、多次哈希

    +

    「开放寻址 Open Addressing」不引入额外的数据结构,而是通过“多次探测”来解决哈希冲突,探测方式主要包括线性探测、平方探测、多次哈希。

    线性探测

    线性探测采用固定步长的线性查找来解决哈希冲突。