140 likes | 400 Views
Tutorial 6 Recursion. Beautiful Recursion: Fractal. Koch Snowflake http://en.wikipedia.org/wiki/Fractal. Sierpinski Triangle Recursive Tree. More Examples. Droste Effect http://en.wikipedia.org/wiki/Droste_effect Russian Doll http://en.wikipedia.org/wiki/Matryoshka_doll
E N D
Beautiful Recursion: Fractal • Koch Snowflake http://en.wikipedia.org/wiki/Fractal • Sierpinski Triangle • Recursive Tree
More Examples • Droste Effect http://en.wikipedia.org/wiki/Droste_effect • Russian Doll http://en.wikipedia.org/wiki/Matryoshka_doll • Recursive Acronyms • PHP: PHP Hypertext Preprocessor • GNU: GNU’s Not Unix • VISA: VISA International Service Association http://en.wikipedia.org/wiki/Recursive_acronym • A “Definition” in an English-English dictionary • Recursion • See “Recursion”. • If you are tired, stop.
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).
ExampleHow 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 < n, 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(intn,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);}
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
Student Presentation • Gr3 Main Backup • Cao Hoang DangCai Jingfang, Du Xin • Chng Jiajie Ding Ying Shiaun • Chen Tianni Rebecca Jashan Deep Kaur • Huang Chuanxian Leow Wei Jie, Lim Wei Hong • Gr4 Main Backup • Tan Peck Luan Tan Shu Yu Cynthia • Li Yawen Tan Miang Yeow • Wang Kang Ahmed Shafeeq • Wong Suet Teng, M Chong Tze Koi Overview of the questions: • Trace IsEven/IsOdd • Recursive counting • Recursive sort • Reversing BasicLinkedList The rabbit question is your homework… • Gr5 Main Backup • Wu Shujun Joyeeta Biswas • Teo Sim Yee, Stephanie Tan Yan Hao • Wang Ruohan Zheng Yang • Liu Na Zhang Denan • Gr6 Main Backup • Rasheilla Bte Rajah Tan Ping Yang • Lou Wei Chen Gerard J Koh Jye Yiing • Zhang Chao Wong Shiang Ker • Gan Zhi Wei James Kuganeswari D/O K
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_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.
Q2: Recursive Counting • Adam can climb: • 1 stair per step, or • 2 stairs per step • Given n stairs,Count how many waysAdam can go upFrom stair 0 (ground) to stair n! • n = 4 • 1+1+1+1 • 1+1+2 • 1+2+1 • 2+1+1 • 2+2 • 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, or1+2) • steps(4), 5 ways(1+1+1+1, 2+1+1, 1+2+1, or1+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)
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
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);
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
Midterm Test • If you want to know the truth… • Visit my website • I have scanned my answers and upload the PDF there.