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

Session 6

This document discusses recursion and backtracking. It begins with an introduction to recursion, explaining how it works using divide-and-conquer and providing examples like calculating factorials recursively. Next, it demonstrates how recursion works by stepping through the recursive factorial function call stack. Several problems are then presented that can be solved using recursion, like calculating Fibonacci numbers and greatest common divisors. The document also introduces backtracking as a method for systematically searching all possible solutions to a problem. Finally, several examples of problems solved using backtracking are given, such as generating all subsequences of a string.

Uploaded by

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

Session 6

This document discusses recursion and backtracking. It begins with an introduction to recursion, explaining how it works using divide-and-conquer and providing examples like calculating factorials recursively. Next, it demonstrates how recursion works by stepping through the recursive factorial function call stack. Several problems are then presented that can be solved using recursion, like calculating Fibonacci numbers and greatest common divisors. The document also introduces backtracking as a method for systematically searching all possible solutions to a problem. Finally, several examples of problems solved using backtracking are given, such as generating all subsequences of a string.

Uploaded by

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

RECURSION

&
BACKTRACKING
PREPARED BY : KHALED EL-HADY
Table of contents

01 Intro to recursion 04 Problems

How recursion
02 works ? 05 Intro to Backtracking

03 Why recursion
works ?
06 Problems again !
• INTRO TO RECURSION

- One way to think of recursion is that it’s when a function calls itself.

- Another way to think of recursion is that it’s a way of solving problems


using divide-and-conquer technique.

- Okay, but what is divide-and-conquer ?


Keep reducing the problem to a smaller version of the same problem, until
you reach a version which you can solve directly (base case).

3
• INTRO TO RECURSION
- Factorial:
- a! = a * (a-1) * (a-2) * … * 2 * 1 .

- Factorial iteratively:
- We can get a! by multiplying all numbers from a to 1. Simply, by making a loop.

- Factorial recursively:
- Another way to solve the problem is to notice that a! = a * (a-1)! .
- We can solve the big problem a! if we solved the smaller one (a-1)! .
- Similarly, (a-1)! = (a-1) * (a-2)! . We can keep going until we reach 1! which equals 1 .
- 1! was our base case which can be solved directly.
4
• INTRO TO RECURSION

• Let’s get to the fun part, the code in the


figure illustrates how to get the factorial of a
number recursively.
• Our function has two paths:

1. if a != 1  Let’s break the problem


more until we reach our base case. So,
the answer is a*(a-1)!

2. if a == 1  then this our base case


which can be solved directly. The
answer is simply 1.

5
• HOW RECURSION WORKS ?
- In this part, let’s trace the code to get a feeling of what’s happening.

a=5 a=4
main() factorial(5) factorial(4)

long long a = 5; a = 5; a = 4;
-- Some code -- 5 * 24 = 120 -- Some code -- -- Some code --
4 * 6 = 24
cout << factorial(a); return a * factorial(a-1); return a * factorial(a-1);

cout << 120 3*2=6 a=3


a=1 a=2
factorial(1) factorial(2) factorial(3)

a = 1; a = 2; a = 3;
1 2*1=2
-- Some code -- -- Some code -- -- Some code --
return 1; return a * factorial(a-1); return a * factorial(a-1);

6
• HOW RECURSION WORKS ?

• Each function call creates its own scope.

• The value of a variable in a scope isn’t changed by recursive calls.

• Flow of control passes back to previous scope once function call returns values.

• Recursion may be simpler, more intuitive.

• Recursion may be efficient from programmer POV.

• Recursion may not be efficient from computer POV.


7
• WHY RECURSION
WORKS ?

• Factorial called with a = 1 has no


recursive calls and stops.

• Factorial called with a > 1 makes a


recursive call with a smaller version of
a. So, it must eventually reach a call
with a = 1 .

Code

8
• WHY RECURSION
WORKS ?

• Mathematical Induction:
To prove a statement is true for all n. First, Prove it’s true for the
smallest value (0 or 1). Second, Prove that if it’s true for some number k, it
will be true for k+1.

• Recursion Induction:
 We know that factorial(1) returns a correct value which is one.
 We also know that our recursion step is correct which is
a! = a * (a-1)!
 Then, our function must return the correct value for all inputs.

9
• PROBLEMS:
- FIBONACCI

Given a number N. Print the value of


the Nth Fibonacci number.

Solution

10
• PROBLEMS:
- PRINT FROM N TO 1
Given a number N. Print numbers
from N to 1 in separate lines.

Solution

11
• PROBLEMS:
- PRINT FROM 1 TO N
Given a number N. Print numbers
from 1 to N in separate lines.

Solution

12
• PROBLEMS :

Solution

13
• PROBLEMS :
- GCD

Given two numbers A and B. Print


the GCD of A and B.
- GCD is the greatest common divisor
of A and B.

Solution

14
• PROBLEMS :
- PALINDROME

Given a number N and an array A of N


number. Determine if it’s palindrome
or not.

Solution

15
• PROBLEMS :
- FAST POWER

Implement fast power function using


recursion.

Solution

16
• PROBLEMS :

Solution

17
• PROBLEMS :

INPUT:

33 Solution
524
135
927

OUTPUT:
24

18
• INTRO TO BACKTRACKING

- A method where you are solving problems recursively and trying to search about
every possible combination to get the desired response to your problem.

- Backtracking is a function that calls itself but can be represented as a tree of


calling subproblems.

19
• PROBLEMS :

Solution

20
• PROBLEMS:
- GENERATING
SUBSEQUENCES
- Given string S, print all nonempty
subsequences of it.

Solution

21
• PROBLEMS :

Solution
INPUT:

3
64
432211
35
211
12 400
50 50 50 50 50 50 25 25 25 25 25 25

22
• PROBLEMS :

Solution

23
!THANK YOU
MAIL : [email protected]
FACEBOOK : ICPC.MANSOURA
WEBSITE : MSC.ACM.ORG

You might also like