page229.html

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

HTML
90
字号
<HTML><HEAD><TITLE>Average Case Analysis</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="tex2html3837" HREF="page230.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3835" HREF="page223.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3831" HREF="page228.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3839" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION008420000000000000000">Average Case Analysis</A></H2><A NAME="sechashingloadfactor">&#160;</A><P>The previous section has shown that in the worst case,the running time to insert an object into a separately chained hash tableis <I>O</I>(1),and the time to find or delete an object is <I>O</I>(<I>n</I>).But these bounds are no better than the same operations on plain lists!Why have we gone to all the trouble inventing hash tables?<P>The answer lies not in the worst-case performance,but in the average expected performance.Suppose we have a hash table of size <I>M</I>.Let there be exactly <I>n</I> items in the hash table.We call the quantity  <IMG WIDTH=64 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62233" SRC="img935.gif"  >the <em>load factor</em><A NAME=11373>&#160;</A><A NAME=11455>&#160;</A><A NAME=11456>&#160;</A>.The load factor is simply the ratioof the number of items in the hash table to the array length.<P>Program&nbsp;<A HREF="page223.html#proghashTablea"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the implementation for<tt>getLoadFactor</tt> method of the <tt>HashTable</tt> class.This method computes  <IMG WIDTH=64 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62233" SRC="img935.gif"  > by accessing the <tt>count</tt> propertyto determine <I>n</I> and by calling the <tt>len</tt> function to determine <I>M</I>.<P>Consider a chained hash table.Let  <IMG WIDTH=14 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline62243" SRC="img937.gif"  > be the number of items in the  <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif"  > linked list,for  <IMG WIDTH=129 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline62247" SRC="img938.gif"  >.The average length of a linked list is<P> <IMG WIDTH=500 HEIGHT=66 ALIGN=BOTTOM ALT="eqnarray11382" SRC="img939.gif"  ><P>The average length of a linked list is exactly the load factor!<P>If we are given the load factor  <IMG WIDTH=8 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline62235" SRC="img936.gif"  >,we can determine the <em>average</em> running times for the various operations.The average running time of <tt>insert</tt>is the same as its worst case time, <I>O</I>(1)--this result does not depend on  <IMG WIDTH=8 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline62235" SRC="img936.gif"  >.On the other hand,the average running time for <tt>withdraw</tt> does depend on  <IMG WIDTH=8 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline62235" SRC="img936.gif"  >.It is  <IMG WIDTH=153 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62257" SRC="img940.gif"  > since the time required to delete an itemfrom a linked list of length  <IMG WIDTH=8 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline62235" SRC="img936.gif"  > is  <IMG WIDTH=32 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62261" SRC="img941.gif"  >.<P>To determine the average running time for the <tt>find</tt> operation,we need to make an assumption about whether the item that is being soughtis in the table.If the item is not found in the table,the search is said to be <em>unsuccessful</em>.The average running time for an unsuccessful search is<P> <IMG WIDTH=370 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62219" SRC="img942.gif"  ><P>On the other hand, if the search target is in the table,the search is said to be <em>successful</em>.The average number of comparisons needed to find an arbitrary itemin a linked list of length  <IMG WIDTH=8 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline62235" SRC="img936.gif"  > is<P> <IMG WIDTH=305 HEIGHT=47 ALIGN=BOTTOM ALT="displaymath62220" SRC="img943.gif"  ><P>Thus, the average running time for a successful search is<P> <IMG WIDTH=404 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62221" SRC="img944.gif"  ><P><P>So, while any one search operation can be as bad as <I>O</I>(<I>n</I>),if we do a large number of random searches,we expect that the average running time will be  <IMG WIDTH=32 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62261" SRC="img941.gif"  >.In fact, if we have a sufficiently good hash functionand a reasonable set of objects in the container,we can expect that those objects are distributed throughout the table.Therefore, any one search operationwill not be very much worse than the worst case.<P>Finally, if we know how many objects will be inserted into thehash table <em>a priori</em>,then we can choose a table size <I>M</I> which is larger thanthe maximum number of items expected.By doing this, we can ensure that  <IMG WIDTH=92 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62271" SRC="img945.gif"  >.That is, a linked list contains no more than one item on average.In this case, the average time for <tt>withdraw</tt> is  <IMG WIDTH=100 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62203" SRC="img930.gif"  >and for <tt>find</tt> it is  <IMG WIDTH=173 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline62275" SRC="img946.gif"  >.<P><HR><A NAME="tex2html3837" HREF="page230.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3835" HREF="page223.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3831" HREF="page228.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3839" 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 + -
显示快捷键?