page42.html

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

HTML
88
字号
<HTML><HEAD><TITLE>Analyzing Recursive Methods</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="tex2html1692" HREF="page43.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html1690" HREF="page37.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html1684" HREF="page41.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html1694" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION002150000000000000000">Analyzing Recursive Methods</A></H2><P>In this section we analyze the performance of arecursive algorithm<A NAME=437>&#160;</A>which computes the factorial<A NAME=438>&#160;</A> of a number.Recall that the factorial of a non-negative integer <I>n</I>, written <I>n</I>!,is defined as<P><A NAME="eqnmodelfactorial">&#160;</A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation439" SRC="img45.gif"  ><P><P>However, we can also define factorial <em>recursively</em> as follows<P> <IMG WIDTH=344 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath57733" SRC="img46.gif"  ><P>It is this latter definition which leads to the algorithm givenin Program&nbsp;<A HREF="page42.html#progexamplec"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> to compute the factorial of <I>n</I>.Table&nbsp;<A HREF="page42.html#tblfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the running times of each of theexecutable statements in Program&nbsp;<A HREF="page42.html#progexamplec"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="455">&#160;</A><A NAME="progexamplec">&#160;</A> <IMG WIDTH=575 HEIGHT=105 ALIGN=BOTTOM ALT="program452" SRC="img47.gif"  ><BR><STRONG>Program:</STRONG> Recursive program to compute <I>n</I>!.<BR><P><P><P><A NAME="654">&#160;</A><P>    <A NAME="tblfactorialc">&#160;</A>    <DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=3 BORDER FRAME=HSIDES RULES=GROUPS><COL ALIGN=CENTER><COL ALIGN=CENTER><COL ALIGN=CENTER><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>	    </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP COLSPAN=2> time</TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP><P>	    statement </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I>=0 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I><I>&gt;</I>0 </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>2 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=77 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline57747" SRC="img48.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=77 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline57747" SRC="img48.gif"  > </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=96 HEIGHT=18 ALIGN=MIDDLE ALT="tex2html_wrap_inline57683" SRC="img25.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=169 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline57753" SRC="img49.gif"  > </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=183 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline57755" SRC="img50.gif"  > </TD></TR></TBODY><CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program&nbsp;<A HREF="page42.html#progexamplec"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.</CAPTION></TABLE></P></DIV><P><P>Notice that we had to analyze the running time of thetwo possible outcomes of the conditional test on line&nbsp;2 separately.Clearly, the running time of the program depends on the resultof this test.<P>Furthermore, the method <tt>factorial</tt> calls itself recursively on line&nbsp;5.Therefore, in order to write down the running time of line&nbsp;5,we need to know the running time,  <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline57757" SRC="img51.gif"  >, of <tt>factorial</tt>.But this is precisely what we are trying to determine in the first place!We escape from this catch-22 by assuming that we already knowwhat is the function  <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline57757" SRC="img51.gif"  >,and that we can make use of that function to determine the running timeof line&nbsp;5.<P>By summing the columns in Table&nbsp;<A HREF="page42.html#tblfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we get thatthe running time of Program&nbsp;<A HREF="page42.html#progexamplec"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is<P><P><A NAME="eqnmodelrecurrence">&#160;</A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation477" SRC="img52.gif"  ><P>where  <IMG WIDTH=176 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline57761" SRC="img53.gif"  > and <IMG WIDTH=350 HEIGHT=21 ALIGN=MIDDLE ALT="tex2html_wrap_inline57763" SRC="img54.gif"  >.This kind of equation is called a<em>recurrence relation</em><A NAME=483>&#160;</A>because the function is defined in terms of itself recursively.<P><BR> <HR><UL> <LI> <A NAME="tex2html1695" HREF="page43.html#SECTION002151000000000000000">Solving Recurrence Relations-Repeated Substitution</A></UL><HR><A NAME="tex2html1692" HREF="page43.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html1690" HREF="page37.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html1684" HREF="page41.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html1694" 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 + -
显示快捷键?