0% found this document useful (0 votes)
48 views29 pages

Data Structures: Msc. Ibrahim Mesecan

The document discusses recursion and provides examples of recursive functions including factorial, summing numbers from 1 to N, displaying an array, finding the minimum value in an array, and calculating the Fibonacci sequence. It explains the key concepts of recursion including base cases, recursive calls, and avoiding infinite recursion. Examples are provided to demonstrate how recursive functions work step-by-step.

Uploaded by

Klevis Xhyra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views29 pages

Data Structures: Msc. Ibrahim Mesecan

The document discusses recursion and provides examples of recursive functions including factorial, summing numbers from 1 to N, displaying an array, finding the minimum value in an array, and calculating the Fibonacci sequence. It explains the key concepts of recursion including base cases, recursive calls, and avoiding infinite recursion. Examples are provided to demonstrate how recursive functions work step-by-step.

Uploaded by

Klevis Xhyra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures

Msc. Ibrahim Mesecan

Suggested reading:
“Algorithms” Robert Sedgewick & Kevin Wayne
4th ed. Addison Wesley

Ibrahim
Mesecan
Page 1
Ibrahim
Mesecan
Page 2
Course Objectives
The course objectives :
 To understand the need of recursion
 To understand how recursive functions are
stored in the heap
 To understand the base case
 To be able to define base case
 To be able construct recursive functions
 To understand running times

Ibrahim
Mesecan
Page 3 Recursion & Backtracking
Why do we need recursion?
 In many cases, mathematical expressions can
easily be expressed in term of themselves.
 As in Factorial,
n!=n.(n-1)!
 This makes writing codes easier. And expressing
solutions become more natural.

 The base case needed to be described well.

Ibrahim
Mesecan
Page 4 Recursion & Backtracking
What is recursion?
 When a function calls itself, this function is called a
recursive function.
 There are two types of recursion
 Direct recursion: A function calls itself
 Indirect recursion: A function calls another, and the
other calls back this function.

Ibrahim
Mesecan
Page 5 Recursion & Backtracking
What is recursion?
 Because a function calls itself, if not well designed,
this may go infinitely. To avoid this infinite calls the
base case must be defined.
 In the base case, the function is given a direct value,
and it’s provided that there is no more recursive
calls.

Ibrahim
Mesecan
Page 6 Recursion & Backtracking
Example: n! Factorial
 We can easily express n! = n.(n-1)!;
 What is the base case?
1!=1;
 Any other case:
𝑛. 𝑛 − 1 !
 Then, the entire function description
𝑖𝑓 𝑛 ≤ 1 1
𝑛! =
𝑖𝑓 𝑛 > 1 𝑛. 𝑛 − 1 !

Ibrahim
Mesecan
Page 7 Recursion & Backtracking
Example: n! Factorial

static int factorial (int n)


{
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}

How does it work when you call factorial(4)?

Ibrahim
Mesecan
Page 8 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=?
return

res = factorial(4);

Ibrahim
Mesecan
Page 9 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3)
n=4 n=3
factn1=? factn1=?
return return

res = factorial(4);

Ibrahim
Mesecan
Page 10 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2)
n=4 n=3 n=2
factn1=? factn1=? factn1=?
return return return

res = factorial(4);

Ibrahim
Mesecan
Page 11 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2) factorial(1)
n=4 n=3 n=2 n=1
factn1=? factn1=? factn1=? return 1;
return return return

res = factorial(4);

Ibrahim
Mesecan
Page 12 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3) factorial(2)
n=4 n=3 n=2
factn1=? factn1=? factn1=1
return return return 2

res = factorial(4);

Ibrahim
Mesecan
Page 13 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4) factorial(3)
n=4 n=3
factn1=? factn1=2
return return 6

res = factorial(4);

Ibrahim
Mesecan
Page 14 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=6
return 24

res = factorial(4);

Ibrahim
Mesecan
Page 15 Recursion & Backtracking
Example: factorial(4)
static int factorial (int n) {
if (n<=1) return 1;
int factn1 = factorial (n-1);
return n * factn1;
}
factorial(4)
n=4
factn1=6
return 24

res = 24;

Ibrahim
Mesecan
Page 16 Recursion & Backtracking
Base case example - 2

