CSC236 Week 4. Larry Zhang

Similar documents
CSC236 Week 5. Larry Zhang

I. Recursive Descriptions A phrase like to get the next term you add 2, which tells how to obtain

Inference rule for Induction

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Fall Recursion and induction. Stephen Brookes. Lecture 4

CS 4349 Lecture September 13th, 2017

CSC236H Lecture 5. October 17, 2018

CSC236 Week 6. Larry Zhang

What does this print?

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

THE PRINCIPLE OF INDUCTION. MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin

CSC148 Week 9. Larry Zhang

Lecture 6: Sequential Sorting

CSC148 Week 7. Larry Zhang

Lecture 4 Median and Selection

CS 4349 Lecture August 21st, 2017

This chapter covers recursive definition, including finding closed forms.

n = 1 What problems are interesting when n is just 1?

The Running Time of Programs

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

n! = 1 * 2 * 3 * 4 * * (n-1) * n

(Refer Slide Time: 01.26)

ASYMPTOTIC COMPLEXITY

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Recursively Defined Functions

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Intro. Scheme Basics. scm> 5 5. scm>

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

Lecture 3: Recursion; Structural Induction

Solving Linear Recurrence Relations (8.2)

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Unit #2: Recursion, Induction, and Loop Invariants

RECURSION, RECURSION, (TREE) RECURSION! 2

APCS-AB: Java. Recursion in Java December 12, week14 1

CS240 Fall Mike Lam, Professor. Recurrences

Discrete Structures. Fall Homework3

Divide and Conquer Algorithms

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Intro. Speed V Growth

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Math 485, Graph Theory: Homework #3

EECS 477: Introduction to algorithms. Lecture 7

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Figure 4.1: The evolution of a rooted tree.

Unit #3: Recursion, Induction, and Loop Invariants

Generating Functions

6.046 Recitation 1: Proving Correctness Bill Thies, Fall 2004 Outline

Class Note #02. [Overall Information] [During the Lecture]

Computing and Programming

CS 151. Recursion (Review!) Wednesday, September 19, 12

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

Recursive definition of sets and structural induction

Introduction to Algorithms

6.001 Notes: Section 4.1

Recursive Definitions Structural Induction Recursive Algorithms

RECURSION, RECURSION, (TREE) RECURSION! 3

Warmup Problem. Describe how structural induction differs from our MATH 135 notion of induction. 1/25

CSC148 Week 6. Larry Zhang

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 4 Notes. Class URL:

CSC263 Week 12. Larry Zhang

CSC263 Week 8. Larry Zhang.

Practice Problems for the Final

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

It is important that you show your work. There are 134 points available on this test.

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Lecture 3. Recurrences / Heapsort

Selection (deterministic & randomized): finding the median in linear time

Math 152: Applicable Mathematics and Computing

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;


Dynamic Programming Algorithms

Dynamic Programming Algorithms

The Substitution method

ANS:

Mergesort again. 1. Split the list into two equal parts

MITOCW ocw f99-lec07_300k

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Hot X: Algebra Exposed

Greedy Algorithms CLRS Laura Toma, csci2200, Bowdoin College

3. Replace any row by the sum of that row and a constant multiple of any other row.

Analyzing Complexity of Lists

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Lecture 4. The Substitution Method and Median and Selection

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Population Problems. To develop a model for population growth, To develop proficiency with algebraic equations

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

Chapter 1 Programming: A General Overview

The divide and conquer strategy has three basic parts. For a given problem of size n,

Induction Schemes. Math Foundations of Computer Science

Hints for Exercise 4: Recursion

CS 4800: Algorithms & Data. Lecture 1 January 10, 2017

Math 214 Introductory Statistics Summer Class Notes Sections 3.2, : 1-21 odd 3.3: 7-13, Measures of Central Tendency

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Analysis of Algorithms - Introduction -

Transcription:

CSC236 Week 4 Larry Zhang 1

Announcements PS2 is out Larry s office hours in the reading week: as usual Tuesday 12-2, Wednesday 2-4 2

NEW TOPIC Recursion To really understand the math of recursion, and to be able to analyze runtimes of recursive programs. 3

ConcepTest 4

Recursively Defined Functions The functions that describe the runtime of recursive programs 5

Definition of functions The usual way: define a function using a closed-form. Another way: using a recursive definition A function defined in terms of itself. 6

