Introduction to Recursion Lecture 6 Introduction to Recursion Please fasten your seat belts.
Whatintheheckisrecursion? LB Whatintheheckisrecursion?
Solving a problem in terms of itself. LB Solving a problem in terms of itself. wget Given a URL Read in the file Save it to disk Scan through file Find any JPG or GIF references then save them Find any HTML references then call wget
Solving a problem in terms of itself LB Solving a problem in terms of itself ShowMe Given the name of a directory Read the directory Process each entry If an entry is a file then Print it on the screen Else // It’s a directory!!! ShowMe(the Entry)
Solving a problem in terms of itself LB Solving a problem in terms of itself n! Is it: n * (n-1) * (n-2) * ... 3 * 2 * 1 Or n * (n - 1)!
Solving a problem in terms of itself LB Solving a problem in terms of itself 7! Is it: 7 * 6 * 5 * 4 * 3 * 2 * 1 Or 7 * 6!
Recursion Defined A procedure or function which calls itself. Powerful mechanism for repetition. Makes algorithms more compact and simple. Module calls a “clone” of itself. Very useful, especially for dynamic data types.
Three Characteristics of Recursion Calls itself recursively Has some terminating condition Moves “closer” to the terminating condition. Know this!!!
Two Flavors of Recursion if (terminating condition) then do final actions else move one step closer to terminating condition call “clone” of module endif - or - if (NOT (terminating condition)) then
Everyday Example with Final Actions procedure Pay_The_Bills() if (No_More_Bills_To_Pay) then Mail_The_Bills else Grab a bill Determine payment Write a check Place check in envelope Place stamp on envelope Pay_The_Bills() endif endprocedure //Pay_The_Bills
Everyday Example with No Final Actions procedure Wash_The_Dishes() if (More_Dirty_Dishes) then Grab a dirty dish Wash it Put it in drainer Wash_The_Dishes() endif endprocedure //Wash_The_Dishes
Canonical Example LB function fact returnsa Num(N iot in Num) // Don’t forget comments!!! if(N = 0) then fact returns 1 else fact returns N * fact(N - 1) endif endfunction // fact
A Recursive Procedure Problem: Count from N to 10. procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen Call the procedure CountToTen (7)
ModuleID: Data values Unfinished business Tracing The Recursion To keep track of recursive execution, do what a computer does: maintain information on an activation stack. Each stack frame contains: • Module identifier and variables • Any unfinished business ModuleID: Data values Unfinished business
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=7
7 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 CountToTen: count=7
7 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 CountToTen: count=7
7 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 CountToTen: count=8 CountToTen: count=7
7 8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 CountToTen: count=8 CountToTen: count=7
7 8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 CountToTen: count=8 CountToTen: count=7
7 8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen CountToTen: count=11 7 8 9 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=8 CountToTen: count=7
7 8 9 10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen 7 8 9 10 CountToTen: count=7
Return to the algorithm. 7 8 9 10
Reversing the Work and Recursion Problem: Count from 10 to N. procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen Now the work will happen as the frames pop off the stack!
procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen CountToTen: count=11 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
10 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 CountToTen: count=10 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
10 9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
10 9 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 CountToTen: count=9 CountToTen: count=8 CountToTen: count=7
10 9 8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 8 CountToTen: count=8 CountToTen: count=7
10 9 8 procedure CountToTen (count iot in Num) if (count <= 10) then print (count) // work CountToTen (count + 1) // recurse endif endprocedure //CountToTen procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 8 CountToTen: count=8 CountToTen: count=7
10 9 8 7 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 8 7 CountToTen: count=7
10 9 8 7 procedure CountToTen (count iot in Num) if (count <= 10) then CountToTen (count + 1) // recurse print (count) // work endif endprocedure //CountToTen 10 9 8 7 CountToTen: count=7
Return to the algorithm. 10 9 8 7
Questions?