Recursion James Atlas University of Delaware 3/4/2009
Overview Review Recursion –Real-world –Programming –Algorithms
Review (Conditional) Programming Control Mechanisms –conditional (if-else) if i == 0 disp(“zero”); else disp(“non-zero”);
Review (Iteration) Programming Control Mechanisms –iteration (while) i = 1; sum = 0; while i <= 100 sum = sum + i; i = i + 1; end;
Recursion Overview Solution depends on solutions to smaller instances of the same problem
Destroying a Rock Acknowledgement: original example credited to Bradley Kjell, Central Connecticut State University
Destroying a Rock (cont’) When do you stop using the hammer?
Real-world Recursion Technique Two Steps to Recursion: –Is the problem solved? –If not, divide problem into smaller problems, and then solve the smaller problems
Rock-destroying Procedure Two Steps to Recursion: –Is the problem solved? When a piece is small, stop hammering.
Rock-destroying Procedure (cont’) Two Steps to Recursion: –Divide problem into smaller problems To destroy a large rock, hit it with a hammer. The rock shatters, leaving small and large pieces.
Rock-destroying Procedure (cont’) Two Steps to Recursion: –Divide problem into smaller problems To destroy a large rock, hit it with a hammer. The rock shatters, leaving small and large pieces. –and then solve the smaller problems Repeat the Rock-destroying Procedure for each piece.
Search Lost diamond ring on the beach
Search Lost diamond ring on the beach
Search Lost diamond ring on the beach
Search Lost diamond ring on the beach
Search Lost diamond ring on the beach
Programming Recursive Data Recursive Function
Recursive Data Data Structure that refers to itself Example: Binary Tree class BinaryTree BinaryTree leftChild; BinaryTree rightChild;
Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Tracing Recursion Non-recursive, call to function a() function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion functionarglocal return value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion ab() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion ab() bc() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion ab() bc() c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion ab() b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
Tracing Recursion Recursive call to function sum(3) function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1)
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0)
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0) sumn=0 0
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=10 1 sumn=0 0
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
Questions?
Computer Science Examples
File System DOS, Windows 9X, NT 4.0 –Folders contain files or sub-folders –Data is defined recursively Problem: How to search for a file?
Sorting Algorithms Problem: How to sort a list of numbers? Quicksort Mergesort Both use a divide and conquer approach
Quicksort “Divide and Conquer” as recursion Sort a list of numbers: quicksort(list) if list size <= 1 return list; else p = remove random number from list; first = all numbers <= p in list; second = all numbers > p in list; return quicksort(first) + p + quicksort(second);
Search (revisited) “Divide and Conquer” as recursion General form: solve(problem) if base case return easy solve; else return solve(smaller problem 1) operator solve(smaller problem 2);
Dividing a Sub Problem: 16 equal pieces