SlideShare a Scribd company logo
1 of 35
Recursion
The Power of Calling a Method from Itself
Svetlin Nakov
Telerik Software Academy
academy.telerik.com
ManagerTechnicalTraining
http://nakov.com
Table of Contents
1. What is Recursion?
2. Calculating Factorial Recursively
3. Generating All 0/1Vectors Recursively
4. Finding All Paths in a Labyrinth Recursively
5. Recursion or Iteration?
2
What is Recursion?
 Recursion is when a methods calls itself
 Very powerful technique for implementing
combinatorial and other algorithms
 Recursion should have
 Direct or indirect recursive call
 The method calls itself directly or through other
methods
 Exit criteria (bottom)
 Prevents infinite recursion
3
Recursive Factorial – Example
 Recursive definition of n! (n factorial):
4
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
Recursive Factorial – Example
 Calculating factorial:
 0! = 1
 n! = n* (n-1)!, n>0
 Don't try this at home!
 Use iteration instead
5
static decimal Factorial(decimal 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
Generating 0/1Vectors
 How to generate all 8-bit vectors recursively?
00000000
00000001
...
01111111
10000000
...
11111110
11111111
 How to generate all n-bit vectors?
7
Generating 0/1Vectors (2)
 Algorithm Gen01(n): put 0 and 1 at the last
position n and call Gen01(n-1) for the rest:
8
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)  Stop!
Generating 0/1Vectors (3)
9
static void Gen01(int index, int[] vector)
{
if (index == -1)
Print(vector);
else
for (int i=0; i<=1; i++)
{
vector[index] = i;
Gen01(index-1, vector);
}
}
static void Main()
{
int size = 8;
int[] vector = new int[size];
Gen01(size-1, vector);
}
Generating 0/1Vectors
Live Demo
Finding All Paths in a Labyrinth
 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 the all 4 directions: left, right, up, down
 We need to find all paths to the bottom right
corner
11
Start
position End
position
*
* * * *
* * * * *
Finding All Paths in a Labyrinth (2)
 There are 3 different paths from the top left
corner to the bottom right corner:
12
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)
Finding All Paths in a Labyrinth (2)
 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 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)
13
Find All Paths: Algorithm
 Representing the labyrinth as matrix of characters
(in this example 5 rows and 7 columns):
 Spaces (' ') are passable cells
 Asterisks ('*') are not passable cells
 The symbol 'e' is the exit (can be multiple)
14
static char[,] lab =
{
{' ', ' ', ' ', '*', ' ', ' ', ' '},
{'*', '*', ' ', '*', ' ', '*', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', '*', '*', '*', '*', '*', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', 'е'},
};
Find All Paths: Algorithm (2)
15
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)
Find All Paths: Algorithm (3)
16
// Temporary 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
Find All Paths and PrintThem
 How to print all paths found by our recursive
algorithm?
 Each move's direction can be stored in array
 Need to pass the movement direction at each
recursive call
 At the start of each recursive call the current
direction is appended to the array
 At the end of each recursive call the last
direction is removed form the array 18
static char[] path =
new char[lab.GetLength(0) * lab.GetLength(1)];
static int position = 0;
Find All Paths and PrintThem (2)
19
static void FindPathToExit(int row, int col, char direction)
{
...
// Append the current direction to the path
path[position++] = 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
position--;
}
Find and Print All
Paths in a Labyrinth
Live Demo
Recursion or Iteration?
When to Use andWhen to Avoid Recursion?
Recursion Can be Harmful!
 When used incorrectly the recursion could take
too much memory and computing power
 Example:
22
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
How the Recursive Fibonacci
Calculation Works?
 fib(n) makes about fib(n) recursive calls
 The same value is calculated many, many times!
24
Fast Recursive Fibonacci
 Each Fibonacci sequence member can be
remembered once it is calculated
 Can be returned directly when needed again
