270 likes | 292 Views
Chapter 10: Recursion. CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain. 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
E N D
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain
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.
Self Study Recursive Function to Extract Capital Letters from a String caps is the return variable str is the input
Self Study Trace of Call to Recursive Function find_caps
Self Study Sequence of Events for Trace of Call to find_caps from printf Statements
Selection Sort • http://www.ece.unb.ca/brp/lib/java/selectionsort/ • http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html
Selection Sort 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; } #include <stdio.h> 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); } Output: 3 4 5 7 9
Recursive Set Operations on Sets Represented as Character Strings Self Case Study Section: 10.5
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.
Towers of Hanoi Problem (cont’d) • Constraints • Only one disk may be moved at a time. • 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. • No disk may be placed on top of a smaller disk.
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
1 2 3 B C 3 Disk Towers Of Hanoi A 0 disk moves in total
2 3 1 B C 3 Disk Towers Of Hanoi A 1 disk moves in total
3 2 1 B C 3 Disk Towers Of Hanoi A 2 disk moves in total
1 3 2 B C 3 Disk Towers Of Hanoi A 3 disk moves in total
1 2 3 B C 3 Disk Towers Of Hanoi A 4 disk moves in total
1 2 3 B C 3 Disk Towers Of Hanoi A 5 disk moves in total
2 1 3 B C 3 Disk Towers Of Hanoi A 6 disk moves in total
1 2 3 B C 3 Disk Towers Of Hanoi A 7 disk moves in total
How many moves • For 3 pegs: 2n-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 = (2100-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)