📄 page466.html
字号:
<HTML>
<HEAD>
<TITLE>Example-Generalized Fibonacci Numbers</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="tex2html7679" HREF="page467.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page467.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="tex2html7677" HREF="page465.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page465.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="tex2html7671" HREF="page465.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page465.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="tex2html7681" 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="tex2html7682" 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="SECTION0015410000000000000000">Example-Generalized Fibonacci Numbers</A></H2>
<P>
Consider the problem of computing the
<em>generalized Fibonacci numbers</em><A NAME=33284> </A><A NAME=33285> </A>.
The generalized Fibonacci numbers of order <IMG WIDTH=36 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline60907" SRC="img581.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img581.gif" > are given by
<P><A NAME="eqnalgsgenfib"> </A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation33286" SRC="img1938.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1938.gif" ><P>
Notice that the ``normal'' Fibonacci numbers considered in Section <A HREF="page73.html#secfibonacci" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page73.html#secfibonacci"><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>
are the same as the generalized Fibonacci numbers of order 2.
<P>
If we write a recursive function
that implements directly Equation <A HREF="page466.html#eqnalgsgenfib" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page466.html#eqnalgsgenfib"><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 an algorithm with exponential running time.
For example, in Section <A HREF="page73.html#secfibonacci" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page73.html#secfibonacci"><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> it is shown that
the time to compute the second-order Fibonacci numbers
is <IMG WIDTH=123 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68703" SRC="img1873.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1873.gif" >.
<P>
The problem with the direct recursive implementation is that
it does far more work than is needed because
it solves the same subproblem many times.
For example,
to compute <IMG WIDTH=28 HEIGHT=32 ALIGN=MIDDLE ALT="tex2html_wrap_inline68977" SRC="img1939.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1939.gif" > it is necessary to compute
both <IMG WIDTH=27 HEIGHT=32 ALIGN=MIDDLE ALT="tex2html_wrap_inline68979" SRC="img1940.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1940.gif" > and <IMG WIDTH=28 HEIGHT=32 ALIGN=MIDDLE ALT="tex2html_wrap_inline68981" SRC="img1941.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1941.gif" >.
However, in computing <IMG WIDTH=27 HEIGHT=32 ALIGN=MIDDLE ALT="tex2html_wrap_inline68979" SRC="img1940.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1940.gif" >
it is also necessary to compute <IMG WIDTH=28 HEIGHT=32 ALIGN=MIDDLE ALT="tex2html_wrap_inline68981" SRC="img1941.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1941.gif" >, and so on.
<P>
An alternative to the top-down recursive implementation
is to do the calculation from the bottom up.
In order to do this we compute the series of sequences
<P> <IMG WIDTH=500 HEIGHT=106 ALIGN=BOTTOM ALT="eqnarray33304" SRC="img1942.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1942.gif" ><P>
Notice that we can compute <IMG WIDTH=29 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline68987" SRC="img1943.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1943.gif" > from the information contained
in <IMG WIDTH=12 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline67646" SRC="img1662.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1662.gif" > simply by using Equation <A HREF="page466.html#eqnalgsgenfib" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page466.html#eqnalgsgenfib"><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>
Program <A HREF="page466.html#progalgs4c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page466.html#progalgs4c"><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> defines the function <tt>Fibonacci</tt>
which takes two integer arguments <I>n</I> and <I>k</I>
and computes the <IMG WIDTH=21 HEIGHT=13 ALIGN=BOTTOM ALT="tex2html_wrap_inline58453" SRC="img94.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img94.gif" > Fibonacci number of order <I>k</I>
using the approach described above.
This algorithm uses an array to represent
the series of sequences <IMG WIDTH=94 HEIGHT=22 ALIGN=MIDDLE ALT="tex2html_wrap_inline68999" SRC="img1944.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1944.gif" >.
As each subsequent Fibonacci number is computed
it is added to the end of the array.
<P>
<P><A NAME="33320"> </A><A NAME="progalgs4c"> </A> <IMG WIDTH=575 HEIGHT=429 ALIGN=BOTTOM ALT="program33317" SRC="img1945.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1945.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> routine
given in Program <A HREF="page466.html#progalgs4c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page466.html#progalgs4c"><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 a function of both <I>n</I> and <I>k</I>:
<P> <IMG WIDTH=357 HEIGHT=48 ALIGN=BOTTOM ALT="displaymath68969" SRC="img1946.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1946.gif" ><P><HR><A NAME="tex2html7679" HREF="page467.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page467.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="tex2html7677" HREF="page465.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page465.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="tex2html7671" HREF="page465.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page465.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="tex2html7681" 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="tex2html7682" 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 © 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -