COSC 1P03 Ch 7 Recursio Itroductio to Data Structures 8.1
COSC 1P03 Recursio Recursio I Mathematics factorial Fiboacci umbers defie ifiite set with fiite defiitio I Computer Sciece sytax rules fiite defiitio, ifiite laguage Itroductio to Data Structures 8.2
! = 1 2 (-1)! = 1 if = 0 (-1)! if > 0
0, 1, 1, 2, 3, 5, 8, 13, 21, f = if 1 f -1 + f -2 if > 1
Statemet: WhileStatemet WhileStatemet: while ( Expressio ) Statemet
COSC 1P03 Recursive Methods Recursive method a method that, directly or idirectly, ivokes itself Factorial derived from Mathematical defiitio 1 for =0 for >0, ivolves a method call method called, this method suspeded value retured & used i expressio log Fiboacci umbers derived from mathematical defiitio for >1, ivolves two method calls first call, the secod call the expressio Itroductio to Data Structures 8.6
COSC 1P03 Method Executio Method ivocatio susped method pass parameters execute called method reactivate callig method Storage for local variables & formal parameters separate storage for each ivocatio if ot oe ivocatio iterferes with aother poor abstractio somewhat more complex tha origial memory model Itroductio to Data Structures 8.7
COSC 1P03 Memory Maagemet Requiremets ew storage for locals & parameters retur poit could be called from more tha oe place allocate storage o demad at method call Activatio record (AR) block of storage for method ivocatio to use represets method activatio storage for all local variables, parameters, retur poit ad other allocated at call, freed at retur method executio ote: locals become udefied at each call Itroductio to Data Structures 8.8
1. storage for AR is allocated 2. actual parameters are evaluated ad copied to storage for local parameters i AR 3. retur address is copied to AR 4. method executio begis (accessig AR for parameters ad locals) 5. retur value (if ay) is evaluated 6. retur address is accessed 7. AR is deallocated 8. callig method cotiues (at retur address) usig retur value (if ay)
COSC 1P03 Activatio Record Stack Method call/retur patter LIFO ARs are stacked push at call pop at retur top AR stack visible Memory allocatio stack represeted i a array (memory) hardware support code fixed size stack grows/shriks i method/retur call patter heap dyamic allocatio of objects may grow/shrik accordig to allocatio demads Itroductio to Data Structures 8.10
calls: A B C D returs: D C B A
COSC 1P03 Patter of Recursive Executio ARs for recursive methods hadled same as o-recursive may be more tha oe AR for the same method o the stack top AR is for the curretly executig ivocatio of the method E.g. factorial iitial call (factorial(3)) Uderstadig recursio abstractio Itroductio to Data Structures 8.13
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; }; // factorial 3 (a)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; }; // factorial 3 3 (b)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; }; // factorial 2 3 3 (c)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; 1 2 3 }; // factorial 3 (d)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; 0 1 2 3 }; // factorial 3 (e)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; 1 0 1 2 3 }; // factorial 3 (f)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { 1 = * factorial(-1); }; retur ; }; // factorial 1 1 2 3 3 (g)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { 1 = * factorial(-1); }; retur ; }; // factorial 2 2 3 3 (h)
: out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { 2 = * factorial(-1); }; retur ; }; // factorial 6 3 3 (i)
: 6 out.writelog(factorial()); : private log factorial ( it ) { log ; if ( == 0 ) { = 1; } else { = * factorial(-1); }; retur ; }; // factorial 3 (j)
COSC 1P03 Recursive Algorithms Termiatio requiremets trivial case reductio proof reductio leads to trivial case Proof of termiatio factorial trivial case: =0 reductio: factorial(-1) proof fiboacci trivial cases: =0, =1 reductio: fiboacci(-1), fiboacci(-2) proof Itroductio to Data Structures 8.24
COSC 1P03 Applyig Recursio Idicators variety of cases trivial case ca express geeral case i terms of reductio Itroductio to Data Structures 8.25
COSC 1P03 Recursio vs Iteratio Iterative algorithm always exists cosider machie code Ca traslate recursive algorithm ito iterative replace by loop stack of state e.g. tail recursio do t eve eed stack iterative factorial Differet poit of view e.g. Fiboacci umbers geerate i sequece Itroductio to Data Structures 8.26
COSC 1P03 Compariso factorial O() vs O() Fiboacci O() vs O(2 ) Pealties for recursio space time Advatages of recursio recursive algorithm sometimes simpler programmer productivity maiteace Basic rule look for iterative solutio whe ot easily foud or complex cosider recursio Itroductio to Data Structures 8.27