Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion James Atlas University of Delaware 3/4/2009.

Similar presentations


Presentation on theme: "Recursion James Atlas University of Delaware 3/4/2009."— Presentation transcript:

1 Recursion James Atlas University of Delaware 3/4/2009

2 Overview Review Recursion –Real-world –Programming –Algorithms

3 Review (Conditional) Programming Control Mechanisms –conditional (if-else) if i == 0 disp(“zero”); else disp(“non-zero”);

4 Review (Iteration) Programming Control Mechanisms –iteration (while) i = 1; sum = 0; while i <= 100 sum = sum + i; i = i + 1; end;

5 Recursion Overview Solution depends on solutions to smaller instances of the same problem

6 Destroying a Rock Acknowledgement: original example credited to Bradley Kjell, Central Connecticut State University

7 Destroying a Rock (cont’) When do you stop using the hammer?

8 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

9 Rock-destroying Procedure Two Steps to Recursion: –Is the problem solved? When a piece is small, stop hammering.

10 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.

11 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.

12 Search Lost diamond ring on the beach

13 Search Lost diamond ring on the beach

14 Search Lost diamond ring on the beach

15 Search Lost diamond ring on the beach

16 Search Lost diamond ring on the beach

17 Programming Recursive Data Recursive Function

18 Recursive Data Data Structure that refers to itself Example: Binary Tree class BinaryTree BinaryTree leftChild; BinaryTree rightChild;

19 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);

20 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);

21 Tracing Recursion Non-recursive, call to function a() function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

22 Tracing Recursion functionarglocal return value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

23 Tracing Recursion ab() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

24 Tracing Recursion ab() bc() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

25 Tracing Recursion ab() bc() c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

26 Tracing Recursion ab() b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;

27 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;

28 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;

29 Tracing Recursion Recursive call to function sum(3) function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);

30 Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value

31 Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1)

32 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)

33 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

34 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

35 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

36 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

37 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

38 Questions?

39

40

41

42 Computer Science Examples

43 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?

44 Sorting Algorithms Problem: How to sort a list of numbers? Quicksort Mergesort Both use a divide and conquer approach

45 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);

46 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);

47 Dividing a Sub Problem: 16 equal pieces


Download ppt "Recursion James Atlas University of Delaware 3/4/2009."

Similar presentations


Ads by Google