Let s work out a few values for T₀(n) and T₁(n) T₀(n) T₁(n) n=1 1 1 n=2 3 3 Maybe T₀(n) and T₁(n) are equivalent to each other. n=3 n=4 n=5 7 15 31 7 15 31 7

Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier to evaluate Just need n to know f(n) Instead of having to know f(n-1) to know f(n) It is easier for telling the growth rate of the function T₀(n) is clearly Θ(2ⁿ), while T₁(n) it not clear. 8

Our Goal: Given a recursively defined function, find its equivalent closed-form function 9

Method #1 Repeated Substitution 10

Repeated substitution: Steps Step 1: Substitute a few time to find a pattern Step 2: Guess the recurrence formula after k substitutions (in terms of k and n) For each base case: Step 3: solve for k Step 4: Plug k back into the formula (from Step 2) to find a potential closed form. ( Potential because it might be wrong) Step 5: Prove the potential closed form is equivalent to the recursive definition using induction. 11

Example 1 12

Step 1 Substitute a few times to find a pattern. Substitute T(n-1) with T(n-2): Substitute T(n-2) with T(n-3): Substitute T(n-3) with T(n-4): 13

Step 2: Guess the recurrence formula after k substitutions The guess: 14

Step 3: Consider the base case What we have now: So, let n - k = 1, and solve for k We want this to be T(1), because we know clearly what T(1) is. k = n - 1 This means you need to make n-1 substitutions to reach the base case n=1 15

Step 4: plug k back into the guessed formula Guessed formula: For Step 3, we got k = n - 1 Substitute k back in, we get... This is the potential closed-form of T(n). 16

Step 5: Drop the word potential by proving it Prove Use induction! Define predicate: P(n): T₁(n) = T₀(n) Base case: n=1 Induction step: is equivalent to # by def of T(n) # by I.H. 17

We have officially found that the closed-form of the recursively defined function is 18

Repeated substitution: Steps Step 1: Substitute a few time to find a pattern Step 2: Guess the recurrence formula after k substitutions (in terms of k and n) For each base case: Step 3: solve for k Step 4: Plug k back into the formula (from Step 2) to find a potential closed form. ( Potential because it might be wrong) Step 5: Prove the potential closed form is equivalent to the recursive definition using induction. 19

ConcepTest 20

Summary Find the closed form of a recursively defined function Method #1: Repeated substitution It s nothing tricky, just follow the steps! Take care of all base cases! It s a bit tedious sometimes, we will learn faster methods later. 21

Handout, Exercise 1 22

Handout, Exercise 2 23

Next Example Sometimes you re not given the recursive definition directly, so you need to develop the recursive definition first 24

Example Give a recursive definition of the number of 2-element subsets of n elements. (Pretend that you never knew n choose 2, and let s develop it from first principles). For example, when n=4, we have 4 elements, e₁, e₂, e₃, e₄, the 2-element subsets are (e₁,e₂), (e₁,e₃), (e₁,e₄), (e₂,e₃), (e₂,e₄), (e₃,e₄) There are 6 of them. 25

Strategy 1. Find the base case(s) that can be evaluated directly 2. Find a way to break the larger problem to smaller problems, so that you can define f(n) in terms of f(m) for some m < n. 26

Give a recursive definition of f(n): The number of 2-element subsets of n elements. 1. Base case: n = 0 f(0) = 0 27

Give a recursive definition of f(n): The number of 2-element subsets of n elements. 2. Break the larger problem to smaller ones: For the set of n elements, how many 2-element subsets does it have? Our set looks like: Break all subsets of S into two parts the subsets that contain en the subsets that do NOT contain en 28

The number of 2-element subsets of n elements. The 2-element subsets that contain en How many? Two elements One has to be en The other can be e1, e2,, en-1 So there are n-1 of such subsets! 29

The number of 2-element subsets of n elements. The 2-element subsets that do NOT contain en How many? They are subsets of The number of 2-element subsets of n-1 elements It s f(n-1)! 30

Sum up after breaking the larger problem into smaller problems The number of 2-element subsets of n elements number of subsets with en: n-1 number of subsets without en: f(n-1) 31

Combining with the base case, the final developed recurrence: f(n): the number of 2-element subsets of n elements. Home exercise: find its closed form using the substitution method. You know what the result should be. 32

ConcepTest Try this: write an efficient program that computes the number of ways to pick up the marbles given the number of marbles in the row. 33