SlideShare a Scribd company logo
Recursion
Recursive Algorithms
and Backtracking
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
2
1. What is Recursion?
2. Recursive Factorial
3. Generating 0/1 Vectors and Combinations
4. Backtracking
 The 8 Queens Problem
 Finding All Paths in a Labyrinth Recursively
5. Recursion or Iteration?
 Harmful Recursion and Optimizing Bad Recursion
Table of Contents
3
 The stack is a small fixed-size chunk of memory (e.g. 1MB)
 Keeps the currently called functions in a stack data structure
 Changes as the program enters / exits a function
The Stack
static void Main()
{
Print("Hello");
}
Main()
Print()
When Print is called it is
pushed onto the stack
When Print returns it
gets popped from the stack
Stack
Debugging a Program
Call Stack Live Demo
What is Recursion?
6
 Recursion is when a method calls itself
 Powerful technique for combinatorial
and branched search algorithms design
 Recursion should have:
 Direct or indirect recursive call
 The method calls itself directly
 Оr through other methods
 Exit criteria (bottom)
 Prevents infinite recursion
What is Recursion?
7
 Recursive definition of n! (n factorial):
Recursive Factorial – Example
n! = n * (n–1)! for n > 0
0! = 1
 5! = 5 * 4! = 5 * 4 * 3 * 2 * 1 * 1 = 120
 4! = 4 * 3! = 4 * 3 * 2 * 1 * 1 = 24
 3! = 3 * 2! = 3 * 2 * 1 * 1 = 6
 2! = 2 * 1! = 2 * 1 * 1 = 2
 1! = 1 * 0! = 1 * 1 = 1
 0! = 1
8
 Calculating factorial:
 0! = 1
 n! = n* (n-1)!, n>0
Recursive Factorial – Example
static decimal Factorial(int num)
{
if (num == 0)
return 1;
else
return num * Factorial(num - 1);
}
The bottom of
the recursion
Recursive call: the
method calls itself
Recursive Factorial
Live Demo
Recursive Array Sum
In-Class Exercise (Lab)
10 3 7 3 4 27
11
 Recursive calls are slightly slower than iteration
 Parameters and return values travel through the stack at each step
 Prefer iteration for linear calculations (without branched calls)
