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

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Sorting.
Introduction to Recursion and Recursive Algorithms
IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Sorting (2nd part) & Binary Search Tree
Recitation 8 Recursion and Linked Lists Ben Stoddard Tabitha Peck March 7, 2014.
Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Recursion.
CMPS1371 Introduction to Computing for Engineers SORTING.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CHAPTER 11 Sorting.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Inventing A Really Bad Sort: It Seemed Like A Good Idea At The Time Jim Huggins Kettering University
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Tutorial 7 Sorting & Complexity Analysis (Almost) always asked in exam! e.g. a. Derive algorithm for this problem! b. What is the time complexity of your.
Tutorial 5 Stack & Queue, Midtest. Last In First Out (LIFO) Stack implemented using Array with top pointer –
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
What is recursion? Imagine checking the dictionary for “apple”  a round fruit with a firm white flesh and a green, red or yellow skin  the usually sweet-tasting.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Recursion, Complexity, and Sorting By Andrew Zeng.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
UNIT 17 Recursion.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Pointers OVERVIEW.
Tutorial 4 Linked List, Stack, & Queue. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Tutorial 10 Heap & Priority Queue. Heap – What is it? Special complete binary tree data structure –Complete BT: no empty node when checked top-down (level.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
Programming Appreciation Camp Session 2: Recursion Steven Halim NUS School of Computing.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Tutorial 3 - Linked List. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
The Linear and Binary Search and more Lecture Notes 9.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Recursion Powerful Tool
Searching and Sorting Searching algorithms with simple arrays
CS 1321.
Lecture 4 Divide-and-Conquer
Sub-Quadratic Sorting Algorithms
Algorithms: Design and Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

Tutorial 6 Recursion

Sierpinski Triangle Recursive Tree Koch Snowflake Beautiful Recursion: Fractal

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.

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

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); }

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

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? – 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 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)

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.