25
static decimal[] fib = new decimal[MAX_FIB];
static decimal Fibonacci(int n)
{
if (fib[n] == 0)
{
// The value of fib[n] is still not calculated
if ((n == 1) || (n == 2))
fib[n] = 1;
else
fib[n] = Fibonacci(n - 1) + Fibonacci(n - 2);
}
return fib[n];
}
Fast Recursive Fibonacci
Live Demo
When to Use Recursion?
 Avoid recursion when an obvious iterative
algorithm exists
 Examples: factorial, Fibonacci numbers
 Use recursion for combinatorial algorithm
where at each step you need to recursively
explore more than one possible continuation
 Examples: permutations, all paths in labyrinth
 If you have only one recursive call in the body of
a recursive method, it can directly become
iterative (like calculating factorial)
27
Summary
 Recursion means to call a method from itself
 It should always have a bottom at which
recursive calls stop
 Very powerful technique for implementing
combinatorial algorithms
 Examples: generating combinatorial
configurations like permutations,
combinations, variations, etc.
 Recursion can be harmful when not used
correctly
28
форум програмиране,форум уеб дизайн
курсове и уроци по програмиране,уеб дизайн – безплатно
програмиранеза деца – безплатни курсове и уроци
безплатен SEO курс -оптимизация за търсачки
уроци по уеб дизайн, HTML,CSS, JavaScript,Photoshop
уроци по програмиранеи уеб дизайн за ученици
ASP.NET MVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC
безплатен курс"Разработка на софтуер в cloud среда"
BG Coder -онлайн състезателна система -online judge
курсове и уроци по програмиране,книги – безплатно отНаков
безплатен курс"Качествен програменкод"
алго академия – състезателно програмиране,състезания
ASP.NET курс -уеб програмиране,бази данни, C#,.NET,ASP.NET
курсове и уроци по програмиране– Телерик академия
курсмобилни приложения с iPhone, Android,WP7,PhoneGap
freeC#book, безплатна книга C#,книга Java,книга C#
Дончо Минков -сайт за програмиране
Николай Костов -блог за програмиране
C#курс,програмиране,безплатно
Recursion
http://academy.telerik.com
Exercises
1. Write a recursive program that simulates execution
of n nested loops from 1 to n. Examples:
1 1 1
1 1 2
1 1 3
1 1 1 2 1
n=2 -> 1 2 n=3 -> ...
2 1 3 2 3
2 2 3 3 1
3 3 2
3 3 3
30
Exercises (2)
2. Write a recursive program for generating and
printing all the combinations with duplicates of k
elements from n-element set. Example:
n=3, k=2  (1 1), (1 2), (1 3), (2 2), (2 3), (3 3)
3. Write a recursive program for generating and
printing all permutations of the numbers 1, 2, ..., n
for given integer number n. Example:
n=3  {1, 2, 3}, {1, 3, 2}, {2, 1, 3},
{2, 3, 1}, {3, 1, 2},{3, 2, 1}
31
Exercises (3)
4. Write a recursive program for generating and
printing all ordered k-element subsets from n-
element set (variationsVk
n).
Example: n=3, k=2
(1 1), (1 2), (1 3), (2 1), (2 2), (2 3), (3 1), (3 2), (3 3)
5. Write a program for generating and printing all
subsets of k strings from given set of strings.
Example: s = {test, rock, fun}, k=2
(test rock), (test fun), (rock fun)
32
Exercises (4)
6. We are given a matrix of passable and non-passable
cells. Write a recursive program for finding all paths
between two cells in the matrix.
7. Modify the above program to check whether a path
exists between two cells without finding all possible
paths.Test it over an empty 100 x 100 matrix.
8. Write a program to find the largest connected area
of adjacent empty cells in a matrix.
9. Implement the BFS algorithm to find the shortest
path between two cells in a matrix (read about
Breath-First Search in Wikipedia).
33
Exercises (5)
10. We are given a matrix of passable and non-passable
cells. Write a recursive program for finding all areas
of passable cells in the matrix.
11. Write a recursive program that traverses the entire
hard disk drive C: and displays all folders recursively
and all files inside each of them.
34
FreeTrainings @Telerik Academy
 Fundamentals of C# Programming
