0% found this document useful (0 votes)
25 views

Lecture 7

Лекция на психолгоий

Uploaded by

qarazhorgakz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 7

Лекция на психолгоий

Uploaded by

qarazhorgakz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Recursive Algorithms, Recurrence Equations

Introduction

In this module, we study recursive algorithms and related concepts. We show how
recursion ties in with induction. That is, the correctness of a recursive algorithm is proved by
induction.

Contents

Simple Examples of Recursive Algorithms


 Factorial
 Finding maximum element of an array
 Computing sum of elements in array
Towers-of-Hanoi Problem
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences

Simple Examples of Recursive Algorithms

Factorial: Consider the factorial definition


𝑛! = 𝑛 × (𝑛 − 1) × (𝑛 − 2) × ⋯ × 2 × 1

This formula may be expressed recursively as

𝑛 × (𝑛 − 1)! , 𝑛>1
𝑛! = {
1, 𝑛=1

Below is a recursive program to compute 𝑛!.

int Factorial (int 𝑛) {


if (𝑛 == 1) return 1;
else {
Temp = Factorial (𝑛 − 1);
return (𝑛 * Temp);
}
}

The input parameter of this function is 𝑛, and the return value is type integer. When
𝑛 = 1, the function returns 1. When 𝑛 > 1, the function calls itself (called a recursive
call) to compute (𝑛 − 1)!. It then multiplies the result by 𝑛, which becomes 𝑛!.

The above program is written in a very basic way for clarity, separating the recursive
call from the subsequent multiplication. These two steps may be combined, as follows.

int Factorial (int 𝑛) {


if (𝑛 == 1) return 1;
else return (n* Factorial (𝑛 − 1));
}

Correctness Proof: The correctness of this recursive program may be proved by


induction.

 Induction Base: From line 1, we see that the function works correctly for 𝑛 = 1.
 Hypothesis: Suppose the function works correctly when it is called with 𝑛 = 𝑚,
for some 𝑚 ≥ 1.
 Induction step: Then, let us prove that it also works when it is called with
𝑛 = 𝑚 + 1. By the hypothesis, we know the recursive call works correctly for
𝑛 = 𝑚 and computes 𝑚!. Subsequently, it is multiplied by 𝑛 = 𝑚 + 1, thus
computes (𝑚 + 1)!. And this is the value correctly returned by the program.

2
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences

Finding Maximum Element of an Array:


As another simple example, let us write a recursive program to compute the maximum
element in an array of n elements, 𝐴[0: 𝑛 − 1]. The problem is broken down as follows.

To compute the Max of n elements for 𝑛 > 1,

 Compute the Max of the first 𝑛 − 1 elements.


 Compare with the last element to find the Max of the entire array.

Below is the recursive program (pseudocode). It is assumed that the array type is dtype,
declared earlier.

dtype Max (dtype 𝐴[ ], int 𝑛) {


if (𝑛 == 1) return 𝐴[0];
else{
𝑇 = Max(𝐴, 𝑛 − 1); //Recursive call to find max of the first 𝑛 − 1 elements
If (𝑇 < 𝐴[𝑛 − 1]) //Compare with the last element
return 𝐴[𝑛 − 1];
else return T;
}
}

Computing Sum of Elements in an Array


Below is a recursive program for computing the sum of elements in an array 𝐴[0: 𝑛 − 1].
𝑛−1

𝑆 = ∑ 𝐴[𝑖]
𝑖=0

dtype Sum (dtype 𝐴[ ], int 𝑛) {


if (𝑛 == 1) return 𝐴[0];
else{
𝑆 = Sum(𝐴, 𝑛 − 1); //Recursive call to compute the sum of the first 𝑛 − 1 elements
𝑆 = 𝑆 + 𝐴[𝑛 − 1]; //Add the last element
return (𝑆)
}
}

3
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences

The above simple problems could be easily solved without recursion. They were
presented recursively only for pedagogical purposes. The next example problem,
however, truly needs the power of recursion. It would be very difficult to solve the
problem without recursion.

Towers of Hanoi Problem


This is a toy problem that is easily solved recursively. There are three towers (posts)
𝐴, 𝐵, and 𝐶. Initially, there are n disks of varying sizes stacked on tower A, ordered by
their size, with the largest disk in the bottom and the smallest one on top. The object of
the game is to have all n discs stacked on tower B in the same order, with the largest
one in the bottom. The third tower is used for temporary storage. There are two rules:

 Only one disk may be moved at a time in a restricted manner, from the top of one
tower to the top of another tower. If we think of each tower as a stack, this means
the moves are restricted to a pop from one stack and push onto another stack.
 A larger disk must never be placed on top of a smaller disk.

The recursive algorithm for moving n disks from tower A to tower B works as follows. If
𝑛 = 1, one disk is moved from tower A to tower B. If 𝑛 > 1,

1 Recursively move the top 𝑛 − 1 disks from 𝐴 𝑡𝑜 𝐶. The largest disk remains on
tower 𝐴 by itself.
2 Move a single disk from 𝐴 𝑡𝑜 𝐵.
3 Recursively move back 𝑛 − 1 disks from 𝐶 𝑡𝑜 𝐵.

An illustration is shown below for 𝑛 = 4.

(a) Initial configuration with 4 disks on Tower 0

4
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences

(b) After recursively moving the top 3 disks from Tower 0 to Tower 2

(c) After moving the bottom disk from Tower 0 to Tower 1

(d) After recursively moving back 3 disks from Tower 2 to Tower 1.

5
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences

Below is the recursive algorithm. The call Towers (𝐴, 𝐵, 𝐶, 𝑛) moves n disks from tower
A to B, using C as temporary storage.

Towers (𝐴, 𝐵, 𝐶, 𝑛) {
1 if (𝑛 == 1) {
2 MoveOne (𝐴, 𝐵);
3 return};
4 Towers (𝐴, 𝐶, 𝐵, 𝑛 − 1);
5 MoveOne (𝐴, 𝐵);
6 Towers (𝐶, 𝐵, 𝐴, 𝑛 − 1);
}

Proof of Correctness

The correctness of the algorithm is proved by induction. For 𝑛 = 1, a single move is


made from A to B. So the algorithm works correctly for 𝑛 = 1. To prove the correctness
for any 𝑛 ≥ 2, suppose the algorithm works correctly for 𝑛 − 1. Then, by the hypothesis,
the recursive call of line 4 works correctly and moves the top 𝑛 − 1 disks to C, leaving
the bottom disk on tower A. The next step, line 5, moves the bottom disk to B. Finally,
the recursive call of line 6 works correctly by the hypothesis and moves back 𝑛 − 1
disks from C to B. Thus, the entire algorithm works correctly for 𝑛.

An Improvement in Algorithm Style

The above algorithm has a single move appearing twice in the code, once for 𝑛 = 1 and
once for 𝑛 > 1. This repetition may be avoided by making 𝑛 = 0 as the termination
criteria for recursion.

Towers (𝐴, 𝐵, 𝐶, 𝑛) {
if (𝑛 == 0) return;
Towers (𝐴, 𝐶, 𝐵, 𝑛 − 1);
MoveOne (𝐴, 𝐵);
Towers (𝐶, 𝐵, 𝐴, 𝑛 − 1);
}

You might also like