markdown
Recursion Basicsmd 9a7f16a
lecture/information/algorithm/foundation/recursion-basics.lecture.n.md
Download PDF

Recursion Basics

date2026-07-14document_iddoc_b4c88adb5820f7b23bf3e6380392173bdescription再帰算法を、基底条件、再帰段階、停止尺度、正当性、呼出しスタック、計算量の関係から厳密に説明する。prerequisites計算量の基本 / 関数の基本 / 条件分岐type講義statusactiverelateddata/lecture/information/algorithm/algorithms-portal.lecture.n.md / data/lecture/information/algorithm/data-structures/stack-and-queue-basics.lecture.n.md
algorithmfoundationundergraduatelecture
data/lecture/information/algorithm/foundation/computational-complexity-basics.lecture.n.md

1Introduction

This lecture explains a recursive algorithm as a method that constructs the result for an input from the result for a smaller input of the same kind. It defines the base case, recursive step, and termination measure needed for the design, and proves termination and correctness separately. It also explains the execution process through the call stack and analyzes its complexity.

2Components of a Recursive Algorithm

A recursive algorithmRecursive algorithm is an algorithm that applies itself to another input during a computation. Such an application is a recursive callRecursive call.

A base caseBase case is a condition under which the algorithm returns a result directly without making a recursive call. A recursive stepRecursive step transforms an input that does not satisfy a base case into a smaller input of the same kind and constructs the original result from the result of the recursive call.

A termination measureTermination measure assigns a nonnegative integer to every input and strictly decreases in every recursive call. It is used to prove that recursive calls cannot continue indefinitely.

3Recursive Definition of Factorial

For nZ0, factorial is defined by

0!=1,n!=n(n-1)!(n1).

The specification of fact(n) is: given nZ0, return n!. A recursive algorithm satisfying this specification is

text
fact(n):
    if n = 0:
        return 1
    return n * fact(n - 1)

The condition n=0 is the base case, and the computation of nfact(n-1) for n1 is the recursive step. We choose μ(n)=n as the termination measure.

4Termination

If n=0, the algorithm terminates without a recursive call. If n1, the recursive call receives n-1, and

0μ(n-1)=n-1<n=μ(n).

Thus, the nonnegative-integer termination measure strictly decreases in every recursive call. A strictly decreasing sequence of nonnegative integers cannot be infinite, so the computation reaches n=0 after finitely many calls. Therefore, fact(n) terminates for every nZ0.

5Correctness

Termination guarantees that the computation ends, but it does not by itself guarantee that the return value satisfies the specification. We prove the latter claim by induction on n.

  • Base case: for n=0, the algorithm returns 1. Since 0!=1, the return value is correct.
  • Inductive step: let n1 and assume fact(n-1)=(n-1)!. The algorithm returns nfact(n-1). By the inductive hypothesis and the definition of factorial,
nfact(n-1)=n(n-1)!=n!.

Therefore, fact(n)=n! for every nZ0. The base case and recursive step correspond respectively to the base case and inductive step of the proof.

6Call Stack

A stack frameStack frame is a record containing the arguments, local variables, return location, and other information required by one function call. The call stackCall stack stores the stack frames of unfinished calls in last-in, first-out order.

During the evaluation of fact(3), frames for fact(3), fact(2), fact(1), and fact(0) are pushed in that order. After the base case returns 1, the calls return in reverse order and construct 1, 2, and 6. Consequently, the recursive-call depth determines the number of frames stored simultaneously.

7Complexity

We use the unit-cost RAM modelUnit-cost RAM model, in which every arithmetic operation costs Θ(1), and count arithmetic operations. An analysis of arbitrary-precision bit complexity must instead account for the number of bits in n! and the cost of the multiplication algorithm.

In this model, for each input with n>0, fact(n) makes one recursive call and performs a constant-time multiplication. Its time complexity satisfies

T(0)=Θ(1),T(n)=T(n-1)+Θ(1),

so T(n)=Θ(n). The maximum call depth is n+1. Because each frame uses a constant amount of storage, the auxiliary-space complexity of the call stack is Θ(n).

8Design and Verification Procedure

  1. State the specification of the input and return value.
  2. Define a base case that requires no recursive call.
  3. Define a recursive step that constructs the result from a smaller input of the same kind.
  4. Provide a termination measure that strictly decreases in every recursive call, and prove termination.
  5. Prove correctness, for example by induction.
  6. Evaluate time and space complexity from the number of recursive calls and the maximum call depth.
data/lecture/information/algorithm/foundation/sorting-basics.lecture.n.md
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる