1 / 27

Chapter 10: Recursion

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

shumatem
Download Presentation

Chapter 10: Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. Self Study Recursive Function to Extract Capital Letters from a String caps is the return variable str is the input

  6. Self Study Trace of Call to Recursive Function find_caps

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

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

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

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

  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. 1 2 3 B C 3 Disk Towers Of Hanoi A 0 disk moves in total

  18. 2 3 1 B C 3 Disk Towers Of Hanoi A 1 disk moves in total

  19. 3 2 1 B C 3 Disk Towers Of Hanoi A 2 disk moves in total

  20. 1 3 2 B C 3 Disk Towers Of Hanoi A 3 disk moves in total

  21. 1 2 3 B C 3 Disk Towers Of Hanoi A 4 disk moves in total

  22. 1 2 3 B C 3 Disk Towers Of Hanoi A 5 disk moves in total

  23. 2 1 3 B C 3 Disk Towers Of Hanoi A 6 disk moves in total

  24. 1 2 3 B C 3 Disk Towers Of Hanoi A 7 disk moves in total

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

  26. 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)

  27. Thank You

More Related