Course
 csharpfundamentals.telerik.com
 Telerik Software Academy
 academy.telerik.com
 Telerik Academy @ Facebook
 facebook.com/TelerikAcademy
 Telerik Software Academy Forums
 forums.academy.telerik.com

More Related Content

What's hot

What's hot (20)

10. Recursion
10. Recursion10. Recursion
10. Recursion
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Alg1
Alg1Alg1
Alg1
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
Lec4
Lec4Lec4
Lec4
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
 
03 notes
03 notes03 notes
03 notes
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 

Viewers also liked

arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01Abdul Samee
 
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891FREMEProjectH2020
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Abdul Samee
 
Freme at feisgiltt 2015 freme use cases
Freme at feisgiltt 2015   freme use casesFreme at feisgiltt 2015   freme use cases
Freme at feisgiltt 2015 freme use casesFREMEProjectH2020
 
Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Abdul Samee
 
Freme general-overview-version-june-2015
Freme general-overview-version-june-2015Freme general-overview-version-june-2015
Freme general-overview-version-june-2015FREMEProjectH2020
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01Abdul Samee
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02Abdul Samee
 
Garima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen
 

Viewers also liked (12)

Linked data tooling XML
Linked data tooling XMLLinked data tooling XML
Linked data tooling XML
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
Fremeatfeisgiltt2015 fremelinkeddatalocalisers-150603090934-lva1-app6891
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
Freme at feisgiltt 2015 freme use cases
Freme at feisgiltt 2015   freme use casesFreme at feisgiltt 2015   freme use cases
Freme at feisgiltt 2015 freme use cases
 
Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02Input outputdisplaydevices-140819061228-phpapp02
Input outputdisplaydevices-140819061228-phpapp02
 
Freme general-overview-version-june-2015
Freme general-overview-version-june-2015Freme general-overview-version-june-2015
Freme general-overview-version-june-2015
 
Sasaki mlkrep-20150710
Sasaki mlkrep-20150710Sasaki mlkrep-20150710
Sasaki mlkrep-20150710
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
Garima Sareen Nagpal - CV
Garima Sareen Nagpal - CVGarima Sareen Nagpal - CV
Garima Sareen Nagpal - CV
 

Similar to 13 recursion-120712074623-phpapp02

10 Recursion
10 Recursion10 Recursion
10 Recursionmaznabili
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questionsGradeup
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Introduction to python cheat sheet for all
Introduction to python cheat sheet for allIntroduction to python cheat sheet for all
Introduction to python cheat sheet for allshwetakushwaha45
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingzukun
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................nourhandardeer3
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter NotesJanie Clayton
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Ziyauddin Shaik
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer AlgorithmsAlaa Al-Makhzoomy
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Scienceresearchinventy
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 englishyassminkhaldi1
 
Lecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptxLecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptxngBinh3
 

Similar to 13 recursion-120712074623-phpapp02 (20)

10 Recursion
10 Recursion10 Recursion
10 Recursion
 
FUNDAMETAL ALG.ppt
FUNDAMETAL ALG.pptFUNDAMETAL ALG.ppt
FUNDAMETAL ALG.ppt
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
Introduction to python cheat sheet for all
Introduction to python cheat sheet for allIntroduction to python cheat sheet for all
Introduction to python cheat sheet for all
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
3D Math Without Presenter Notes
3D Math Without Presenter Notes3D Math Without Presenter Notes
3D Math Without Presenter Notes
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Science
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 
Lecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptxLecture 7_introduction_algorithms.pptx
Lecture 7_introduction_algorithms.pptx
 