Performance: Recursion vs. Iteration
static long RecurFact(int n)
{
if (n == 0)
return 1;
else
return n * Fact(n - 1);
}
static long IterFact(int num)
{
long result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
Recursive factorial: Iterative factorial:
Factorial Performance
Live Demo
13
 Infinite recursion == a method calls itself infinitely
 Typically, infinite recursion == bug in the program
 The bottom of the recursion is missing or wrong
 In C# / Java / C++ causes "stack overflow" error
Infinite Recursion
static long Calulate(int n)
{
return Calulate(n + 1);
}
Infinite Recursion
Live Demo
15
 Direct recursion
 A method directly calls itself
 Indirect recursion
 Method A calls B, method B calls A
 Or even A  B  C  A
 Funny example of infinite indirect recursion:
 http://www.nakov.com/blog/2013/01/23/indirect-recursion/
Direct and Indirect Recursion
void A() {
…
A();
}
void A() {
…
B();
}
void B() {
…
A();
}
16
 Recursive methods have 3 parts:
 Pre-actions (before calling the recursion)
 Recursive calls (step-in)
 Post-actions (after returning from recursion)
 Recursive computation can be based on:
 Pre-actions + recursive calls (forward way)
 Recursive calls + post-actions (backward way)
 Pre-actions + recursive calls + post-actions (combined way)
Recursion Pre-Actions and Post-Actions
17
Pre-Actions and Post-Actions – Example
static void PrintFigure(int n)
{
if (n == 0) // Bottom of the recursion
return;
// Pre-action: print n asterisks
Console.WriteLine(new string('*', n));
// Recursive call: print figure of size n-1
PrintFigure(n - 1);
// Post-action: print n hashtags
Console.WriteLine(new string('#', n));
}
Pre-Actions
and Post-Actions
Live Demo
19
 How to generate all 8-bit vectors recursively?
 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1
 ...
 0 1 1 1 1 1 1 1
 1 0 0 0 0 0 0 0
 ...
 1 1 1 1 1 1 1 0
 1 1 1 1 1 1 1 1
 How to generate all n-bit vectors?
Generating 0/1 Vectors
20
 Algorithm Gen01(n): put 0 and 1 at the last position n and call
Gen01(n-1) for the rest:
Generating 0/1 Vectors (2)
x x x x x x 0
Gen01(6):
Gen01(5)
x x x x x x 1
Gen01(5)
x x x x x 0 y
Gen01(5):
Gen01(4)
x x x x x 1 y
Gen01(4)
…
Gen01(-1):
Print;
Stop!

21
Generating 0/1 Vectors (3)
static void Gen01(int index, int[] vector)
{
if (index < 0)
Print(vector);
else
for (int i = 0; i <= 1; i++)
{
vector[index] = i;
Gen01(index-1, vector);
}
}
static void Main()
{
int n = 8;
int[] vector = new int[n];
Gen01(n-1, vector);
}
Generating 0/1 Vectors
Live Demo
Generating Combinations
Simple Recursive Algorithm
24
 Combinations in math represent all the ways to extract a subset
from a larger set of elements
 Select k members from a set of n elements
 Example: we can select 3 different elements from the set
{4, 5, 6, 7, 8} in 10 different ways:
(4, 5, 6) (4, 5, 7) (4, 5, 8) (4, 6, 7) (4, 6, 8)
(4, 7, 8) (5, 6, 7) (5, 6, 8) (5, 7, 8) (6, 7, 8)
 Combinations with and without repetitions can be easily
generated with recursion
Generating Combinations
25
 Algorithm GenCombs(k): put the numbers [1 … n] at position k
and call GenCombs(k+1) recursively for the rest of the elements:
Generating Combinations (2)
1 x x x x x x
GenCombs(0):
GenCombs(1)
Put all numbers in range
[1..n] at position k
1 1 x x x x x
GenCombs(1):
GenCombs(2)
Put all numbers in range
[1..n] at position k
GenCombs(n):
…
Print;
Stop!
Bottom of
recursion
26
Generating Combinations (3)
static void GenCombs(
int[] arr, int index, int startNum, int endNum)
{
if (index >= arr.Length) // Combination found --> print it
Console.WriteLine("(" + String.Join(", ", arr) + ")");
else
for (int i = startNum; i <= endNum; i++)
{
arr[index] = i;
GenCombs(arr, index + 1,
i + 1, endNum);
}
}
static void Main()
{
int[] arr = new int[3];
GenCombs(arr, 0, 4, 8);
}
Generating Combinations
Live Demo
Backtracking
Solving Computational Problems
by Generating All Candidates
29
 What is backtracking?
 Backtracking is a class of algorithms for finding all solutions to
some combinatorial computational problem
 E.g. find all paths from Sofia to Varna
 How does backtracking work?
 At each step tries all perspective possibilities recursively
 Drop all non-perspective possibilities as early as possible
 Backtracking has exponential running time!
Backtracking
30
Backtracking Algorithm (Pseudocode)
void Backtracking(Node node)
{
if (node is solution)
PrintSolution(node);
else
for each child c of node
if (c is perspective candidate)
{
MarkPositionVisited(c);
Backtracking(c);
UnmarkPositionVisited(c);
}
}
The "8 Queens" Puzzle
Backtracking in Practice
32
 Write a program to find all
possible placements of
 8 queens on a chessboard
 So that no two queens can
attack each other
 http://en.wikipedia.org/wiki/
Eight_queens_puzzle
The "8 Queens" Puzzle
33
 Backtracking
algorithm
 For finding all
solutions to the
"8 Queens Puzzle"
 At each step:
 Put a queen at
free position
 Recursive call
 Remove the
queen
Solving The "8 Queens" Puzzle
void PutQueens(row)
{
if (row == 8)
PrintSolution();
else
for (col = 0 … 7)
if (CanPlaceQueen(row, col))
{
MarkAllAttackedPositions(row, col);
PutQueens(row + 1);
UnmarkAllAttackedPositions(row, col);
}
}
The "8 Queens" Puzzle
In-Class Exercise (Lab)
35
 We are given a labyrinth
 Represented as matrix of cells of size M x N
 Empty cells are passable, the others (*) are not
 We start from the top left corner and can move in all 4 directions:
left, right, up, down
 We want to find all paths to the bottom right corner
Finding All Paths in a Labyrinth
Start
position
End
position
36
 There are 3 different paths from the top left corner to the
bottom right corner:
Finding All Paths in a Labyrinth (2)
0 1 2 *
* * 3 * *
6 5 4
7 * * * * *
8 9 10 11 12 13 14
0 1 2 * 8 9 10
* * 3 * 7 * 11
4 5 6 12
* * * * * 13
14
1) 2)
0 1 2 *
* * 3 * *
4 5 6 7 8
* * * * * 9
10
3)
37
 Suppose we have an algorithm FindExit(x,y) that finds and prints
