page239.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 74 行

HTML
74
字号
<HTML><HEAD><TITLE>Linear Probing</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html3955" HREF="page240.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3953" HREF="page238.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3947" HREF="page238.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3957" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION008610000000000000000">Linear Probing</A></H2><P>The simplest collision resolution strategy in open addressingis called <em>linear probing</em><A NAME=12587>&#160;</A>.In linear probing, the function <I>c</I>(<I>i</I>) is a linear function in <I>i</I>.That is, it is of the form<P> <IMG WIDTH=297 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62389" SRC="img983.gif"  ><P>Property&nbsp;1 requires that <I>c</I>(0)=0.Therefore,  <IMG WIDTH=9 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62399" SRC="img984.gif"  > must be zero.<P>In order for  <IMG WIDTH=62 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62401" SRC="img985.gif"  > to satisfy Property&nbsp;2, <IMG WIDTH=10 HEIGHT=7 ALIGN=BOTTOM ALT="tex2html_wrap_inline62403" SRC="img986.gif"  > and <I>M</I> must be relatively prime.If we know the <I>M</I> will always be a prime number,then any  <IMG WIDTH=10 HEIGHT=7 ALIGN=BOTTOM ALT="tex2html_wrap_inline62403" SRC="img986.gif"  > will do.On the other hand,if we cannot be certain that <I>M</I> is prime,then  <IMG WIDTH=10 HEIGHT=7 ALIGN=BOTTOM ALT="tex2html_wrap_inline62403" SRC="img986.gif"  > must be one.Therefore, linear probing sequence that is usually used is<P> <IMG WIDTH=331 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62390" SRC="img987.gif"  ><P>for  <IMG WIDTH=144 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline62415" SRC="img988.gif"  >.<P>Figure&nbsp;<A HREF="page239.html#fighash3"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates an example ofa scatter table using open addressing together with linear probing.For example, consider the string <tt>&quot;&#229;tta&quot;</tt>.This string hashes to array position  <IMG WIDTH=13 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline62283" SRC="img950.gif"  >.The corresponding linear probing sequence begins at position <IMG WIDTH=13 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline62283" SRC="img950.gif"  > and goes on to positions  <IMG WIDTH=14 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline62285" SRC="img951.gif"  >,  <IMG WIDTH=13 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline62287" SRC="img952.gif"  >,....In this case,the search for the string <tt>&quot;&#229;tta&quot;</tt> succeeds after three probes.<P><P><A NAME="13237">&#160;</A><A NAME="fighash3">&#160;</A> <IMG WIDTH=575 HEIGHT=385 ALIGN=BOTTOM ALT="figure12595" SRC="img989.gif"  ><BR><STRONG>Figure:</STRONG> Scatter table using open addressing and linear probing.<BR><P><P>To insert an item <I>x</I> into the scatter table,an empty cell is found by following the same probe sequence thatwould be used in a search for item <I>x</I>.Thus, linear probing finds an empty cell by doing a linear searchbeginning from array position <I>h</I>(<I>x</I>).<P>An unfortunate characteristic of linear probing arises from the factthat as the table fills,clusters of consecutive cells formand the time required for a search increases with the size of the cluster.Furthermore, when we attempt to insert an item in the tableat a position which is already occupied,that item is ultimately inserted at the end of the cluster--thereby increasing its length.This by itself is not inherently a bad thing.After all, when using the chained approach,every insertion increase the length of some chain by one.However, whenever an insertion is made between two clustersthat are separated by one unoccupied position,the two clusters become one,thereby potentially increasing the cluster length by an amountmuch greater than one--a bad thing!This phenomenon is called <em>primary clustering</em><A NAME=13241>&#160;</A>.<P><HR><A NAME="tex2html3955" HREF="page240.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3953" HREF="page238.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3947" HREF="page238.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3957" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?