ספטמבר 04copyright meir kalech1 c programming language chapter 8: recursion

27
04 ררררררCopyright Meir Kalech 1 C programming Language Chapter 8 : Recursion

Post on 21-Dec-2015

235 views

Category:

Documents


4 download

TRANSCRIPT

ספטמבר 04 Copyright Meir Kalech 1

C programming Language

Chapter 8 :Recursion

ספטמבר 04 Copyright Meir Kalech 2

What is Recursion? Recursive function is a function that contains a call to itself. Upon calling itself, the function is passed to the stack (more

precisely: the function parameters are passed to the stack).

void func(){ puts(”test”);} void main(){ for(int i=0; i<3; i++)

func();}

func

main

iteration1

func

main

iteration2

func

main

iteration3

ספטמבר 04 Copyright Meir Kalech 3

What is Recursion? We can change the loop to a recursive call. Instead of calling the function

three times in the main, the function calls itself until the stopping condition (recursion base).

Recursion without a recursion base is an infinite recursion. A recursive function stays in the stack until all the functions that were

called afterwards have returned.

void func(int i){ if(i>0) { puts(”test”); func(i-1); }}void main(){ func(3);}

func(3)

main

func(3)

func(2)

main

func(3)

func(2)

func(1)

main

func(3)

func(2)

func(1)

func(0)

main

func(3)

func(2)

func(1)

main

func(3)

func(2)

main

func(3)

main

Return iteration1

Return iteration2

Return iteration3

Call 4 Call 3 Call 2 Call 1

Stopping condition

Recursive call

ספטמבר 04 Copyright Meir Kalech 4

Statement after Recursion Call

Recursion has two stages:1. Pre-stopping recursion: function is inserted

recursively into the stack.2. Post-stopping recursion: function is deleted

recursively from the stack. We can do operations after the

recursive call statement. In this case, the operations will be done only after the return from the recursive call.

ספטמבר 04 Copyright Meir Kalech 5

Statement after Recursion Call - Example

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);} main

Main call

ספטמבר 04 Copyright Meir Kalech 6

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

main

Call 1

func(3):

ספטמבר 04 Copyright Meir Kalech 7

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

i=2

main

Call 2

func(3):

func(2):

ספטמבר 04 Copyright Meir Kalech 8

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

i=2

i=1

main

Call 3

func(3):

func(2):

func(1):

ספטמבר 04 Copyright Meir Kalech 9

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

i=2

i=1

i=0

main

Call 4

func(3):

func(2):

func(1):

func(0):

ספטמבר 04 Copyright Meir Kalech 10

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

i=2

i=1

main

Back to 3

func(3):

func(2):

func(1):

Output: 1

ספטמבר 04 Copyright Meir Kalech 11

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

i=2

main

Back to 2

func(3):

func(2):

Output: 1 2

ספטמבר 04 Copyright Meir Kalech 12

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

i=3

main

Back to 1

func(3):

Output: 1 2 3

ספטמבר 04 Copyright Meir Kalech 13

void func(int i){ if(i>0) { func(i-1);

printf(“%d”, i); }}void main(){ func(3);}

Statement after Recursion Call - Example

main

Back to main

Output: 1 2 3

ספטמבר 04 Copyright Meir Kalech 14

Solving Problems with Recursion

The relationship between induction and recursion:

Induction: We have a problem for element n. The

solution is known for a base element. We assume that

the solution is known for element n-1.

Recursion: The function gets element n. The function

returns a known solution for a base element (recursion

base). It calls itself recursively with element n-1.

ספטמבר 04 Copyright Meir Kalech 15

Solving Problems with Recursion - Factorial

For example, factorial calculation: What is the factorial of n?

• n * (n-1)! What is the factorial of n-1?

• n-1 * (n-2)! What is the factorial of n-2?

• n-2 * (n-3)!• …

What is the factorial of 0?• 1

5! == 5 * 4!4! == 4 * 3!3! == 3 * 2!2! == 2 * 1!1! == 1 * 0!0! == 1

ספטמבר 04 Copyright Meir Kalech 16

Solving Problems with Recursion - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);}

main

main call

ספטמבר 04 Copyright Meir Kalech 17

Solving Problems with Recursions - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

main

Call 1

factorial(3):

ספטמבר 04 Copyright Meir Kalech 18

Solving Problems with Recursions - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

n=2Return: 2*?

main

Call 2

factorial(3):

factorial(2):

ספטמבר 04 Copyright Meir Kalech 19

Solving Problems with Recursions - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

n=2Return: 2*?

n=1Return: 1*?

main

Call 3

factorial(3):

factorial(2):

factorial(1):

ספטמבר 04 Copyright Meir Kalech 20

Solving Problems with Recursions - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

n=2Return: 2*?

n=1Return: 1*?

n=0Return: 1

main

Call 4

factorial(3):

factorial(2):

factorial(1):

factorial(0):

ספטמבר 04 Copyright Meir Kalech 21

Solving Problems with Recursion - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

n=2Return: 2*?

n=1Return: 1*1

main

Back to 3

factorial(3):

factorial(2):

factorial(1):

1

ספטמבר 04 Copyright Meir Kalech 22

Solving Problems with Recursion - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*?

n=2Return: 2*1

main

Back to 2

factorial(3):

factorial(2):

1

ספטמבר 04 Copyright Meir Kalech 23

Solving Problems with Recursion - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);} n=3

Return: 3*2

main

Back to 1

factorial(3):

2

ספטמבר 04 Copyright Meir Kalech 24

Solving Problems with Recursion - Factorial

int factorial(int n){

if(n==0) //recursion basereturn 1;

elsereturn n * factorial(n-

1);}

void main(){ factorial(3);}

main

Back to main

6

ספטמבר 04 Copyright Meir Kalech 25

Comparison between Recursive and Iterative Functions

Recursive function

Iterative function

LengthShorterLonger

Writing styleClearerLess Clear

Execution Speed

SlowerFaster

Memory resources

MoreLess

Writing timeShorterLonger (significantly)

ספטמבר 04 Copyright Meir Kalech 26

Comparison between Recursive and Iterative Functions

Explanation: Writing style -

Iterative functions are usually harder to write. Execution Speed -

The recursive function is slower since the computer has to recursively return (based on the stack).

Memory resources - The recursive function uses a stack every time

and therefore requires more resources.

ספטמבר 04 Copyright Meir Kalech 27

Comparison between Recursive and Iterative Functions

Transformation Rule: Every recursive function that contains a single

recursive call, can be re-written as an iterative function.

And vice versa - any function with one general loop can be re-written as a recursive function. 

Conclusion: The main benefits of recursion are shorter and

clearer programs, which saves programming time, but it’s “expensive" in memory resources and execution speed.