all paths to the exit (bottom right corner) starting from position (x,y)
 If (x,y) is not passable, no paths are found
 If (x,y) is already visited, no paths are found
 Otherwise:
 Mark position (x,y) as visited (to avoid cycles)
 Find recursively all paths to the exit from all neighbor cells: (x-1,y) ,
(x+1,y) , (x,y+1) , (x,y-1)
 Mark position (x,y) as free (can be visited again)
Finding All Paths in a Labyrinth (3)
38
 Representing the labyrinth as matrix of characters (in this example 5
rows and 7 columns):
 Spaces (' ') are passable cells
 Asterisks ('*') are not passable
 The symbol 'e' is the exit (can occur multiple times)
Find All Paths: Algorithm
static char[,] lab =
{
{' ', ' ', ' ', '*', ' ', ' ', ' '},
{'*', '*', ' ', '*', ' ', '*', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', '*', '*', '*', '*', '*', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', 'е'},
};
39
Find All Paths: Algorithm (2)
static void FindExit(int row, int col)
{
if ((col < 0) || (row < 0) || (col >= lab.GetLength(1))
|| (row >= lab.GetLength(0)))
{
// We are out of the labyrinth -> can't find a path
return;
}
// Check if we have found the exit
if (lab[row, col] == 'е')
{
Console.WriteLine("Found the exit!");
}
if (lab[row, col] != ' ')
{
// The current cell is not free -> can't find a path
return;
}
(example continues)
40
Find All Paths: Algorithm (3)
// Temporaryly mark the current cell as visited
lab[row, col] = 's';
// Invoke recursion to explore all possible directions
FindExit(row, col - 1); // left
FindExit(row - 1, col); // up
FindExit(row, col + 1); // right
FindExit(row + 1, col); // down
// Mark back the current cell as free
lab[row, col] = ' ';
}
static void Main()
{
FindExit(0, 0);
}
Find All Paths in a Labyrinth
Live Demo
42
 How to print all paths found by our recursive algorithm?
 Each move's direction can be stored in a list
 Need to pass the movement direction at each recursive call (L, R,
U, or D)
 At the start of each recursive call the current direction is
appended to the list
 At the end of each recursive call the last direction is removed from
the list
Find All Paths and Print Them
static List<char> path = new List<char>();
43
Find All Paths and Print Them (2)
static void FindPathToExit(int row, int col, char direction)
{
...
// Append the current direction to the path
path.Add(direction);
if (lab[row, col] == 'е')
{
// The exit is found -> print the current path
}
...
// Recursively explore all possible directions
FindPathToExit(row, col - 1, 'L'); // left
FindPathToExit(row - 1, col, 'U'); // up
FindPathToExit(row, col + 1, 'R'); // right
FindPathToExit(row + 1, col, 'D'); // down
...
// Remove the last direction from the path
path.RemoveAt(path.Count - 1);
}
Find and Print All Paths
in a Labyrinth
Live Demo
Recursion or Iteration?
When to Use and When to Avoid Recursion?
46
 When used incorrectly recursion could take too much memory and
computing power
 Example:
