page459.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 64 行
HTML
64 行
<HTML><HEAD><TITLE>Example-Generalized 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="tex2html6457" HREF="page460.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6455" HREF="page458.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6449" HREF="page458.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6459" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0014410000000000000000">Example-Generalized Fibonacci Numbers</A></H2><P>Consider the problem of computing the<em>generalized Fibonacci numbers</em><A NAME=33095> </A><A NAME=33096> </A>.The generalized Fibonacci numbers of order <IMG WIDTH=36 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60375" SRC="img576.gif" > are given by<P><A NAME="eqnalgsgenfib"> </A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation33097" SRC="img1829.gif" ><P>Notice that the ``normal'' Fibonacci numbers considered in Section <A HREF="page75.html#secfibonacci"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>are the same as the generalized Fibonacci numbers of order 2.<P>If we write a recursive methodthat implements directly Equation <A HREF="page459.html#eqnalgsgenfib"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,we get an algorithm with exponential running time.For example, in Section <A HREF="page75.html#secfibonacci"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> it is shown thatthe time to compute the second-order Fibonacci numbersis <IMG WIDTH=122 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline67913" SRC="img1764.gif" >.<P>The problem with the direct recursive implementation is thatit does far more work than is needed because it solves the same subproblem many times.For example,to compute <IMG WIDTH=28 HEIGHT=34 ALIGN=MIDDLE ALT="tex2html_wrap_inline68187" SRC="img1830.gif" > it is necessary to computeboth <IMG WIDTH=28 HEIGHT=34 ALIGN=MIDDLE ALT="tex2html_wrap_inline68189" SRC="img1831.gif" > and <IMG WIDTH=28 HEIGHT=34 ALIGN=MIDDLE ALT="tex2html_wrap_inline68191" SRC="img1832.gif" >.However, in computing <IMG WIDTH=28 HEIGHT=34 ALIGN=MIDDLE ALT="tex2html_wrap_inline68189" SRC="img1831.gif" >it is also necessary to compute <IMG WIDTH=28 HEIGHT=34 ALIGN=MIDDLE ALT="tex2html_wrap_inline68191" SRC="img1832.gif" >, and so on.<P>An alternative to the top-down recursive implementationis to do the calculation from the bottom up.In order to do this we compute the series of sequences<P> <IMG WIDTH=500 HEIGHT=107 ALIGN=BOTTOM ALT="eqnarray33115" SRC="img1833.gif" ><P>Notice that we can compute <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68197" SRC="img1834.gif" > from the information containedin <IMG WIDTH=13 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline67035" SRC="img1616.gif" > simply by using Equation <A HREF="page459.html#eqnalgsgenfib"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P>Program <A HREF="page459.html#progexampler"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the method <tt>Fibonacci</tt>which takes two integer arguments <I>n</I> and <I>k</I>and computes the <IMG WIDTH=21 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57913" SRC="img94.gif" > Fibonacci number of order <I>k</I>using the approach described above.This algorithm uses an array to representthe series of sequences <IMG WIDTH=94 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68209" SRC="img1835.gif" >.As each subsequent Fibonacci number is computedit is added to the end of the array.<P><P><A NAME="33131"> </A><A NAME="progexampler"> </A> <IMG WIDTH=575 HEIGHT=315 ALIGN=BOTTOM ALT="program33128" SRC="img1836.gif" ><BR><STRONG>Program:</STRONG> Dynamic programming example--computing generalized Fibonacci numbers.<BR><P><P>The worst-case running time of the <tt>Fibonacci</tt> methodgiven in Program <A HREF="page459.html#progexampler"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is a function of both <I>n</I> and <I>k</I>:<P> <IMG WIDTH=358 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath68179" SRC="img1837.gif" ><P><HR><A NAME="tex2html6457" HREF="page460.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6455" HREF="page458.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6449" HREF="page458.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6459" 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 + -
显示快捷键?