Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Recursion CIS265/506: Chapter 06 - Recursion.

Similar presentations


Presentation on theme: "Data Structures Recursion CIS265/506: Chapter 06 - Recursion."— Presentation transcript:

1 Data Structures Recursion CIS265/506: Chapter 06 - Recursion

2 CIS265/506: Chapter 06 - Recursion
What is it? An approach to writing repetitive algorithms The algorithm calls itself Not available in older languages COBOL FORTRAN CIS265/506: Chapter 06 - Recursion

3 CIS265/506: Chapter 06 - Recursion
Properties One or more simple cases have a straightforward, non-recursive solution The other cases are redefined in terms that are closer to the simple case CIS265/506: Chapter 06 - Recursion

4 CIS265/506: Chapter 06 - Recursion
Sample Algorithm If this is a simple case solve it else redefine the problem using recursion CIS265/506: Chapter 06 - Recursion

5 CIS265/506: Chapter 06 - Recursion
Case Study - Factorial CIS265/506: Chapter 06 - Recursion

6 CIS265/506: Chapter 06 - Recursion
What is Factorial? The product of integral values from one to itself So: Factorial(3) - usually written as 3! - is 3 X 2 X 1 = 6 Generally: n! = if n = 0 n x (n-1) x (n-2) x … x 3 x 2 x 1 if n > 0 CIS265/506: Chapter 06 - Recursion

7 CIS265/506: Chapter 06 - Recursion
Iterative Solution // This fragment will calculate the factorial using // a loop // n, which is defined elsewhere, is the argument // in n! factN = 1; // initialize this variable for (x=1; x <= n; x++) { factN = factN * x; } CIS265/506: Chapter 06 - Recursion

