CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
TA SURVEY Have the TA write their name on the board Fill out the survey at: The TA should walk out 5 minutes.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Dynamic Programming David Kauchak cs161 Summer 2009.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CPS120: Introduction to Computer Science Sorting.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion Powerful Tool
Introduction to Algorithms: Divide-n-Conquer Algorithms
Using recursion for Searching and Sorting
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Version 1.0.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
CS 3343: Analysis of Algorithms
Tutorial 8 Pointers and Strings
CS1010 Discussion Group 11 Week 9 – Pointers.
Chapter 4 Divide-and-Conquer
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Chapter 14: Recursion Starting Out with C++ Early Objects
Algorithm Analysis CSE 2011 Winter September 2018.
CS 3343: Analysis of Algorithms
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 12 Recursion (methods calling themselves)
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS201: Data Structures and Discrete Mathematics I
Digital Logic & Design Lecture 02.
Recursion.
A Robust Data Structure
CSE 373 Data Structures and Algorithms
Recursion Chapter 11.
Divide and Conquer Algorithms Part I
Basics of Recursion Programming with Recursion
Algorithm Efficiency and Sorting
Recursion Chapter 11.
Dr. Sampath Jayarathna Cal Poly Pomona
Last Class We Covered Recursion Stacks Parts of a recursive function:
CS1100 Computational Engineering
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 12.
CS203 Lecture 15.
ITEC324 Principle of CS III
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….

Slides are at http://www.comp.nus.edu.sg/~yanhwa/ HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

PE 2 Saturday T_T

Lab 4 Frog Computation function jump should not printf(“Invalid move!”). It should return -1 to main() if jump is invalid and let main do the printing How to reduce repeated code and combine left facing frog and right facing frog?

Lab 4 Frog

for (i = 0; i < size; i++) { scanf("%s", mod[i]); } Lab 4 Array of strings? char mod[][MAX_LEN] for (i = 0; i < size; i++) { scanf("%s", mod[i]); }

Use anything! (kidding) Lab 4 Elevator Use a structure! Use array of strings! Use arrays! Use anything! (kidding)

Divide and conquer paradigm Lecture Summary Recursion Divide and conquer paradigm At first you will learn by tracing: Winding and Unwinding. Experience the magic. Assume nothing. Later, you will learn to accept that the magic can only be fully understood if you assume that it works and you only need to care about the base case and how it combines correct answer(s) from previous calls to get the a new correct answer for this call!

Recursion is like (strong) maths induction. Lecture Summary Recursion Recursion is like (strong) maths induction. Understand why the base case is as such. Assume that fib(k) is correct for k < n Thus we assume fib(n-1) and fib(n-2) is correct. Now that we got two correct answers for the smaller problems, how do we combine them to solve the larger problem of fib(n)? Recurrence relation: fn = fn-1 + fn-2 n≥ 2 f0 = 0 f1 = 1

Lecture Summary Recursion Do you understand how recursion makes use of the function call stack for it to work? Stack overflow Very important to make sure your base case(s) can be reached eventually and not calling itself forever. https://www.youtube.com/watch?v=PORo1ut9kMs

Recursion May not be efficient (esp due to duplicate computation) Lecture Summary Recursion May not be efficient (esp due to duplicate computation) Function call overhead (calling the function, storing the activation record)

Tutorial 1a Trace from bottom-up for such questions to reduce chance of mistakes. f(0) -> 0 f(1) -> 2 * 1 + f(0) -> 2 f(2) -> 2 * 2 + f(1) -> 2 + 4 = 6 f(3) -> 2 * 3 + f(2) -> 6 + 6 = 12 f(4) -> 2 * 4 + f(3) -> 12 + 8 = 20 f(5) -> 2 * 5 + f(4) -> 20 + 10 = 30

Tutorial 1c What does the function compute? Trace the function?

Summing digits in an integer Tutorial 2 Summing digits in an integer What should be the base case? n < 10 return n (only have one digit left) At each call we return the “last digit” And combine with the sum of the rest of the digits

What is the smaller problem? Tutorial 3 What is the smaller problem? At this call, I see the last element and compare it with m. I decide whether to return the last element or m. What is m? m is the largest element for the rest of the array Thus it finds the maximum value of the array.

Combine qn 2 and qn 3 together Tutorial 4

What should we do at each call? Tutorial 5 North east paths What should we do at each call? We should decide which direction to go. When do we have the base case? When do we only have one path left?

Tutorial 5

Tutorial 6 Auxiliary function

Tutorial 7

Tutorial 7

What should I assume is already done for me? Tutorial 8 What is the base case? What should I assume is already done for me? What should I do at this step? How to combine the answers from prev step with this step? At each step I need to fill the row and column!

Tutorial 9 I will compare the mid value every time with the value we want to find How to know what is the mid and know where I am searching? I have “arrows” variables that store the indexes of the “start” and “end” So I need extra information… = auxiliary function Since for binary search we assume the array to be already sorted, if the value we want to find is larger than mid we only need to search at the part of the array mid+ 1 to end and can ignore the rest. = our recursive call Same for if value is smaller than mid

Tutorial 9