递归算法及归纳法.pptVIP

  • 20
  • 0
  • 约8.72千字
  • 约 48页
  • 2018-11-18 发布于天津
  • 举报
递归算法及归纳法

递归算法及归纳法 3.1 简介 递归算法是由Prof. John McCarthy (麻省理工学院→Stanford大学) 提出来的,1970得到应用,那时是出算法的高峰期。今天,几年所有的通用语言都支持递归。 递归和归纳法之间的关系非常密切 它们以及与它们相关的证明可互相代替。 3.2 递归例程 3.2.1 Activation Frame and Recursive Procedure Calls The basic unit of storage for an individual procedure invocation at run time is called an activation frame. It provide storage space for the procedure’s local variables,actual parameters, and compiler “temporary variables” ,including the return value, if the procedure returns a value. It also provides storage space for other bookkeeping (簿记)needs,such as return address, which tells what instruction the program should execute after this procedure exits. Thus it provides a “frame of reference ” in which the procedure executes for this invocation only. The compiler generates code to allocation space in a region of storage called the frame stack as part of the code that implements a procedure call.This space is referenced by a special register called the frame pointer, so that as this procedure invocation executes, it knows where its local variables, input parameters, and return value are stored. Each procedure invocation that is active has a unique activation frame. A procedure invocation is active from the time it is entered until it exits. If recursion occurs, all invocations of the recursive procedure that are active simultaneously have distinct frames. As a procedure invocation (recursive or not)exits, its activation frame is automatically de-allocated so that the space can be used by some future function invocation. A hand execution of code that depicts the states of activation frames is called an activation trace. Fibonacci function int fib(int n) { int f,f1,f2; if (n2) f=n; else { f1=fib(n-1); f2=fib(n-2); f=f1+f2; } return f; } FIBONACCI 递归程程序,其执行(包括激活等)过程见书PP104页。 Lemma3.1 In a computation with out while or for loops, but possibly with recursive procedure ca

文档评论(0)

1亿VIP精品文档

相关文档