page503.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 65 行
HTML
65 行
<HTML><HEAD><TITLE>Running Time 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="tex2html6952" HREF="page504.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6950" HREF="page502.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6944" HREF="page502.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6954" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0015531000000000000000">Running Time Analysis</A></H3><P>The <tt>buildHeap</tt> method does exactly <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58273" SRC="img177.gif" ><tt>percolateDown</tt> operations.As discussed above, the running time for <tt>percolateDown</tt> is <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline69571" SRC="img2075.gif" >,where <IMG WIDTH=12 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline62361" SRC="img976.gif" > is the height in the tree of the node at array position <I>i</I>.The highest node in the tree is the rootand its height is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.If we make the simplifying assumption that the running time for<tt>percolateDown</tt> is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" > for every value of <I>i</I>,we get that the total running time for <tt>buildHeap</tt> is <IMG WIDTH=68 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59353" SRC="img402.gif" >.<P>However, <IMG WIDTH=45 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60271" SRC="img553.gif" > is not a tight bound.The maximum number of iterations of the <tt>percolateDown</tt>loop done during the entire process of building the heapis equal to the sum of the heights of all of the nodes in the tree!The following theorem shows that this is <I>O</I>(<I>n</I>).<P><BLOCKQUOTE> <b>Theorem</b><A NAME="theoremsortingii"> </A>Consider a <em>perfect</em> binary tree <I>T</I> of height <I>h</I>having <IMG WIDTH=90 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline65527" SRC="img1379.gif" > nodes.The sum of the heights of the nodes in <I>T</I>is <IMG WIDTH=253 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline69597" SRC="img2076.gif" >.</BLOCKQUOTE><P> extbfProofA perfect binary tree has 1 node at height <I>h</I>,2 nodes at height <I>h</I>-1, 4 nodes at height <I>h</I>-2 and so on.In general, there are <IMG WIDTH=12 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline69605" SRC="img2077.gif" > nodes at height <I>h</I>-<I>i</I>.Therefore, the sum of the heights of the nodes is <IMG WIDTH=96 HEIGHT=31 ALIGN=MIDDLE ALT="tex2html_wrap_inline69609" SRC="img2078.gif" >.<P>The summation can be solved as follows:First, we make the simple variable substitution <I>i</I>=<I>j</I>-1:<P><A NAME="eqnsortingviii"> </A> <IMG WIDTH=500 HEIGHT=282 ALIGN=BOTTOM ALT="eqnarray40854" SRC="img2079.gif" ><P>Note that the summation which appears on the right hand sideis identical to that on the left.Rearranging Equation <A HREF="page503.html#eqnsortingviii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> and simplifying gives:<P> <IMG WIDTH=500 HEIGHT=71 ALIGN=BOTTOM ALT="eqnarray40882" SRC="img2080.gif" ><P><P>It follows directly from Theorem <A HREF="page503.html#theoremsortingii"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>that the sum of the heights of a perfect binary tree is <I>O</I>(<I>n</I>).But a heap is not a <em>perfect</em> tree--it is a <em>complete</em> tree.Nevertheless, it is easy to show that the same bound applies toa complete tree.The proof is left as an exercise for the reader (Exercise <A HREF="page517.html#exercisesortingcomplete"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).Therefore, the running time for the <tt>buildHeap</tt> method is <I>O</I>(<I>n</I>),were <I>n</I> is the length of the array to be heapified.<P><HR><A NAME="tex2html6952" HREF="page504.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6950" HREF="page502.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6944" HREF="page502.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6954" 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 © 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 + -
显示快捷键?