page409.html

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

HTML
78
字号
<HTML><HEAD><TITLE>Collapsing Find</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="tex2html5891" HREF="page410.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5889" HREF="page403.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5883" HREF="page408.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5893" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0012420000000000000000">Collapsing Find</A></H2><A NAME="secsetsfind">&#160;</A><P>Unfortunately, using the <tt>join</tt> algorithmgiven in Program&nbsp;<A HREF="page408.html#progpartitionAsForestd"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>can result in particularly bad trees.For example, Figure&nbsp;<A HREF="page409.html#figsets4"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows the worst possible tree that can be obtained.Such a tree is bad because its height is <I>O</I>(<I>N</I>).In such a tree both the worst case and the average case running timefor the <tt>find</tt> operation is <I>O</I>(<I>N</I>).<P><P><A NAME="29293">&#160;</A><A NAME="figsets4">&#160;</A> <IMG WIDTH=575 HEIGHT=271 ALIGN=BOTTOM ALT="figure29100" SRC="img1634.gif"  ><BR><STRONG>Figure:</STRONG> A degenerate tree.<BR><P><P>There is an interesting trick we can playthat can improve matters significantly.Recall that the find operation starts from a given nodeand locates the root of the tree containing that node.If, having found the root,we replace the parent of the given node with the root,the next time we do a <tt>find</tt> it will be more efficient.<P>In fact, we can go one step further and replace the parentof every node along the search path to the root.This is calleda <em>collapsing find</em><A NAME=29298>&#160;</A><A NAME=29299>&#160;</A>operation.Doing so does not change the asymptotic complexityof the <tt>find</tt> operation.However, a subsequent <tt>find</tt> operation which begins at any pointalong the search path to the root will run in constant time!<P>Program&nbsp;<A HREF="page409.html#progpartitionAsForestV2a"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the code fora collapsing version of the <tt>find</tt> operation.The <tt>find</tt> method first determines the root node as before.Then, a second pass is made up the chain from the initial node to the root,during which the parent of each node is assigned the root.Clearly, this version of <tt>find</tt> is slower than theone given in Program&nbsp;<A HREF="page408.html#progpartitionAsForestc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> because it makes two passesup the chain rather than one.However, the running of this version of <tt>find</tt> is still <I>O</I>(<I>d</I>),where <I>d</I> is the depth of the node from which the search begins.<P><P><A NAME="29332">&#160;</A><A NAME="progpartitionAsForestV2a">&#160;</A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program29308" SRC="img1635.gif"  ><BR><STRONG>Program:</STRONG> <tt>PartitionAsForest</tt> class collapsing <tt>find</tt> method.<BR><P><P>Figure&nbsp;<A HREF="page409.html#figsets5"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the effect of a collapsing find operation.After the find,all the nodes along the search path are attached directly to the root.That is, they have had their depths decreased to one.As a side-effect, any node which is in the subtree of a node along the searchpath may have its depth decreased by the collapsing find operation.The depth of a node is never increased by the find operation.Eventually, if we do enough collapsing find operations,it is possible to obtain a tree of height onein which all the non-root nodes point directly at the root.<P><P><A NAME="29460">&#160;</A><A NAME="figsets5">&#160;</A> <IMG WIDTH=575 HEIGHT=181 ALIGN=BOTTOM ALT="figure29318" SRC="img1636.gif"  ><BR><STRONG>Figure:</STRONG> Example of collapsing find.<BR><P><HR><A NAME="tex2html5891" HREF="page410.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5889" HREF="page403.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5883" HREF="page408.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5893" 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 + -
显示快捷键?