Recursion Can be Harmful!
static decimal Fibonacci(int n)
{
if ((n == 1) || (n == 2))
return 1;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
static void Main()
{
Console.WriteLine(Fibonacci(10)); // 89
Console.WriteLine(Fibonacci(50)); // This will hang!
}
Harmful Recursion
Live Demo
48
 fib(n) makes about fib(n) recursive calls
 The same value is calculated many, many times!
How the Recursive Fibonacci Calculation Works?
49
 Each Fibonacci
sequence member
 Can be remembered
once it is calculated
 Can be returned
directly when needed
again
Fast Recursive Fibonacci
static decimal[] fib = new decimal[MAX_FIB];
static decimal Fib(int n)
{
if (fib[n] == 0)
{
// fib[n] is still not calculated
if ((n == 1) || (n == 2))
fib[n] = 1;
else
fib[n] = Fib(n - 1) + Fib(n - 2);
}
return fib[n];
}
Fast Recursive Fibonacci
Live Demo
51
 Avoid recursion when an obvious iterative algorithm exists
 Examples: factorial, Fibonacci numbers
 Use recursion for combinatorial algorithms where
 At each step you need to recursively explore more than one
possible continuation
 I.e. for branched recursive algorithms
 Examples: permutations, all paths in a labyrinth
 If you have only one recursive call in the body of a recursive
method, it can directly become iterative (like calculating factorial)
When to Use Recursion?
52
 Recursion means to call a method from itself
 It should always have a bottom at which the recursive calls stop
 Very powerful technique for implementing combinatorial algorithms
 Examples: generating combinatorial configurations like vectors,
permutations, combinations, variations, etc.
 Backtracking finds all solutions / optimal solution of
combinatorial problem by generating all possibilities
 Without non-perspective candidates
 Recursion can be harmful when not used correctly
Summary
?
Recursion
https://softuni.bg/trainings/1331/algorithms-april-2016
License
 This course (slides, examples, labs, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International" license
54
 Attribution: this work may contain portions from
 "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
 "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license
Free Trainings @ Software University
 Software University Foundation – softuni.org
 Software University – High-Quality Education,
Profession and Job for Software Developers
 softuni.bg
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University @ YouTube
 youtube.com/SoftwareUniversity
 Software University Forums – forum.softuni.bg

More Related Content

PDF
Stacks,queues,linked-list
pinakspatel
 
PPTX
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Complement Verb
 
PDF
Vector
Joyjit Choudhury
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PDF
Functions in C++
Pranali Chaudhari
 
PPTX
Looping statement in python
RaginiJain21
 
PDF
Linked list
Nurjahan Nipa
 
PPT
Oops ppt
abhayjuneja
 
Stacks,queues,linked-list
pinakspatel
 
Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11,...
Complement Verb
 
Function C programming
Appili Vamsi Krishna
 
Functions in C++
Pranali Chaudhari
 
Looping statement in python
RaginiJain21
 
Linked list
Nurjahan Nipa
 
Oops ppt
abhayjuneja
 

What's hot (20)

PPTX
OOP concepts -in-Python programming language
SmritiSharma901052
 
PDF
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
PDF
C++ chapter 1
jasvinder162
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
Algorithm Introduction
Ashim Lamichhane
 
PPTX
Functional Programming
Ryan Riley
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPT
Functions in c++
Maaz Hasan
 
PPTX
Paradigms
Edward Blurock
 
PPTX
Lecture09 recursion
Hariz Mustafa
 
PPTX
Tokens expressionsin C++
HalaiHansaika
 
PPTX
Structure in c language
sangrampatil81
 
PPT
Object-Oriented Programming Concepts
Kwangshin Oh
 
PDF
Object-oriented Programming-with C#
Doncho Minkov
 
PDF
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Edureka!
 
PDF
Looping in c language
Infinity Tech Solutions
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PDF
C++ tokens and expressions
NabeelaNousheen
 
OOP concepts -in-Python programming language
SmritiSharma901052
 
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
C++ chapter 1
jasvinder162
 
Python Flow Control
Mohammed Sikander
 
Algorithm Introduction
Ashim Lamichhane
 
Functional Programming
Ryan Riley
 
classes and objects in C++
HalaiHansaika
 
Functions in c++
Maaz Hasan
 
Paradigms
Edward Blurock
 
Lecture09 recursion
Hariz Mustafa
 
Tokens expressionsin C++
HalaiHansaika
 
Structure in c language
sangrampatil81
 
Object-Oriented Programming Concepts
Kwangshin Oh
 
Object-oriented Programming-with C#
Doncho Minkov
 
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Edureka!
 
Looping in c language
Infinity Tech Solutions
 
Functions in c
sunila tharagaturi
 
Exception Handling in C#
Abid Kohistani
 
C++ tokens and expressions
NabeelaNousheen
 
Ad

Similar to 10. Recursion (20)

PPTX
13 recursion-120712074623-phpapp02
Abdul Samee
 
PPT
10 Recursion
maznabili
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPT
M251_Meeting 9 (Recursion_AdvancedJava).ppt
smartashammari
 
PDF
Recursion For the Rest of Us (CS Fundamentals Series)
Haseeb Qureshi
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPTX
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Dheeraj Kataria
 
PDF
(1) collections algorithms
Nico Ludwig
 
PPT
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
PPT
Recursion
Malainine Zaid
 
PDF
Algorithm review
chidabdu
 
PDF
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
PDF
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
PDF
Iterations and Recursions
Abdul Rahman Sherzad
 
PPT
Lecture 7
Soran University
 
PPT
recursion based on koffmann and wolfgang
vidhyapm2
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PDF
Recursive algorithms
subhashchandra197
 
PPTX
VCE Unit 01 (2).pptx
skilljiolms
 
13 recursion-120712074623-phpapp02
Abdul Samee
 
10 Recursion
maznabili
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
M251_Meeting 9 (Recursion_AdvancedJava).ppt
smartashammari
 
Recursion For the Rest of Us (CS Fundamentals Series)
Haseeb Qureshi
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Dheeraj Kataria
 
(1) collections algorithms
Nico Ludwig
 
FUNDAMETAL ALG.ppt
Menaka Sivakumar
 
Recursion
Malainine Zaid
 
Algorithm review
chidabdu
 
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
Iterations and Recursions
Abdul Rahman Sherzad
 
Lecture 7
Soran University
 
recursion based on koffmann and wolfgang
vidhyapm2
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Recursive algorithms
subhashchandra197
 
VCE Unit 01 (2).pptx
skilljiolms
 
Ad

More from Intro C# Book (20)

PPTX
17. Java data structures trees representation and traversal
Intro C# Book
 
PPTX
Java Problem solving
Intro C# Book
 
PPTX
21. Java High Quality Programming Code
Intro C# Book
 
PPTX
20.5 Java polymorphism
Intro C# Book
 
PPTX
20.4 Java interfaces and abstraction
Intro C# Book
 
PPTX
20.3 Java encapsulation
Intro C# Book
 
PPTX
20.2 Java inheritance
Intro C# Book
 
PPTX
20.1 Java working with abstraction
Intro C# Book
 
PPTX
19. Java data structures algorithms and complexity
Intro C# Book
 
PPTX
18. Java associative arrays
Intro C# Book
 
PPTX
16. Java stacks and queues
Intro C# Book
 
PPTX
14. Java defining classes
Intro C# Book
 
PPTX
13. Java text processing
Intro C# Book
 
PPTX
12. Java Exceptions and error handling
Intro C# Book
 
PPTX
11. Java Objects and classes
Intro C# Book
 
PPTX
09. Java Methods
Intro C# Book
 
PPTX
05. Java Loops Methods and Classes
Intro C# Book
 
PPTX
07. Java Array, Set and Maps
Intro C# Book
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPTX
02. Data Types and variables
Intro C# Book
 
17. Java data structures trees representation and traversal
Intro C# Book
 
Java Problem solving
Intro C# Book
 
21. Java High Quality Programming Code
Intro C# Book
 
20.5 Java polymorphism
Intro C# Book
 
20.4 Java interfaces and abstraction
Intro C# Book
 
20.3 Java encapsulation
Intro C# Book
 
20.2 Java inheritance
Intro C# Book
 
20.1 Java working with abstraction
Intro C# Book
 
19. Java data structures algorithms and complexity
Intro C# Book
 
18. Java associative arrays
Intro C# Book
 
16. Java stacks and queues
Intro C# Book
 
14. Java defining classes
Intro C# Book
 
13. Java text processing
Intro C# Book
 
12. Java Exceptions and error handling
Intro C# Book
 
11. Java Objects and classes
Intro C# Book
 
09. Java Methods
Intro C# Book
 
05. Java Loops Methods and Classes
Intro C# Book
 
07. Java Array, Set and Maps
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
02. Data Types and variables
Intro C# Book
 

Recently uploaded (20)

PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
MCEAMONVILLAVER
 
PPTX
Crypto Recovery California Services.pptx
lionsgate network
 
PPTX
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
PPTX
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
PPTX
nagasai stick diagrams in very large scale integratiom.pptx
manunagapaul
 
PPT
Transformaciones de las funciones elementales.ppt
rirosel211
 
PPTX
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
APNIC
 
PDF
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
PDF
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
PPTX
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
PDF
Centralized Business Email Management_ How Admin Controls Boost Efficiency & ...
XgenPlus Technologies
 
PDF
5g is Reshaping the Competitive Landscape
Stellarix
 
PDF
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
PPTX
How tech helps people in the modern era.
upadhyayaryan154
 
PDF
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
PDF
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
MCEAMONVILLAVER
 
Crypto Recovery California Services.pptx
lionsgate network
 
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
nagasai stick diagrams in very large scale integratiom.pptx
manunagapaul
 
Transformaciones de las funciones elementales.ppt
rirosel211
 
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
APNIC
 
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
Centralized Business Email Management_ How Admin Controls Boost Efficiency & ...
XgenPlus Technologies
 
5g is Reshaping the Competitive Landscape
Stellarix
 
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
How tech helps people in the modern era.
upadhyayaryan154
 
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
Cybersecurity Awareness Presentation ppt.
banodhaharshita
 

10. Recursion

  • 1. Recursion Recursive Algorithms and Backtracking SoftUni Team Technical Trainers Software University http://softuni.bg
  • 2. 2 1. What is Recursion? 2. Recursive Factorial 3. Generating 0/1 Vectors and Combinations 4. Backtracking  The 8 Queens Problem  Finding All Paths in a Labyrinth Recursively 5. Recursion or Iteration?  Harmful Recursion and Optimizing Bad Recursion Table of Contents
  • 3. 3  The stack is a small fixed-size chunk of memory (e.g. 1MB)  Keeps the currently called functions in a stack data structure  Changes as the program enters / exits a function The Stack static void Main() { Print("Hello"); } Main() Print() When Print is called it is pushed onto the stack When Print returns it gets popped from the stack Stack
  • 4. Debugging a Program Call Stack Live Demo
  • 6. 6  Recursion is when a method calls itself  Powerful technique for combinatorial and branched search algorithms design  Recursion should have:  Direct or indirect recursive call  The method calls itself directly  Оr through other methods  Exit criteria (bottom)  Prevents infinite recursion What is Recursion?
  • 7. 7  Recursive definition of n! (n factorial): Recursive Factorial – Example n! = n * (n–1)! for n > 0 0! = 1  5! = 5 * 4! = 5 * 4 * 3 * 2 * 1 * 1 = 120  4! = 4 * 3! = 4 * 3 * 2 * 1 * 1 = 24  3! = 3 * 2! = 3 * 2 * 1 * 1 = 6  2! = 2 * 1! = 2 * 1 * 1 = 2  1! = 1 * 0! = 1 * 1 = 1  0! = 1
  • 8. 8  Calculating factorial:  0! = 1  n! = n* (n-1)!, n>0 Recursive Factorial – Example static decimal Factorial(int num) { if (num == 0) return 1; else return num * Factorial(num - 1); } The bottom of the recursion Recursive call: the method calls itself
  • 10. Recursive Array Sum In-Class Exercise (Lab) 10 3 7 3 4 27
  • 11. 11  Recursive calls are slightly slower than iteration  Parameters and return values travel through the stack at each step  Prefer iteration for linear calculations (without branched calls) Performance: Recursion vs. Iteration static long RecurFact(int n) { if (n == 0) return 1; else return n * Fact(n - 1); } static long IterFact(int num) { long result = 1; for (int i = 1; i <= n; i++) result *= i; return result; } Recursive factorial: Iterative factorial:
  • 13. 13  Infinite recursion == a method calls itself infinitely  Typically, infinite recursion == bug in the program  The bottom of the recursion is missing or wrong  In C# / Java / C++ causes "stack overflow" error Infinite Recursion static long Calulate(int n) { return Calulate(n + 1); }
  • 15. 15  Direct recursion  A method directly calls itself  Indirect recursion  Method A calls B, method B calls A  Or even A  B  C  A  Funny example of infinite indirect recursion:  http://www.nakov.com/blog/2013/01/23/indirect-recursion/ Direct and Indirect Recursion void A() { … A(); } void A() { … B(); } void B() { … A(); }
  • 16. 16  Recursive methods have 3 parts:  Pre-actions (before calling the recursion)  Recursive calls (step-in)  Post-actions (after returning from recursion)  Recursive computation can be based on:  Pre-actions + recursive calls (forward way)  Recursive calls + post-actions (backward way)  Pre-actions + recursive calls + post-actions (combined way) Recursion Pre-Actions and Post-Actions
  • 17. 17 Pre-Actions and Post-Actions – Example static void PrintFigure(int n) { if (n == 0) // Bottom of the recursion return; // Pre-action: print n asterisks Console.WriteLine(new string('*', n)); // Recursive call: print figure of size n-1 PrintFigure(n - 1); // Post-action: print n hashtags Console.WriteLine(new string('#', n)); }
  • 19. 19  How to generate all 8-bit vectors recursively?  0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 1  ...  0 1 1 1 1 1 1 1  1 0 0 0 0 0 0 0  ...  1 1 1 1 1 1 1 0  1 1 1 1 1 1 1 1  How to generate all n-bit vectors? Generating 0/1 Vectors
  • 20. 20  Algorithm Gen01(n): put 0 and 1 at the last position n and call Gen01(n-1) for the rest: Generating 0/1 Vectors (2) x x x x x x 0 Gen01(6): Gen01(5) x x x x x x 1 Gen01(5) x x x x x 0 y Gen01(5): Gen01(4) x x x x x 1 y Gen01(4) … Gen01(-1): Print; Stop! 
  • 21. 21 Generating 0/1 Vectors (3) static void Gen01(int index, int[] vector) { if (index < 0) Print(vector); else for (int i = 0; i <= 1; i++) { vector[index] = i; Gen01(index-1, vector); } } static void Main() { int n = 8; int[] vector = new int[n]; Gen01(n-1, vector); }
  • 24. 24  Combinations in math represent all the ways to extract a subset from a larger set of elements  Select k members from a set of n elements  Example: we can select 3 different elements from the set {4, 5, 6, 7, 8} in 10 different ways: (4, 5, 6) (4, 5, 7) (4, 5, 8) (4, 6, 7) (4, 6, 8) (4, 7, 8) (5, 6, 7) (5, 6, 8) (5, 7, 8) (6, 7, 8)  Combinations with and without repetitions can be easily generated with recursion Generating Combinations
  • 25. 25  Algorithm GenCombs(k): put the numbers [1 … n] at position k and call GenCombs(k+1) recursively for the rest of the elements: Generating Combinations (2) 1 x x x x x x GenCombs(0): GenCombs(1) Put all numbers in range [1..n] at position k 1 1 x x x x x GenCombs(1): GenCombs(2) Put all numbers in range [1..n] at position k GenCombs(n): … Print; Stop! Bottom of recursion
  • 26. 26 Generating Combinations (3) static void GenCombs( int[] arr, int index, int startNum, int endNum) { if (index >= arr.Length) // Combination found --> print it Console.WriteLine("(" + String.Join(", ", arr) + ")"); else for (int i = startNum; i <= endNum; i++) { arr[index] = i; GenCombs(arr, index + 1, i + 1, endNum); } } static void Main() { int[] arr = new int[3]; GenCombs(arr, 0, 4, 8); }
  • 29. 29  What is backtracking?  Backtracking is a class of algorithms for finding all solutions to some combinatorial computational problem  E.g. find all paths from Sofia to Varna  How does backtracking work?  At each step tries all perspective possibilities recursively  Drop all non-perspective possibilities as early as possible  Backtracking has exponential running time! Backtracking
  • 30. 30 Backtracking Algorithm (Pseudocode) void Backtracking(Node node) { if (node is solution) PrintSolution(node); else for each child c of node if (c is perspective candidate) { MarkPositionVisited(c); Backtracking(c); UnmarkPositionVisited(c); } }
  • 31. The "8 Queens" Puzzle Backtracking in Practice
  • 32. 32  Write a program to find all possible placements of  8 queens on a chessboard  So that no two queens can attack each other  http://en.wikipedia.org/wiki/ Eight_queens_puzzle The "8 Queens" Puzzle
  • 33. 33  Backtracking algorithm  For finding all solutions to the "8 Queens Puzzle"  At each step:  Put a queen at free position  Recursive call  Remove the queen Solving The "8 Queens" Puzzle void PutQueens(row) { if (row == 8) PrintSolution(); else for (col = 0 … 7) if (CanPlaceQueen(row, col)) { MarkAllAttackedPositions(row, col); PutQueens(row + 1); UnmarkAllAttackedPositions(row, col); } }
  • 34. The "8 Queens" Puzzle In-Class Exercise (Lab)
  • 35. 35  We are given a labyrinth  Represented as matrix of cells of size M x N  Empty cells are passable, the others (*) are not  We start from the top left corner and can move in all 4 directions: left, right, up, down  We want to find all paths to the bottom right corner Finding All Paths in a Labyrinth Start position End position
  • 36. 36  There are 3 different paths from the top left corner to the bottom right corner: Finding All Paths in a Labyrinth (2) 0 1 2 * * * 3 * * 6 5 4 7 * * * * * 8 9 10 11 12 13 14 0 1 2 * 8 9 10 * * 3 * 7 * 11 4 5 6 12 * * * * * 13 14 1) 2) 0 1 2 * * * 3 * * 4 5 6 7 8 * * * * * 9 10 3)
  • 37. 37  Suppose we have an algorithm FindExit(x,y) that finds and prints all paths to the exit (bottom right corner) starting from position (x,y)  If (x,y) is not passable, no paths are found  If (x,y) is already visited, no paths are found  Otherwise:  Mark position (x,y) as visited (to avoid cycles)  Find recursively all paths to the exit from all neighbor cells: (x-1,y) , (x+1,y) , (x,y+1) , (x,y-1)  Mark position (x,y) as free (can be visited again) Finding All Paths in a Labyrinth (3)
  • 38. 38  Representing the labyrinth as matrix of characters (in this example 5 rows and 7 columns):  Spaces (' ') are passable cells  Asterisks ('*') are not passable  The symbol 'e' is the exit (can occur multiple times) Find All Paths: Algorithm static char[,] lab = { {' ', ' ', ' ', '*', ' ', ' ', ' '}, {'*', '*', ' ', '*', ' ', '*', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', '*', '*', '*', '*', '*', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', 'е'}, };
  • 39. 39 Find All Paths: Algorithm (2) static void FindExit(int row, int col) { if ((col < 0) || (row < 0) || (col >= lab.GetLength(1)) || (row >= lab.GetLength(0))) { // We are out of the labyrinth -> can't find a path return; } // Check if we have found the exit if (lab[row, col] == 'е') { Console.WriteLine("Found the exit!"); } if (lab[row, col] != ' ') { // The current cell is not free -> can't find a path return; } (example continues)
  • 40. 40 Find All Paths: Algorithm (3) // Temporaryly mark the current cell as visited lab[row, col] = 's'; // Invoke recursion to explore all possible directions FindExit(row, col - 1); // left FindExit(row - 1, col); // up FindExit(row, col + 1); // right FindExit(row + 1, col); // down // Mark back the current cell as free lab[row, col] = ' '; } static void Main() { FindExit(0, 0); }
  • 41. Find All Paths in a Labyrinth Live Demo
  • 42. 42  How to print all paths found by our recursive algorithm?  Each move's direction can be stored in a list  Need to pass the movement direction at each recursive call (L, R, U, or D)  At the start of each recursive call the current direction is appended to the list  At the end of each recursive call the last direction is removed from the list Find All Paths and Print Them static List<char> path = new List<char>();
  • 43. 43 Find All Paths and Print Them (2) static void FindPathToExit(int row, int col, char direction) { ... // Append the current direction to the path path.Add(direction); if (lab[row, col] == 'е') { // The exit is found -> print the current path } ... // Recursively explore all possible directions FindPathToExit(row, col - 1, 'L'); // left FindPathToExit(row - 1, col, 'U'); // up FindPathToExit(row, col + 1, 'R'); // right FindPathToExit(row + 1, col, 'D'); // down ... // Remove the last direction from the path path.RemoveAt(path.Count - 1); }
  • 44. Find and Print All Paths in a Labyrinth Live Demo
  • 45. Recursion or Iteration? When to Use and When to Avoid Recursion?
  • 46. 46  When used incorrectly recursion could take too much memory and computing power  Example: Recursion Can be Harmful! static decimal Fibonacci(int n) { if ((n == 1) || (n == 2)) return 1; else return Fibonacci(n - 1) + Fibonacci(n - 2); } static void Main() { Console.WriteLine(Fibonacci(10)); // 89 Console.WriteLine(Fibonacci(50)); // This will hang! }
  • 48. 48  fib(n) makes about fib(n) recursive calls  The same value is calculated many, many times! How the Recursive Fibonacci Calculation Works?
  • 49. 49  Each Fibonacci sequence member  Can be remembered once it is calculated  Can be returned directly when needed again Fast Recursive Fibonacci static decimal[] fib = new decimal[MAX_FIB]; static decimal Fib(int n) { if (fib[n] == 0) { // fib[n] is still not calculated if ((n == 1) || (n == 2)) fib[n] = 1; else fib[n] = Fib(n - 1) + Fib(n - 2); } return fib[n]; }
  • 51. 51  Avoid recursion when an obvious iterative algorithm exists  Examples: factorial, Fibonacci numbers  Use recursion for combinatorial algorithms where  At each step you need to recursively explore more than one possible continuation  I.e. for branched recursive algorithms  Examples: permutations, all paths in a labyrinth  If you have only one recursive call in the body of a recursive method, it can directly become iterative (like calculating factorial) When to Use Recursion?
  • 52. 52  Recursion means to call a method from itself  It should always have a bottom at which the recursive calls stop  Very powerful technique for implementing combinatorial algorithms  Examples: generating combinatorial configurations like vectors, permutations, combinations, variations, etc.  Backtracking finds all solutions / optimal solution of combinatorial problem by generating all possibilities  Without non-perspective candidates  Recursion can be harmful when not used correctly Summary
  • 54. License  This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license 54  Attribution: this work may contain portions from  "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license  "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license
  • 55. Free Trainings @ Software University  Software University Foundation – softuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University @ YouTube  youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bg

Editor's Notes

  • #9: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #20: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #22: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #27: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #53: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*