Sum of the numbers from 1 to N

Ibrahim
Mesecan
Page 17 Recursion & Backtracking
Base case example - 2

Sum of the numbers from 1 to N


 We can easily express
Sum(n) = n + Sum(n-1);
 What is the base case?

Ibrahim
Mesecan
Page 18 Recursion & Backtracking
Base case example - 2

Sum of the numbers from 1 to N


 We can easily express
Sum(n) = n + Sum(n-1);
 What is the base case?
𝑆𝑢𝑚(0) = 0;
 Any other case:

Ibrahim
Mesecan
Page 19 Recursion & Backtracking
Base case example - 2

Sum of the numbers from 1 to N


 We can easily express
Sum(n) = n + Sum(n-1);
 What is the base case?
𝑆𝑢𝑚(0) = 0;
 Any other case:
𝑛 + 𝑆𝑢𝑚 𝑛 − 1 ;
 Then, the entire function description
𝑖𝑓 𝑛 == 0 0
𝑆𝑢𝑚(1. . 𝑛) =
𝑖𝑓 𝑛 ≥ 1 𝑛 + 𝑆𝑢𝑚 1. . 𝑛 − 1 ;
Ibrahim
Mesecan
Page 20 Recursion & Backtracking
Sum of the numbers from 1 to N

public int Sum( int N )


{
if( N < 1 ) return 0;
return N + Sum(N-1);
}

Ibrahim
Mesecan
Page 21 Recursion & Backtracking
Base case example - 3

Display an array
 Show the last element. Then call the function to
show the previous element in the list.
 What is the base case?
if(n==0)
 What we do in the base case?
Show just the number and don’t recur
System.out.print(lst[N]+", ");
 Any other case:
System.out.print(lst[N]+", ");
Ibrahim
Mesecan show(lst,N-1);
Page 22 Recursion & Backtracking
Display an array

static void show(int lst[], int N )


{
System.out.print(lst[N]+", ");
if( N >=1 ) show(lst,N-1);
}

Ibrahim
Mesecan
Page 23 Recursion & Backtracking
FindMin

𝑛=0 𝑙𝑠𝑡[𝑛]
 𝐹𝑀𝑖𝑛(𝑙𝑠𝑡, 𝑛) =
𝑛 ≥ 1 min(𝑙𝑠𝑡 𝑛 , 𝐹𝑀𝑖𝑛(𝑙𝑠𝑡, 𝑛 − 1)

int FndMin(int lst[], int n)


{
if (n==0) return lst[n];
return Math.min(lst[n],FndMin(lst,n-1));
}

Ibrahim
Mesecan
Page 24 Recursion & Backtracking
Fibonacci

 What is the base case?


if(n<=1)
 What we do in the base case?
return 1;
and don’t recur
 Any other case:
Fib(n) = Fib(n-1) + Fib(n-2);

Ibrahim
Mesecan
Page 25 Recursion & Backtracking
Fibonacci
static int cnt = 1;
static int Fib( int N )
{
cnt++;
if( N <= 1 ) return 1;
return Fib(N - 1) + Fib( N - 2 );
}

What is the output for the following? Why?


int res = Fib(10); int res = Fib(20);
System.out.println(cnt); System.out.println(cnt);
Output for 10: Output for (20):
177 21891
Ibrahim
Mesecan
Page 26 Recursion & Backtracking
Ex: Drawing Circles

Ibrahim
Mesecan
Page 27 Recursion & Backtracking
Binary numbers

Trace how it works for Binary(0) for n=2.

static void Binary(int m) {


if (m == n) {
print();
} else {
X[m] = 0; Binary(m + 1);
X[m] = 1; Binary(m + 1);
}
}
Ibrahim
Mesecan
Page 28 Recursion & Backtracking
Recursive or Iterative

 Nearly all the recursive questions can be


implemented as iterative (with loops) solutions
 In general, recursive algorithms are expressed
easier than iterative algorithms.
 There are times that you can use recursive
algorithms better without increasing the running
time complexity.
 There are also times that recursive algorithms
increase the running time geometrically.
 If possible, try using iterative solutions.
Ibrahim
Mesecan
Page 29 Recursion & Backtracking

You might also like