Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Salim Arfaoui.

Similar presentations


Presentation on theme: "Recursion Salim Arfaoui."— Presentation transcript:

1 Recursion Salim Arfaoui

2 Outline Learning objectives: Mystery to solve!!!!
In this lecture we will: Study recursion. look at the stack . Learning objectives: Learn what a recursion is and when we need it. Why learn recursion. Demonstrate how a recursive process works. Give an insight of how a recursive function is realized. Mystery to solve!!!!

3 Recursion Definition Objects are defined in terms of other objects of the same type. Describing an action in terms of itself. Solving a problem using smaller occurrences of the same problem.

4 Recursion Example

5

6

7 Recursive Programming
Definition A technique in which: Procedures and functions call themselves. Divide the bigger problem into a succession of smaller problems Successive repetitions are processed up to the (Base Case)

8 Recursive Programming
Example Find your way home: If you are at home, stop moving. Take one step toward home. "find your way home".

9 Recursive Programming
Factorial Example Problem: calculate n! (n factorial) Formula: n! = 1 if n = 0 n! = n · · ·3·2·1 if n > 0 5! = 5 · 4 · 3·2·1 if n = 5 n! = n·(n-1)·(n – 2) · · ·2·1 if n > 0 Recursively: if n = 0, then n! = 1 Base Case if n > 0, then n! = n * (n-1)! General Case

10 Recursive Programming
Factorial Example * Iterative Solution: int factorial (int n){ int result; for (int i = 1; i <= n; i++) {     result = result * i; } return result;} * Recursive Solution: int factorial (int n){ if(n<=1) Base Case return 1; else return n * factorial(n-1);} Recursive call

11 Recursive Programming
Factorial Example Fact. 6th: N=0, Finished: returns 1 Stack Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

12 Recursive Programming
Factorial Example Stack Fact. 5th: N=1, Finished: returns 1*1 Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

13 Recursive Programming
Factorial Example Stack Fact. 4th: N=2, Finished: returns 2*1 Fact (1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

14 Recursive Programming
Factorial Example Stack Fact. 3rd: N=3, Finished: returns 3*2 Fact (2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

15 Recursive Programming
Factorial Example Stack Fact. 2nd: N=4, Finished: returns 4*6 Fact (3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

16 Recursive Programming
Factorial Example Stack Fact. 1st: N=5, Finished: returns 5*24 Fact (4) Main Algorithm: Unfinished: answer <- Fact (5) Fact (5)

17 Recursive Programming
Factorial Example Main Algorithm: Finished: answer <- 120

18 Mystery!!!! When will the world end ?!
Mystery: A 64-disk version of the puzzle lies in a Hanoi monastery, where monks work continuously toward solving the puzzle. When they complete the puzzle, the world will come to an end. Objective: Move the entire stack to another Pole Rules: ) Only one disk can be moved at a time. 2) A disk can only be moved if it is the uppermost disk. 3) No disk may be placed on top of a smaller disk.

19 Mystery!!!! When will the world end ?! T(n) = 264 - 1
How long would it take the monks to solve the puzzle? Move 1 disc per millisecond: Approximately 584,600,000 years.  Move 1 disc per second: roughly 585 billion years (18,446,744,073,709,551,615 turns)

20 For Next Time Types of recursion
Linear Recursive: only makes a single call to itself each time the it runs (Factoriel) Tail recursive:  the recursive call is the last thing the function does (GCD) Binary Recursive: functions with two recursive calls. Nested Recursion: one of the arguments to the recursive function is the recursive function itself! (Ackerman's function) Mutual Recursion: doesn't necessarily need to call itself. function A calls function B which calls function C which in turn calls function A.


Download ppt "Recursion Salim Arfaoui."

Similar presentations


Ads by Google