page450.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 61 行
HTML
61 行
<HTML><HEAD><TITLE>Example-Computing Fibonacci Numbers</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="tex2html6355" HREF="page451.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6353" HREF="page448.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6347" HREF="page449.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6357" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0014320000000000000000">Example-Computing Fibonacci Numbers</A></H2><A NAME="secalgsfibonacci"> </A><P>The Fibonacci numbers<A NAME=32706> </A>are given by following recurrence<P><A NAME="eqnalgsfib"> </A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation32707" SRC="img1763.gif" ><P>Section <A HREF="page75.html#secfibonacci"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> presents a recursive method to computethe Fibonacci numbers by implementing directly Equation <A HREF="page450.html#eqnalgsfib"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.(See Program <A HREF="page75.html#progexamplem"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).The running time of that program is shown to be <IMG WIDTH=122 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67913" SRC="img1764.gif" >.<P>In this section we present a divide-and-conquer styleof algorithm for computing Fibonacci numbers.We make use of the following identities<P> <IMG WIDTH=171 HEIGHT=44 ALIGN=BOTTOM ALT="gather32717" SRC="img1765.gif" ><P>for <IMG WIDTH=37 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59027" SRC="img353.gif" >.(See Exercise <A HREF="page79.html#exerciseasymptoticfib"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).Thus, we can rewrite Equation <A HREF="page450.html#eqnalgsfib"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> as<P><A NAME="eqnalgsfibx"> </A> <IMG WIDTH=500 HEIGHT=88 ALIGN=BOTTOM ALT="equation32725" SRC="img1766.gif" ><P><P>Program <A HREF="page450.html#progexamplep"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the method <tt>Fibonacci</tt> whichimplements directly Equation <A HREF="page450.html#eqnalgsfibx"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Given <I>n</I><I>></I>1 it computes <IMG WIDTH=18 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline59871" SRC="img470.gif" > by calling itself recursivelyto compute <IMG WIDTH=41 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline67925" SRC="img1767.gif" > and <IMG WIDTH=57 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline67927" SRC="img1768.gif" >and then combines the two results as required.<P><P><A NAME="32745"> </A><A NAME="progexamplep"> </A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program32742" SRC="img1769.gif" ><BR><STRONG>Program:</STRONG> Divide-and-conquer Example--computing Fibonacci numbers.<BR><P><P>To determine a bound on the running time of the <tt>Fibonacci</tt> methodin Program <A HREF="page450.html#progexamplep"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we assume that <I>T</I>(<I>n</I>) is a non-decreasing function.That is, <IMG WIDTH=114 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67931" SRC="img1770.gif" > for all <IMG WIDTH=38 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58983" SRC="img341.gif" >.Therefore <IMG WIDTH=175 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67935" SRC="img1771.gif" >.Although the program works correctly for all values of <I>n</I>,it is convenient to assume that <I>n</I> is a power of 2.In this case, the running time of the method is upper-bounded by <I>T</I>(<I>n</I>)where<P><A NAME="eqnalgsfibtn"> </A> <IMG WIDTH=500 HEIGHT=48 ALIGN=BOTTOM ALT="equation32751" SRC="img1772.gif" ><P><P>Equation <A HREF="page450.html#eqnalgsfibtn"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is easily solved using repeated substitution:<P> <IMG WIDTH=500 HEIGHT=209 ALIGN=BOTTOM ALT="eqnarray32757" SRC="img1773.gif" ><P>Thus, <I>T</I>(<I>n</I>)=2<I>n</I>-1=<I>O</I>(<I>n</I>).<P><HR><A NAME="tex2html6355" HREF="page451.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6353" HREF="page448.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6347" HREF="page449.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6357" 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 + -
显示快捷键?