13 recursion-120712074623-phpapp02

  • 1. Recursion The Power of Calling a Method from Itself Svetlin Nakov Telerik Software Academy academy.telerik.com ManagerTechnicalTraining http://nakov.com
  • 2. Table of Contents 1. What is Recursion? 2. Calculating Factorial Recursively 3. Generating All 0/1Vectors Recursively 4. Finding All Paths in a Labyrinth Recursively 5. Recursion or Iteration? 2
  • 3. What is Recursion?  Recursion is when a methods calls itself  Very powerful technique for implementing combinatorial and other algorithms  Recursion should have  Direct or indirect recursive call  The method calls itself directly or through other methods  Exit criteria (bottom)  Prevents infinite recursion 3
  • 4. Recursive Factorial – Example  Recursive definition of n! (n factorial): 4 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
  • 5. Recursive Factorial – Example  Calculating factorial:  0! = 1  n! = n* (n-1)!, n>0  Don't try this at home!  Use iteration instead 5 static decimal Factorial(decimal num) { if (num == 0) return 1; else return num * Factorial(num - 1); } The bottom of the recursion Recursive call: the method calls itself
  • 7. Generating 0/1Vectors  How to generate all 8-bit vectors recursively? 00000000 00000001 ... 01111111 10000000 ... 11111110 11111111  How to generate all n-bit vectors? 7
  • 8. Generating 0/1Vectors (2)  Algorithm Gen01(n): put 0 and 1 at the last position n and call Gen01(n-1) for the rest: 8 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)  Stop!
  • 9. Generating 0/1Vectors (3) 9 static void Gen01(int index, int[] vector) { if (index == -1) Print(vector); else for (int i=0; i<=1; i++) { vector[index] = i; Gen01(index-1, vector); } } static void Main() { int size = 8; int[] vector = new int[size]; Gen01(size-1, vector); }
  • 11. Finding All Paths in a Labyrinth  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 the all 4 directions: left, right, up, down  We need to find all paths to the bottom right corner 11 Start position End position * * * * * * * * * *
  • 12. Finding All Paths in a Labyrinth (2)  There are 3 different paths from the top left corner to the bottom right corner: 12 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)
  • 13. Finding All Paths in a Labyrinth (2)  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 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) 13
  • 14. Find All Paths: Algorithm  Representing the labyrinth as matrix of characters (in this example 5 rows and 7 columns):  Spaces (' ') are passable cells  Asterisks ('*') are not passable cells  The symbol 'e' is the exit (can be multiple) 14 static char[,] lab = { {' ', ' ', ' ', '*', ' ', ' ', ' '}, {'*', '*', ' ', '*', ' ', '*', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', '*', '*', '*', '*', '*', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', 'е'}, };
  • 15. Find All Paths: Algorithm (2) 15 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)
  • 16. Find All Paths: Algorithm (3) 16 // Temporary 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); }
  • 17. Find All Paths in a Labyrinth Live Demo
  • 18. Find All Paths and PrintThem  How to print all paths found by our recursive algorithm?  Each move's direction can be stored in array  Need to pass the movement direction at each recursive call  At the start of each recursive call the current direction is appended to the array  At the end of each recursive call the last direction is removed form the array 18 static char[] path = new char[lab.GetLength(0) * lab.GetLength(1)]; static int position = 0;
  • 19. Find All Paths and PrintThem (2) 19 static void FindPathToExit(int row, int col, char direction) { ... // Append the current direction to the path path[position++] = 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 position--; }
  • 20. Find and Print All Paths in a Labyrinth Live Demo
  • 21. Recursion or Iteration? When to Use andWhen to Avoid Recursion?
  • 22. Recursion Can be Harmful!  When used incorrectly the recursion could take too much memory and computing power  Example: 22 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! }
  • 24. How the Recursive Fibonacci Calculation Works?  fib(n) makes about fib(n) recursive calls  The same value is calculated many, many times! 24
  • 25. Fast Recursive Fibonacci  Each Fibonacci sequence member can be remembered once it is calculated  Can be returned directly when needed again 25 static decimal[] fib = new decimal[MAX_FIB]; static decimal Fibonacci(int n) { if (fib[n] == 0) { // The value of fib[n] is still not calculated if ((n == 1) || (n == 2)) fib[n] = 1; else fib[n] = Fibonacci(n - 1) + Fibonacci(n - 2); } return fib[n]; }
  • 27. When to Use Recursion?  Avoid recursion when an obvious iterative algorithm exists  Examples: factorial, Fibonacci numbers  Use recursion for combinatorial algorithm where at each step you need to recursively explore more than one possible continuation  Examples: permutations, all paths in labyrinth  If you have only one recursive call in the body of a recursive method, it can directly become iterative (like calculating factorial) 27
  • 28. Summary  Recursion means to call a method from itself  It should always have a bottom at which recursive calls stop  Very powerful technique for implementing combinatorial algorithms  Examples: generating combinatorial configurations like permutations, combinations, variations, etc.  Recursion can be harmful when not used correctly 28
  • 29. форум програмиране,форум уеб дизайн курсове и уроци по програмиране,уеб дизайн – безплатно програмиранеза деца – безплатни курсове и уроци безплатен SEO курс -оптимизация за търсачки уроци по уеб дизайн, HTML,CSS, JavaScript,Photoshop уроци по програмиранеи уеб дизайн за ученици ASP.NET MVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC безплатен курс"Разработка на софтуер в cloud среда" BG Coder -онлайн състезателна система -online judge курсове и уроци по програмиране,книги – безплатно отНаков безплатен курс"Качествен програменкод" алго академия – състезателно програмиране,състезания ASP.NET курс -уеб програмиране,бази данни, C#,.NET,ASP.NET курсове и уроци по програмиране– Телерик академия курсмобилни приложения с iPhone, Android,WP7,PhoneGap freeC#book, безплатна книга C#,книга Java,книга C# Дончо Минков -сайт за програмиране Николай Костов -блог за програмиране C#курс,програмиране,безплатно Recursion http://academy.telerik.com
  • 30. Exercises 1. Write a recursive program that simulates execution of n nested loops from 1 to n. Examples: 1 1 1 1 1 2 1 1 3 1 1 1 2 1 n=2 -> 1 2 n=3 -> ... 2 1 3 2 3 2 2 3 3 1 3 3 2 3 3 3 30
  • 31. Exercises (2) 2. Write a recursive program for generating and printing all the combinations with duplicates of k elements from n-element set. Example: n=3, k=2  (1 1), (1 2), (1 3), (2 2), (2 3), (3 3) 3. Write a recursive program for generating and printing all permutations of the numbers 1, 2, ..., n for given integer number n. Example: n=3  {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2},{3, 2, 1} 31
  • 32. Exercises (3) 4. Write a recursive program for generating and printing all ordered k-element subsets from n- element set (variationsVk n). Example: n=3, k=2 (1 1), (1 2), (1 3), (2 1), (2 2), (2 3), (3 1), (3 2), (3 3) 5. Write a program for generating and printing all subsets of k strings from given set of strings. Example: s = {test, rock, fun}, k=2 (test rock), (test fun), (rock fun) 32
  • 33. Exercises (4) 6. We are given a matrix of passable and non-passable cells. Write a recursive program for finding all paths between two cells in the matrix. 7. Modify the above program to check whether a path exists between two cells without finding all possible paths.Test it over an empty 100 x 100 matrix. 8. Write a program to find the largest connected area of adjacent empty cells in a matrix. 9. Implement the BFS algorithm to find the shortest path between two cells in a matrix (read about Breath-First Search in Wikipedia). 33
  • 34. Exercises (5) 10. We are given a matrix of passable and non-passable cells. Write a recursive program for finding all areas of passable cells in the matrix. 11. Write a recursive program that traverses the entire hard disk drive C: and displays all folders recursively and all files inside each of them. 34
  • 35. FreeTrainings @Telerik Academy  Fundamentals of C# Programming Course  csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com  Telerik Academy @ Facebook  facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com

Editor's Notes

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  5. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*