Recursio Recursio Jordi Cortadella Departmet of Computer Sciece Priciple: Reduce a complex problem ito a simpler istace of the same problem Recursio Itroductio to Programmig Dept. CS, UPC 2 Mathematical iductio: example The sum of the first odd umbers is 2 : Recursio is a cocept tightly related to mathematical iductio (typically used for proofs o atural umbers): Proof a base case for the first atural umber Iductive step: proof that validity for also implies validity for +1 (domio effect) Iformal proof: 1 3 5 7 9 2-1 Itroductio to Programmig Dept. CS, UPC 3 Itroductio to Programmig Dept. CS, UPC 4
Mathematical iductio: example Factorial Defiitio of factorial:! = 1 2 2 1 Base case: it works for = 1 Iductive step: let us assume it works for // Pre: 0 // Returs! it factorial(it ) { // iterative solutio it f = 1; for (it i = ; i > 0; --i) f = f i; retur f; Itroductio to Programmig Dept. CS, UPC 5 Factorial Itroductio to Programmig Dept. CS, UPC 6 Factorial: recursive solutio Recursive defiitio: factorial() // Pre: 0 // Returs! 1!, > 0! = ቊ 1, = 0 factorial(-1) factorial(-2) factorial(2) 2 factorial(1) 1-2 -1! it factorial(it ) { // recursive solutio if ( == 0) retur 1; else retur factorial( - 1); Itroductio to Programmig Dept. CS, UPC 7 Itroductio to Programmig Dept. CS, UPC 8
Recursio Geerally, recursive solutios are simpler tha (or as simple as) iterative solutios. There are some problems i which oe solutio is much simpler tha the other. Geerally, recursive solutios are slightly less efficiet tha the iterative oes (if the compiler does ot try to optimize the recursive calls). There are atural recursive solutios that ca be extremely iefficiet. Be careful! Itroductio to Programmig Dept. CS, UPC 9 Recursive desig: termiatio It is ot clear whether the followig fuctio termiates: // Pre: 1 // Returs the umber of steps of the Collatz sequece // that starts with. it Collatz(it ) { // recursive solutio if ( == 1) retur 0; else if (%2 == 0) retur 1 + Collatz(/2); else retur 1 + Collatz(3 + 1); The reaso is that 3 +1 is ot closer to 1 tha Recursive desig: 3 steps 1. Idetify the basic cases i which a simple o-recursive solutio ca be provided. Example: 0! = 1! = 1. 2. Recursive cases: solve the complex cases i terms of simpler istaces of the same problem (domio effect). Example: factorial() = factorial(-1). 3. Termiatio: guaratee that the parameters of the call move closer to the basic cases at each recursive call (the domio chai is ot ifiite). Example: I the case of a factorial, -1 is closer to 0 tha. Therefore, we ca guaratee that this fuctio termiates. Itroductio to Programmig Dept. CS, UPC 10 Recursio: behid the scees... f = factorial(4);... it factorial(it ) 4 if ( 4 <= 1) retur 1; else retur 4 factorial(-1); 3 it factorial(it ) 3 if ( 3 <= 1) retur 1; else retur 3 factorial(-1); 2 it factorial(it ) 2 if ( 2 <= 1) retur 1; else retur 2 factorial(-1); 1 it factorial(it ) 1 if ( 1 <= 1) retur 1; else retur 1 factorial(-1); Itroductio to Programmig Dept. CS, UPC 11 Itroductio to Programmig Dept. CS, UPC 12
Recursio: behid the scees... f = factorial(4); 24... it 24 factorial(it ) 4 if ( 4 <= 1) retur 1; else retur 4 factorial(-1); 24 6 3 it 6 factorial(it ) 3 if ( 3 <= 1) retur 1; else retur 3 factorial(-1); 6 2 2 it 2 factorial(it ) 2 if ( 2 <= 1) retur 1; else retur 2 factorial(-1); 2 1 1 it 1 factorial(it ) 1 if ( 1 <= 1) retur 1; else retur 1 factorial(-1); Recursio: behid the scees Each time a fuctio is called, a ew istace of the fuctio is created. Each time a fuctio returs, its istace is destroyed. The creatio of a ew istace oly requires the allocatio of memory space for data (parameters ad local variables). The istaces of a fuctio are destroyed i reverse order of their creatio, i.e. the first istace to be created will be the last to be destroyed. Itroductio to Programmig Dept. CS, UPC 13 Prit a umber i base b Desig a procedure that prits a umber i base b to cout. Examples: 1024 is 10000000000 i base 2 1101221 i base 3 2662 i base 7 1024 i base 10 Itroductio to Programmig Dept. CS, UPC 14 Prit a umber i base b // Pre: 0, 2 b 10 // Prits the represetatio of i base b void prit_base(it, it b); Basic case: < b oly oe digit. Prit it. Geeral case: b Prit the leadig digits (/b) Prit the last digit (%b) Itroductio to Programmig Dept. CS, UPC 15 Itroductio to Programmig Dept. CS, UPC 16
Write a umber i base b A simpler versio // Pre: 0, 2 b 10 // Prits the represetatio of i base b // Pre: 0, 2 b 10 // Prits the represetatio of i base b void prit_base(it, it b) { if ( < b) cout << ; else { prit_base(/b, b); cout << %b; void prit_base(it, it b) { if ( >= b) prit_base(/b, b); cout << %b; Itroductio to Programmig Dept. CS, UPC 17 Itroductio to Programmig Dept. CS, UPC 18 The puzzle was iveted by the Frech mathematicia Édouard Lucas i 1883. There is a leged about a Idia temple that cotais a large room with three time-wor posts i it, surrouded by 64 golde disks. To fulfil a aciet prophecy, Brahmi priests have bee movig these disks, i accordace with the rules of the puzzle, sice that time. The puzzle is therefore also kow as the Tower of Brahma puzzle. Accordig to the leged, whe the last move i the puzzle is completed, the world will ed. It is ot clear whether Lucas iveted this leged or was ispired by it. (from http://e.wikipedia.org/wiki/tower_of_haoi) Rules of the puzzle: A complete tower of disks must be moved from oe post to aother. Oly oe disk ca be moved at a time. No disk ca be placed o top of a smaller disk. Not allowed! Itroductio to Programmig Dept. CS, UPC 19 Itroductio to Programmig Dept. CS, UPC 20
What rules determie the ext move? How may moves do we eed? There is o trivial iterative solutio. Itroductio to Programmig Dept. CS, UPC 21 Itroductio to Programmig Dept. CS, UPC 22 // Pre: is the umber of disks ( 0). // from, to ad aux are the ames of the pegs. // Post: solves the by movig disks // from peg from to peg to usig peg aux Iductive reasoig: assume that we kow how to solve Haoi for -1 disks Haoi(-1) from left to middle (safe: the largest disk is always at the bottom) Move the largest disk from the left to the right Haoi(-1) from the middle to the right (safe: the largest disk is always at the bottom) Itroductio to Programmig Dept. CS, UPC 23 void Haoi(it, char from, char to, char aux) { if ( > 0) { Haoi( - 1, from, aux, to); cout << Move disk from << from << to << to << edl; Haoi( - 1, aux, to, from); Itroductio to Programmig Dept. CS, UPC 24
// Mai program to solve the // for ay umber of disks it mai() { it Ndisks; // Read the umber of disks ci >> Ndisks; // Solve the puzzle Haoi(Ndisks, L, R, M ); Itroductio to Programmig Dept. CS, UPC 25 > Haoi 5 Move disk from L to M Move disk from M to R Move disk from L to M Move disk from R to L Move disk from L to M Move disk from M to R Move disk from R to L Move disk from M to R Move disk from L to M Move disk from M to R Itroductio to Programmig Dept. CS, UPC 26 Haoi(1,L,R,M) Haoi(1,L,R,M) Haoi(2,L,M,R) L M Haoi(2,L,M,R) Haoi(0,R,L,M) Haoi(2,L,M,R) L M Haoi(0,R,L,M) Haoi(1,R,M,L) R M Haoi(1,R,M,L) R M Haoi(3,L,R,M) Haoi(3,L,R,M) Haoi(1,M,L,R) M L Haoi(1,M,L,R) M L Haoi(2,M,R,L) M R Haoi(0,R,L,M) Haoi(2,M,R,L) Haoi(2,M,R,L) M R Haoi(0,R,L,M) Haoi(1,L,R,M) Haoi(1,L,R,M) Itroductio to Programmig Dept. CS, UPC 27 Itroductio to Programmig Dept. CS, UPC 28
How may moves do we eed for disks? Moves() = 1 + 2 Moves(-1) Moves() 1 1 2 3 3 7 4 15 5 31 6 63 2-1 Let us assume that we ca move oe disk every secod. How log would it take to move disks? time 1 1s 5 31s 10 17m 3s 15 9h 6m 7s 20 12d 3h 16m 15s 25 1y 23d 8h 40m 31s 30 > 34y 40 > 34,000y 50 > 35,000,000y 60 > 36,000,000,000y Itroductio to Programmig Dept. CS, UPC 29 Itroductio to Programmig Dept. CS, UPC 30