page40.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 88 行

HTML
88
字号
<HTML>
<HEAD>
<TITLE>Analyzing Recursive Functions</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html2388" HREF="page41.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page41.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2386" HREF="page35.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page35.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2380" HREF="page39.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page39.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2390" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2391" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION003150000000000000000">Analyzing Recursive Functions</A></H2>
<P>
In this section we analyze the performance of a
recursive algorithm<A NAME=418>&#160;</A>
which computes the factorial<A NAME=419>&#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="equation420" SRC="img45.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img45.gif"  ><P>
<P>
However, we can also define factorial <em>recursively</em> as follows
<P> <IMG WIDTH=344 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath58275" SRC="img46.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img46.gif"  ><P>
It is this latter definition which leads to the algorithm given
in Program&nbsp;<A HREF="page40.html#progfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#progfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> to compute the factorial of <I>n</I>.
Table&nbsp;<A HREF="page40.html#tblfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#tblfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> gives the running times of each of the
executable statements in Program&nbsp;<A HREF="page40.html#progfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#progfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
<P><A NAME="436">&#160;</A><A NAME="progfactorialc">&#160;</A> <IMG WIDTH=575 HEIGHT=143 ALIGN=BOTTOM ALT="program433" SRC="img47.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img47.gif"  ><BR>
<STRONG>Program:</STRONG> Recursive program to compute <I>n</I>!<BR>
<P>
<P>
<P><A NAME="623">&#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>3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=76 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58289" SRC="img48.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img48.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=76 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58289" SRC="img48.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img48.gif"  > </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 
	    4 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=96 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline58225" SRC="img25.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img25.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD></TR>
<TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 
	    6 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=168 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58295" SRC="img49.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/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=184 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58297" SRC="img50.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img50.gif"  > </TD></TR>
</TBODY>
<CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program&nbsp;<A HREF="page40.html#progfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#progfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A></CAPTION></TABLE>
</P></DIV><P>
<P>
Notice that we had to analyze the running time of the
two possible outcomes of the conditional test on line&nbsp;3 separately.
Clearly, the running time of the program depends on the result
of this test.
<P>
Furthermore, the function <tt>Factorial</tt> calls itself recursively on line&nbsp;6.
Therefore, in order to write down the running time of line&nbsp;6,
we need to know the running time,  <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58299" SRC="img51.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/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 know
what is the function  <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58299" SRC="img51.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img51.gif"  >,
and that we can make use of that function to determine the running time
of line&nbsp;6.
<P>
By summing the columns in Table&nbsp;<A HREF="page40.html#tblfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#tblfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> we get that
the running time of Program&nbsp;<A HREF="page40.html#progfactorialc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page40.html#progfactorialc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> is
<P>
<P><A NAME="eqnmodelrecurrence">&#160;</A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation458" SRC="img52.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img52.gif"  ><P>
where  <IMG WIDTH=176 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58303" SRC="img53.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img53.gif"  > and
 <IMG WIDTH=350 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline58305" SRC="img54.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img54.gif"  >.
This kind of equation is called a
<em>recurrence relation</em><A NAME=464>&#160;</A>
because the function is defined in terms of itself recursively.
<P>
<BR> <HR>
<UL> 
<LI> <A NAME="tex2html2392" HREF="page41.html#SECTION003151000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page41.html#SECTION003151000000000000000">Solving Recurrence Relations-Repeated Substitution</A>
</UL>
<HR><A NAME="tex2html2388" HREF="page41.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page41.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html2386" HREF="page35.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page35.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html2380" HREF="page39.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page39.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html2390" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html2391" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

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