8 CIS265/506: Chapter 06 - Recursion
Recursive Solution Note how this function calls itself: int recursiveFactorial ( int n) { if (n == 0) return 1; // Simple Case else { return (n * recursiveFactorial(n-1)) } } CIS265/506: Chapter 06 - Recursion

9 CIS265/506: Chapter 06 - Recursion
Recursive Solution Fact(3) = 3 x 2 = 6 Fact(3) = 3 x Fact(2) Fact(2) = 2 x Fact(1) Fact(2) = 2 x 1 = 2 Fact(1) = 1 x Fact(0) Fact(1) = 1 x 1 =1 Fact(0) = 1 CIS265/506: Chapter 06 - Recursion

10 CIS265/506: Chapter 06 - Recursion
How does it work? Program calls subroutine Current module suspends operation Subroutine takes control Upon completion, subroutine returns to main module Unless called by reference, all local data in calling module are unchanged & parameter list is also unchanged CIS265/506: Chapter 06 - Recursion

11 CIS265/506: Chapter 06 - Recursion
How does it work? Compiler stores the following information in various “stacks” - so program knows where to return to & what information is valid address of current module all parameters & parameter lists any return values Definition: A stack is a data structure in which the last item added is the first item processed. CIS265/506: Chapter 06 - Recursion

12 CIS265/506: Chapter 06 - Recursion
Some pitfalls Generally tough to troubleshoot Expensive in terms of time and space Inefficient Limited use in the “real world” CIS265/506: Chapter 06 - Recursion

13 CIS265/506: Chapter 06 - Recursion
How to design... Determine the base case The statement that solves the problem Factorial: if n=0, n!=1 Determine the general case Logic needed to reduce the size of the problem Factorial: n x (n-1)! Combine these in to an algorithm CIS265/506: Chapter 06 - Recursion

14 Towers of Hanoi Example
The Towers of Hanoi example involves moving a specified number of disks from one tower to another. Legend has it that the world will end when the problem is solved for 64 disks. Problem requires 2n-1 moves, where n is the number of disks. For 3 disks: 7 moves, 10 disks: 1023 moves, 64 disks: ~1.844 x 1019 moves CIS265/506: Chapter 06 - Recursion

15 CIS265/506: Chapter 06 - Recursion
Towers of Hanoi Rules Only one disk at a time can be moved Only the top disk can be moved A larger disk can never be placed on top of a smaller disk CIS265/506: Chapter 06 - Recursion

16 CIS265/506: Chapter 06 - Recursion
Move Disks from Tower A to Tower B 3 2 1 Tower A Tower C Tower B Starting Configuration CIS265/506: Chapter 06 - Recursion

17 CIS265/506: Chapter 06 - Recursion
1 3 Tower A Tower C Tower B After 1 Move CIS265/506: Chapter 06 - Recursion

18 CIS265/506: Chapter 06 - Recursion
1 3 2 Tower A Tower C Tower B After 2 Moves CIS265/506: Chapter 06 - Recursion

19 CIS265/506: Chapter 06 - Recursion
3 1 2 Tower A Tower C Tower B After 3 Moves CIS265/506: Chapter 06 - Recursion

20 CIS265/506: Chapter 06 - Recursion
3 1 2 Tower A Tower C Tower B After 4 Moves CIS265/506: Chapter 06 - Recursion

21 CIS265/506: Chapter 06 - Recursion
3 1 2 Tower A Tower C Tower B After 5 Moves CIS265/506: Chapter 06 - Recursion

22 CIS265/506: Chapter 06 - Recursion
3 1 Tower A Tower C Tower B After 6 Moves CIS265/506: Chapter 06 - Recursion

23 CIS265/506: Chapter 06 - Recursion
3 2 1 Tower A Tower C Tower B After 7 Moves CIS265/506: Chapter 06 - Recursion

24 Towers of Hanoi - Algorithm
If n is 1 then move disk 1 from the from peg to the to peg Else Move n-1 disks from the from peg to the auxiliary peg using the to peg Move disk n from the from peg to the to peg Move n-1 disks from the auxiliary peg to the to peg using the from peg CIS265/506: Chapter 06 - Recursion

25 String Reversal & Palindromes
To find the reverse of a string: If the string contains only one character, its reverse is identical to it & we’re done Otherwise, remove & save the first character Find the reverse of the remaining string then concatenate the saved character onto the right-hand end CIS265/506: Chapter 06 - Recursion

26 CIS265/506: Chapter 06 - Recursion
Merge Sort External sort - stored in secondary memory during sort process Most of the work of an external sort is not sorting but merging A merge is the process that, given two ordered files on a given key, combine the files in to one ordered file on the same given key CIS265/506: Chapter 06 - Recursion

27 CIS265/506: Chapter 06 - Recursion
Merging 2 4 6 8 10 1 2 3 4 5 6 8 10 1 3 5 File 1 File 2 File 3 CIS265/506: Chapter 06 - Recursion

28 Merging Unordered Files
Merge Run - A series of consecutively ordered data in a file Stepdown - Occurs when the sequential ordering of a file is broken Rollout - The process of copying the consecutive series of records to the merge output file after a stepdown CIS265/506: Chapter 06 - Recursion

29 Merging Unordered Files
Three Merge Runs 2 4 16 First Merge Run A Stepdown 9 < 16 6 7 13 Second Merge Run Rollout 8 19 Third Merge Run Note this is not completely sorted - we’ll need to merge the runs again File 1 File 2 File 3

30 CIS265/506: Chapter 06 - Recursion
The Merge Process Assume we have 2300 records to sort We can only put 500 records at a time in main memory We read and sort the first 500 records, then write them to an output file Repeat this process with the remaining chunks of data until all are processed We now need a process to merge all the chunks together for (re)sorting CIS265/506: Chapter 06 - Recursion

31 CIS265/506: Chapter 06 - Recursion
Natural Merge Sorts a constant number of input merge files to one merge output file A distribution phase is required to redistribute the merge runs to the input file for remerging All merge runs are written to the same file CIS265/506: Chapter 06 - Recursion

32 CIS265/506: Chapter 06 - Recursion
Natural Merge If the file is not completely ordered, the merge runs must be distributed to two merge files between each merge phase Very Inefficient CIS265/506: Chapter 06 - Recursion

33 CIS265/506: Chapter 06 - Recursion
Balanced Merge Uses a constant number of input merge files and the same number of output merge files Generally, there are no more than four files Eliminates the distribution phase CIS265/506: Chapter 06 - Recursion

34 CIS265/506: Chapter 06 - Recursion
Balanced Merge A two-way merge requires four files Merges first merge run on file 1 with first merge run on file 2 and writes it to file 3 Merges second merge run on file 1 with second merge run on file 2 and writes it to file 4 Repeat for third, etc. merge runs. Rollout any remaining data as necessary CIS265/506: Chapter 06 - Recursion

35 CIS265/506: Chapter 06 - Recursion
Polyphase Merge Most Complex Type of Merging A constant number of input files are merged to one output file As the data in each input file are completely merged, it immediately becomes the output file and what was the output file becomes the input file CIS265/506: Chapter 06 - Recursion

36 CIS265/506: Chapter 06 - Recursion
Polyphase Merge Merges first merge run on file 1 with first merge run on file 2 and writes it to file 3 Merges second merge run on file 1 with second merge run on file 2 and writes it to file 3 Assume now that file two is empty. We can now say the first merge phase is complete Close merge file 2 and reopen it as output Close merge file 3 and open it as input Repeat as needed. CIS265/506: Chapter 06 - Recursion

37 CIS265/506: Chapter 06 - Recursion
Merge Sort in an Array The merge sort will sort an array with O(NlogN) performance. Assume you have an unsorted array of N items. Create a blank (empty) array to use as a result array. Compare A[0] & A[1], placing the smallest in Results[0], the next in Results[1]. Continue through the array. When complete, copy the results array back to the original. Do this process again, except compare the first element of a pair of elements, placing the smallest element in the Results array. Compare the first pair of the remaining elements until none are left.. Each time you do this process, the list length doubles. CIS265/506: Chapter 06 - Recursion CIS265/506: Chapter 06 - Recursion 16

38 Merge Sort - Another Example
Second pass - List Length = 4 Third pass - List Length = 8 Initial array - List Length = 1 First pass - List Length = 2 CIS265/506: Chapter 06 - Recursion CIS265/506: Chapter 06 - Recursion 17


Download ppt "Data Structures Recursion CIS265/506: Chapter 06 - Recursion."

Similar presentations


Ads by Google