Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 6 Recursion. Sierpinski Triangle Recursive Tree Koch Snowflake Beautiful Recursion: Fractal.

Similar presentations


Presentation on theme: "Tutorial 6 Recursion. Sierpinski Triangle Recursive Tree Koch Snowflake Beautiful Recursion: Fractal."— Presentation transcript:

1 Tutorial 6 Recursion

2 Sierpinski Triangle Recursive Tree Koch Snowflake Beautiful Recursion: Fractal

3 More Examples Droste Effect Russian Doll Recursive Acronyms –PHP: PHP Hypertext Preprocessor –GNU: GNU’s Not Unix –VISA: VISA International Service Association A “Definition” in an English-English dictionary –Recursion See “Recursion”. If you are tired, stop.

4 Recursion: Basic Idea Template for ANY recursion: Recursive_Function_Name(Parameter_List) // you will always see “selection statement” (if, switch, etc) if (Base Case) do something simple else // Recursive Case Recursive_Function_Name(Modified Parameter_List); Modified Parameter_List must bring the recursive function closer to the base case (simplified problem).

5 Example How many ways to choose k items out of n items? How many ways to choose when k < 0, e.g. (k=-1) out of (n=3)? –0, actually this problem is undefined for k < 0…, such case will never exist. How many ways to choose when k > n, e.g. (k=4) out of (n=3)? –0, also undefined, impossible… How many ways to choose when k == n, e.g. (k=3) out of (n=3)? –1, take all How many ways to choose when k = 0, e.g. (k=0) out of (n=3)? –1, do not take anything How many ways to choose in general case, e.g. k 0: (k=2) out of (n=3)? –Either I take one item X, My problem becomes choosing k-1 (k=1) out of n-1 (n=2) –Plus if I do not take that one item X My problem becomes choosing k (k=2) out of n-1 (n=2) Code it: int c(int n,int k) { if (k>n) // assume k<0 is undefined return 0; if (k == n || k == 0) return 1; return c(n-1,k-1) + c(n-1,k); }

6 Recursion is Powerful Divide and Conquer Dynamic Programming Used in future lectures in CS1102: –Sorting: Mergesort and Quicksort are recursive –Tree/Heap/Graph: these data structures and operations are naturally recursive Can also be used for past CS1102 materials: –Linked List data structure and operations are also naturally recursive

7 Student Presentation Gr3MainBackup 1.Cao Hoang DangCai Jingfang, Du Xin 2.Chng Jiajie Ding Ying Shiaun 3.Chen Tianni Rebecca Jashan Deep Kaur 4.Huang ChuanxianLeow Wei Jie, Lim Wei Hong Gr4MainBackup 1.Tan Peck LuanTan Shu Yu Cynthia 2.Li YawenTan Miang Yeow 3.Wang KangAhmed Shafeeq 4.Wong Suet Teng, MChong Tze Koi Overview of the questions: 1.Trace IsEven/IsOdd 2.Recursive counting 3.Recursive sort 4.Reversing BasicLinkedList The rabbit question is your homework… Gr5MainBackup 1.Wu ShujunJoyeeta Biswas 2.Teo Sim Yee, Stephanie Tan Yan Hao 3.Wang RuohanZheng Yang 4.Liu Na Zhang Denan Gr6MainBackup 1.Rasheilla Bte RajahTan Ping Yang 2.Lou Wei Chen Gerard JKoh Jye Yiing 3.Zhang ChaoWong Shiang Ker 4.Gan Zhi Wei JamesKuganeswari D/O K 7

8 Q1: Trace boolean isEven(int number) { if (number == 0) return true; else return isOdd(number-1); } boolean isOdd(int number) { if (number == 0) return false; else return isEven(number-1); } Trace: isEven(5)! Trace: isOdd(1), isEven(2)! –There are repeated sub-problems! –You will learn how to optimize this situation in more advanced modules! Are these two functions recursive? –http://en.wikipedia.org/wiki/Mutual_recursionhttp://en.wikipedia.org/wiki/Mutual_recursion In what situation(s) these two functions may fail? –Negative numbers How to improve these codes to address the situation above? –If input is negative, multiply it with -1 first.

9 Q2: Recursive Counting Adam can climb: –1 stair per step, or –2 stairs per step Given n stairs, Count how many ways Adam can go up From stair 0 (ground) to stair n! n = Thinking steps: –Let steps(n): the number of ways –Start from small cases steps(0), not defined, assume n>0 steps(1), 1 way (1 step) steps(2), 2 ways (1+1 or 2) steps(3), 3 ways (1+1+1, 2+1, or 1+2) steps(4), 5 ways ( , 2+1+1, 1+2+1, or 1+1+2, 2+2) –Feel that this problem is recursive! –Go backwards! There are two ways to reach stair n From stair n-2, then Adam +2 stairs, or From stair n-1, then Adam +1 stair Naturally: –steps(n) = steps(n-1)+steps(n-2)

10 Q3: Recursive Sort Many options We will learn this again in details in Lecture 6 (Sorting) Possible answers: –Recursive bubble sort –Recursive selection sort –Recursive merge sort –Recursive quick sort Recursive selection sort: sort(array, size) if (size == 1) return; // done (base case) pass through the array one time to get the maximum item M; swap M with array[size-1]; call sort(array, size-1); // recursive

11 Q4: Reversing BLL Recursively Original BLL: –A  B  C  D –Head is A Reversed BLL: –A  B  C  D –Head is D Given a BLL, reverse it, recursively! Possible answer: node reverse(node n) { node newhead; if (n.next != null) { newhead = reverse(n.next); n.next.next = n; // change arrow! n.next = NULL; } else // base case, this is old tail! newhead = n; // now: new head return newhead; } Call with: head = reverse(head);

12 Food For Thought If you are asked to design a recursive function to model your life while taking CS1102, what kind of function that you will design? void CS1102_Life(int week_ID) { do_tutorial_and_lab(week_ID); if (next_week) CS1102_Life(week_ID+1); } Wrong, this is infinite… void CS1102_Life(int week_ID) { do_tutorial_and_lab(week_ID); if (week_ID != end of semester) CS1102_Life(week_ID+1); } It is going to be over

13 Midterm Test If you want to know the truth… Visit my website I have scanned my answers and upload the PDF there.


Download ppt "Tutorial 6 Recursion. Sierpinski Triangle Recursive Tree Koch Snowflake Beautiful Recursion: Fractal."

Similar presentations


Ads by Google