markdown
再帰の基本md 9a7f16a
lecture/information/algorithm/foundation/recursion-basics.lecture.n.md
Download PDF

再帰さいき基本きほん

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

1導入どうにゅう

この講義こうぎでは、再帰算法さいきさんぽうを、同種どうしゅちいさい入力にゅうりょくたいする結果けっかからもと入力にゅうりょくたいする結果けっか構成こうせいする方法ほうほうとして説明せつめいする。設計せっけい必要ひつよう基底条件きていじょうけん再帰段階さいきだんかい停止尺度ていししゃくど定義ていぎし、停止性ていしせい正当性せいとうせい区別くべつして証明しょうめいする。さらに、呼出よびだしスタックによる実行過程じっこうかてい計算量けいさんりょう解説かいせつする。

2再帰算法さいきさんぽう構成要素こうせいようそ

再帰算法さいきさんぽうRecursive algorithmとは、ある入力にゅうりょくたいする計算けいさん途中とちゅうで、同一どういつ算法さんぽうべつ入力にゅうりょく適用てきようする算法さんぽうである。この適用てきよう再帰呼出さいきよびだRecursive callという。

基底条件きていじょうけんBase caseとは、再帰呼出さいきよびだしを実行じっこうせずに結果けっか直接返ちょくせつかえ条件じょうけんである。再帰段階さいきだんかいRecursive stepとは、基底条件きていじょうけんたさない入力にゅうりょくちいさい同種どうしゅ入力にゅうりょく変換へんかんし、その再帰呼出さいきよびだしの結果けっかからもと結果けっか構成こうせいする規則きそくである。

停止尺度ていししゃくどTermination measureとは、各入力かくにゅうりょく非負整数ひふせいすう対応たいおうさせ、各再帰呼出かくさいきよびだしで厳密げんみつ減少げんしょうするりょうである。このりょう再帰呼出さいきよびだしが無限むげん継続けいぞくしないことを証明しょうめいするために使用しようする。

3階乗かいじょう再帰的定義さいきてきていぎ

nZ0たいする階乗かいじょうつぎ定義ていぎする。

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

関数かんすう fact(n)仕様しようを「nZ0り、n!かえす」とする。この仕様しようたいする再帰算法さいきさんぽうつぎのように記述きじゅつできる。

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

n=0基底条件きていじょうけんであり、n1 における nfact(n-1)計算けいさん再帰段階さいきだんかいである。停止尺度ていししゃくどには μ(n)=n採用さいようする。

4停止性ていしせい

n=0 なら再帰呼出さいきよびだしを実行じっこうせずに終了しゅうりょうする。n1 なら、再帰呼出さいきよびだしの入力にゅうりょくn-1 であり、

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

成立せいりつする。したがって、非負整数ひふせいすうである停止尺度ていししゃくど各再帰呼出かくさいきよびだしで厳密げんみつ減少げんしょうする。非負整数ひふせいすう厳密減少列げんみつげんしょうれつ無限むげん継続けいぞくしないため、有限回ゆうげんかい呼出よびだしのあとn=0到達とうたつする。よって fact(n) はすべての nZ0停止ていしする。

5正当性せいとうせい

停止性ていしせいは「計算けいさん終了しゅうりょうする」ことを保証ほしょうするが、「返値かえりち仕様しようたす」ことはべつ証明しょうめいする必要ひつようがある。nかんする数学的帰納法すうがくてききのうほうもちいる。

  • 基底段階きていだんかいn=0 では、算法さんぽう1かえす。0!=1 であるため、返値かえりちただしい。
  • 帰納段階きのうだんかいn1 とし、fact(n-1)=(n-1)!帰納法きのうほう仮定かていとする。算法さんぽう返値かえりちnfact(n-1) であるから、仮定かてい階乗かいじょう定義ていぎにより
nfact(n-1)=n(n-1)!=n!

となる。

したがって、すべての nZ0 について fact(n)=n!成立せいりつする。基底条件きていじょうけん再帰段階さいきだんかいは、それぞれ帰納法きのうほう基底段階きていだんかい帰納段階きのうだんかい対応たいおうする。

6呼出よびだしスタック

呼出よびだしフレームStack frameとは、一回いっかい関数呼出かんすうよびだしに必要ひつよう引数ひきすう局所変数きょくしょへんすう復帰位置ふっきいちなどを保持ほじする記録きろくである。呼出よびだしスタックCall stackとは、未完了みかんりょう呼出よびだしフレームを後入先出あといれさきだしの順序じゅんじょ保持ほじするスタックである。

fact(3)実行じっこうでは、fact(3)fact(2)fact(1)fact(0)じゅんにフレームがまれる。基底条件きていじょうけん1かえしたあと逆順ぎゃくじゅん復帰ふっきして 126構成こうせいする。したがって、再帰呼出さいきよびだしのふかさは同時どうじ保持ほじするフレームすう決定けっていする。

7計算量けいさんりょう

ここでは、各算術演算かくさんじゅつえんざん費用ひようΘ(1) とする単位費用たんいひよう RAM モデルUnit-cost RAM model採用さいようし、算術演算回数さんじゅつえんざんかいすう評価ひょうかする。任意精度整数にんいせいどせいすうのビット演算量えんざんりょう評価ひょうかする場合ばあいn!桁数けたすう乗算算法じょうざんさんぽう費用ひよう別途考慮べっとこうりょする必要ひつようがある。

このモデルでは、fact(n)n>0各入力かくにゅうりょくについて一回いっかい再帰呼出さいきよびだしと定数時間ていすうじかん乗算じょうざん実行じっこうする。時間計算量じかんけいさんりょう T(n)

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

たすため、T(n)=Θ(n) である。最大さいだい呼出深度よびだししんどn+1 であり、かくフレームが定数量ていすうりょう領域りょういき使用しようするため、呼出よびだしスタックの補助空間計算量ほじょくうかんけいさんりょうΘ(n) である。

8設計せっけい検証けんしょう手順てじゅん

  1. 入力にゅうりょく返値かえりち仕様しよう明示めいじする。
  2. 再帰呼出さいきよびだしをようしない基底条件きていじょうけんさだめる。
  3. ちいさい同種どうしゅ入力にゅうりょくから結果けっか構成こうせいする再帰段階さいきだんかいさだめる。
  4. 各再帰呼出かくさいきよびだしで厳密げんみつ減少げんしょうする停止尺度ていししゃくど提示ていじし、停止性ていしせい証明しょうめいする。
  5. 帰納法きのうほうなどにより正当性せいとうせい証明しょうめいする。
  6. 再帰呼出さいきよびだしの回数かいすう最大深度さいだいしんどから時間じかん空間計算量くうかんけいさんりょう評価ひょうかする。
data/lecture/information/algorithm/foundation/sorting-basics.lecture.n.md

Recursion Basics

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
タブを全て閉じる