Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.

Similar presentations


Presentation on theme: "Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain."— Presentation transcript:

1 Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain

2 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 3 Recursive Function gcd

4 4 Program Using Recursive Function gcd (cont’d)

5 5 Recursive Function to Extract Capital Letters from a String caps is the return variable str is the input Self Study

6 6 Trace of Call to Recursive Function find_caps Self Study

7 7 Sequence of Events for Trace of Call to find_caps from printf Statements Self Study

8 8 Selection Sort  http://www.ece.unb.ca/brp/lib/java/selectionsort/ http://www.ece.unb.ca/brp/lib/java/selectionsort/  http://www.cs.oswego.edu/~mohammad/classes/csc2 41/samples/sort/Sort2-E.html http://www.cs.oswego.edu/~mohammad/classes/csc2 41/samples/sort/Sort2-E.html

9 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: 3 4 5 7 9

10 10 Recursive Set Operations on Sets Represented as Character Strings Self Case Study Section: 10.5

11 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 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 13 Towers of Hanoi After Steps 1 and 2

14 14 Towers of Hanoi After Steps 1, 2, 3.1, and 3.2

15 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 16 Trace of tower ('A', 'C', 'B', 3);

17 17 3 Disk Towers Of Hanoi A BC 3 2 1 0 disk moves in total

18 18 3 Disk Towers Of Hanoi A BC 3 2 1 1 disk moves in total

19 19 3 Disk Towers Of Hanoi A BC 321 2 disk moves in total

20 20 3 Disk Towers Of Hanoi A BC 32 1 3 disk moves in total

21 21 3 Disk Towers Of Hanoi A BC 32 1 4 disk moves in total

22 22 3 Disk Towers Of Hanoi A BC 321 5 disk moves in total

23 23 3 Disk Towers Of Hanoi A BC 3 2 1 6 disk moves in total

24 24 3 Disk Towers Of Hanoi A BC 3 2 1 7 disk moves in total

25 25 Output Generated by tower('A', 'C', 'B', 3);

26 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 = (2 100 -1) ms = 1267650600228229401496703205375 ms = 1267650600228229401496703205.375 Sec = 21127510003803823358278387 min = 352125166730063722637973 hours = 14671881947085988443249 days = 40196936841331475186 years = 40196936841 billion years (Our universe itself is only 13.7 billion years old)

27 27 Thank You


Download ppt "Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain."

Similar presentations


Ads by Google