mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 14:03:50 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -1950,8 +1950,8 @@
|
||||
<h1 id="63-hash-algorithms">6.3 Hash algorithms<a class="headerlink" href="#63-hash-algorithms" title="Permanent link">¶</a></h1>
|
||||
<p>The previous two sections explained how hash tables work and how collisions are resolved. Whether we use open addressing or separate chaining, <strong>these techniques keep the table working when collisions occur, but do not reduce how often collisions happen</strong>.</p>
|
||||
<p>Frequent collisions can severely degrade performance. In the separate-chaining table shown below, the ideal case distributes pairs evenly among buckets for the best lookup efficiency. In the worst case, all pairs occupy the same bucket, degrading the time complexity to <span class="arithmatex">\(O(n)\)</span>.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.png" data-dark-src="/assets/figure-svg/figures/1c042a764328a0e2a3146fde.json" data-src="/assets/figure-svg/figures/c0e3e4d6caf2c8cfdfe62298.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Best and worst cases for hash collisions</object><object data="/en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Best and worst cases for hash collisions</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 6-8 Best and worst cases for hash collisions </p>
|
||||
<p><ha-svg-figure aria-label="Best-case distribution vs. worst-case degradation in a separate-chaining hash table: Uniform O(1) distribution vs. single-bucket O(n) degradation for six key-value pairs" class="animation-figure" data-book-key="en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.png" data-dark-src="/assets/figure-svg/figures/1c042a764328a0e2a3146fde.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c0e3e4d6caf2c8cfdfe62298.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Best and worst cases for hash collisions</object><object aria-hidden="true" data="/en/chapter_hashing/hash_algorithm.assets/hash_collision_best_worst_condition.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Best and worst cases for hash collisions</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 6-8 Best and worst cases for hash collisions </p><div class="ha-sr-only figure-description"><h2>Description of Figure 6-8: Best-case distribution vs. worst-case degradation in a separate-chaining hash table</h2><p>For the same six key-value pairs, the number of comparisons needed for lookup differs depending on whether they are distributed evenly or concentrated in a single bucket.</p><p>Under the top-left heading "Hash table with chaining", two light green panels stacked vertically show the best case and worst case for the same six key-value pairs (k₁, s₁) through (k₆, s₆).</p><p>On the left of each panel, a light blue vertical column represents the bucket array (containing the head node of each non-empty bucket's linked list and an ellipsis "…" for the remaining buckets), while horizontal arrows to the right connect the nodes of each bucket's singly linked list.</p><p>The upper panel shows "Best case: Key-value pairs are evenly distributed across buckets; Time complexity: O(1)". The six key-value pairs are distributed evenly across three drawn buckets: the first bucket chains (k₁, s₁) → (k₂, s₂), the second bucket chains (k₃, s₃) → (k₄, s₄), and the third bucket chains (k₅, s₅) → (k₆, s₆). Every bucket's linked list is short (only 2 nodes), so after locating the bucket, a lookup finishes within a constant number of comparisons. As the input size grows, achieving O(1) lookup also requires maintaining a suitable load factor so that the average chain length stays constant; uniform distribution alone does not unconditionally guarantee this complexity.</p><p>The lower panel shows "Worst case: All key-value pairs are in one bucket; Time complexity: O(n)". All six key-value pairs map to the first bucket and form a single 6-node singly linked list, (k₁, s₁) → (k₂, s₂) → (k₃, s₃) → (k₄, s₄) → (k₅, s₅) → (k₆, s₆), while all remaining buckets below are empty (indicated by an ellipsis). Here the hash table degenerates completely into a singly linked list, and looking up the last element requires traversing all n nodes.</p><p>Tracing a lookup for k₆ in the figure: in the upper panel, once the third bucket is located, only two entries—k₅ and k₆—need to be checked; in the lower panel, the search must traverse from k₁ to the tail of the list, checking 6 entries in total.</p><p>A hash algorithm should distribute keys as evenly as possible, paired with an appropriate load factor to bound chain lengths and avoid concentrating all keys in a single bucket.</p></div>
|
||||
<p><strong>The hash function determines how key-value pairs are distributed</strong>. Recall its two steps: calculate a hash value, then take it modulo the array length:</p>
|
||||
<div class="highlight"><pre><span></span><code><a href="#__codelineno-0-1" id="__codelineno-0-1" name="__codelineno-0-1"></a><span class="nv">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span>hash<span class="o">(</span>key<span class="o">)</span><span class="w"> </span>%<span class="w"> </span>capacity
|
||||
</code></pre></div>
|
||||
@@ -3042,5 +3042,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user