1 CSE 20 Lecture 11 Function, Recursion & Analysis CK Cheng UC San Diego.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
COSC 2006 Data Structures I Recursion III
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CSE 20 Lecture 11 Function, Recursion & Analysis (Ch. 6 Shaum’s ) May 3, 2011.
Recursion.
Analysis of Recursive Algorithms
Recursion. Recursive Definitions In recursive definitions, we define a function, a predicate, a set, or a more complex structure over an infinite domain.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Analysis of Recursive Algorithms October 29, 2014
Chapter 1 Algorithm Analysis
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
CSE 20 Lecture 12 Induction CK Cheng 1. Induction Outlines Introduction Theorem Examples: The complexity calculation – Tower of Hanoi – Merge Sort – Fibonacci.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Data Structures and Abstractions with Java, 4e Frank Carrano
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
R. Johnsonbaugh Discrete Mathematics 5 th edition, 2001 Chapter 5 Recurrence Relations.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 CSE 20 Lecture 13: Analysis of Recursive Functions CK Cheng.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
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.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Questions 4) What type of algorithmic problem-solving technique (greedy, divide-and-conquer, dynamic programming)
Recursion Continued Divide and Conquer Dynamic Programming.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Analysis of Algorithms CS 477/677
Fundamentals of Algorithms MCS - 2 Lecture # 11
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Introduction to Computer Science - Alice
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Proving correctness.
CS1120: Recursion.
Divide and Conquer (Merge Sort)
Recursion Data Structures.
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Presentation transcript:

1 CSE 20 Lecture 11 Function, Recursion & Analysis CK Cheng UC San Diego

Motivation Verification Complexity of the execution 2 Input x Output y y= Function (x) Recursion

OUTLINE Definition of Function Formulation of Recursive Functions Induction (Verification) Counting (Analysis) 3

4 I. Definition A function f: A → B maps elements in domain A to codomain B such that for each a ϵ A, f(a) is exact one element in B. f: A → B A: Domain B: Codomain f(A): range or image of function f(A) ⊂ B

5 Examples (2) f(x) (1) f(x)=x 2, x ∈ ℝ (3) f(x) NOT a function x f(x) x

6 (5) NOT a function Examples (4) Mapping from x to y When domain A is an integer set Z, we may denote f(x) as f x, ie. f 0, f 1, f 2.

Function: iClicker 7 Let X={1,2,3,4}. Determine which of the following relation is a function A. f={(1,3), (2,4), (3,3)} B. g={(1,2), (3,4), (1,4), (2,3)} C. h={(1,4), (2,3), (3,2), (4,1)} D. w={(1,1), (2,2), (3,3), (4,4)} E. Two of the above.

Formulation of Recursive Functions 8 1.Overview of Recursive Functions 2.Examples 1.Fibonacci Sequence 2.The Tower of Hanoi 3.Merge Sort

Formulation of Recursive Function: overview 9 1.Recursion: A function calls itself. 2.Reduction: For each call, the problem size becomes smaller. 3.Base Case: The smallest sized problem. The derivation of the base case is defined.

Recursive Functions: iClicker 10 A. We can convert a recursive function into a program with iterative loops. B. Recursive function takes much less lines of codes for some problems. C. Recursive function always runs slower due to its complexity. D. Two of the above E. None of the above.

11 Fibonacci Sequence: Problem Fibonacci sequence: Start with a pair of rabbits. For every month, each pair bears a new pair, which becomes productive from their second month. Calculate the number of new pairs in month i, i≥0.

12 Fibonacci Sequence: Algorithm Algorithm array n(i) (# new born pairs), a(i) (# adult pairs) Init n(0)=0, a(0)=1 For i=1, i=i+1, i< #MaxIter {n(i)=a(i-1) a(i)=a(i-1)+n(i-1) } Index: … a(i): … n(i): …

13 Fibonacci Sequence: iClicker The following is Fibonacci sequence: A B C D E. None of the above

14 Fibonacci Sequence: Recursion Function Fibonacci(n) If n< 2, return n Else return(Fibonacci(n-1)+Fibonacci(n-2)) Index: … F(i): …

15 Fibonacci Sequence: Complexity Fibonacci Sequence Index Sequence

16 Fibonacci sequence: Golden Ratio Derivation: Let We have:

17 Fibonacci Sequence and the golden ratio

Tower of Hanoi: Problem Setting: Three poles and a stack of disks on the leftmost pole in order of size with the largest at bottom. Obj: Move the entire pile of disks to the rightmost pole. Rules: – Each move involves only one top disk on one of three poles. – The top disk may be moved to either of the other two poles. – A disk must never be on top of a smaller disk. 18

Tower of Hanoi: Problem Hanoi (4, 1, 3): Move 4 disks from pole 1 to pole 3 19

Tower of Hanoi: Function Setting: n, A=1, B=3 (move n disks from pole1 to pole 3) Output: a sequence of moves (a, b) Function Hanoi(n, A, B) If n=1, return (A, B), Else {C <= other pole(not A nor B) return Hanoi(n-1, A, C), (A, B), Hanoi(n-1, C, B) } 20

Tower of Hanoi: Example Function Hanoi(n, A, B) If n=1, return (A, B), Else {C<= other pole(not A nor B) return Hanoi(n-1, A, C), (A, B), Hanoi(n-1, C, B) } Example: Hanoi(2, 1, 3) =Hanoi(1, 1, 2), (1, 3), Hanoi(1, 2, 3) =(1,2), (1,3), (2,3) 21

Tower of Hanoi: Complexity Number of moves for Hanoi(n, A, B) F(1)= 1 F(n) = 2F(n-1) n F(n)

Merge Sort: Problem Given an array of numbers, sort the numbers from small to large. 23 id items

Merge Sort: Function Initial: Array A from p=0 to r=n-1 Function Merge-Sort(A, p, r) If p < r Then { q= Floor((p+r)/2) Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) } 24

Merge Sort: Function Function Merge(A, p, q, r) Create and copy arrays L=A[p, q], R=A[q+1, r] Set initial i=j=0, L[q-p]=R[r-q]=inf for k=p to r {if L[i]≤R[j] then A[k]=L[i] i=i+1 else A[k]=R[j] j=j+1 } 25

Merge Sort: Complexity Complexity: Let F(n) be the number of L[i]≤R[j] comparisons in Merge-Sort(A, 0, 2 n -1). F(0)= 0 F(1)= 2 F(2)= 2F(1)+2 2 F(n)= 2F(n-1)+2 n 26