Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain
2 Day 2 (Recursion cont’d) What we know from the previous class To be recursive, the problem must be divisible and should have: Starting Terminating condition Progress How to trace a recursive function What we don’t know A recursive function uses stack to store local variables, program counter, etc. before it calls itself again. All the local information about the function are stored on the top of the stack of the program.
3 Recursive Function gcd
4 Program Using Recursive Function gcd (cont’d)
5 Recursive Function to Extract Capital Letters from a String caps is the return variable str is the input Self Study
6 Trace of Call to Recursive Function find_caps Self Study
7 Sequence of Events for Trace of Call to find_caps from printf Statements Self Study
8 Selection Sort 41/samples/sort/Sort2-E.html 41/samples/sort/Sort2-E.html
9 Selection Sort #include void RecursiveSelectionSort (int A[ ], int start, int end){ int j; int IndexOfMin; int Min; if (start == end) return; IndexOfMin = start; Min = A[start]; for (j = start + 1; j<=end; j++){ if (Min > A[j]){ Min = A[j]; IndexOfMin = j; } if (start != IndexOfMin){ A[IndexOfMin] = A[start]; A[start] = Min; } RecursiveSelectionSort(A, start + 1, end); } int main(){ int a[5] = {5, 4, 3, 7, 9}; RecursiveSelectionSort(a, 0, 4); for (int i=0; i< 5; i++){ printf(" %d", a[ i ]); } return 0; } Output:
10 Recursive Set Operations on Sets Represented as Character Strings Self Case Study Section: 10.5
11 Towers of Hanoi Problem It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, smallest at the top, thus making a conical shape.
12 Towers of Hanoi Problem (cont’d) Constraints 1.Only one disk may be moved at a time. 2.Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg. 3.No disk may be placed on top of a smaller disk.
13 Towers of Hanoi After Steps 1 and 2
14 Towers of Hanoi After Steps 1, 2, 3.1, and 3.2
15 Recursive Function tower Two Recursive parts with n>1: Move top (n-1) disks to the auxilary peg Move one disk from from_peg to to_peg Move (n-1) disks from aux_peg to to_peg
16 Trace of tower ('A', 'C', 'B', 3);
17 3 Disk Towers Of Hanoi A BC disk moves in total
18 3 Disk Towers Of Hanoi A BC disk moves in total
19 3 Disk Towers Of Hanoi A BC disk moves in total
20 3 Disk Towers Of Hanoi A BC disk moves in total
21 3 Disk Towers Of Hanoi A BC disk moves in total
22 3 Disk Towers Of Hanoi A BC disk moves in total
23 3 Disk Towers Of Hanoi A BC disk moves in total
24 3 Disk Towers Of Hanoi A BC disk moves in total
25 Output Generated by tower('A', 'C', 'B', 3);
26 How many moves For 3 pegs: 2 n -1 For 3 disks: 7 moves Consider, each move takes 1 ms. What is the time required to solve Tower of Hanoi problem if there are 100 disks? Solution: t = ( ) ms = ms = Sec = min = hours = days = years = billion years (Our universe itself is only 13.7 billion years old)
27 Thank You