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

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

Recursive Definitions Structural Induction Recursive Algorithms

Recursively Defined Functions

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Inference rule for Induction

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

CS 220: Discrete Structures and their Applications. Recursive objects and structural induction in zybooks

10/9/17. Using recursion to define objects. CS 220: Discrete Structures and their Applications

Chapter Summary. Recursively defined Functions. Recursively defined sets and Structures Structural Induction

Mathematical Induction

Mathematical Induction

Recursive Definitions and Structural Induction

5.3 Recursive definitions and structural induction

Announcements. CS243: Discrete Structures. Strong Induction and Recursively Defined Structures. Review. Example (review) Example (review), cont.

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

CSE 20 DISCRETE MATH WINTER

Midterm solutions. n f 3 (n) = 3

Recursion vs Induction

CS201 Discussion 7 MARKOV AND RECURSION

Reading for this lecture (Goodrich and Tamassia):

Divide and Conquer Algorithms

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Glynda, the good witch of the North

AXIOMS FOR THE INTEGERS

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Binary Search to find item in sorted array

Recursive definition of sets and structural induction

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Analyzing Complexity of Lists

More Complicated Recursion CMPSC 122

Phil 320 Chapter 1: Sets, Functions and Enumerability I. Sets Informally: a set is a collection of objects. The objects are called members or

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Multiple-choice (35 pt.)

This chapter covers recursive definition, including finding closed forms.

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

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Solutions to the Second Midterm Exam

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

recursive algorithms 1

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Lecture 3.4: Recursive Algorithms

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

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Reduction & Recursion Overview

Discrete Mathematics. Kruskal, order, sorting, induction

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

7. Tile That Courtyard, Please

Lecture 15 : Review DRAFT

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Figure 4.1: The evolution of a rooted tree.

Recursion. Chapter 7

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 15: Recursion

DVA337 HT17 - LECTURE 4. Languages and regular expressions

Discrete Structures. Fall Homework3

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSC236H Lecture 5. October 17, 2018

Reasoning and writing about algorithms: some tips

Computer Science 236 Fall Nov. 11, 2010

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

Recursion and Structural Induction

Backtracking. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. The Eight Queens Problem. Chapter 5

Recursion and Induction

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

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

Lecture 9: Sorting Algorithms

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

The Hunt-Szymanski Algorithm for LCS

Power Set of a set and Relations

Recursion I and II. Discrete Structures (CS 173) Madhusudan Parthasarathy, University of Illinois 1

EK131 E5 Introduction to Engineering

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

PDF created with pdffactory Pro trial version Recursion

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSCI 121: Recursive Functions & Procedures

Analysis of Recursive Algorithms

Complexity of Algorithms

4&5 Binary Operations and Relations. The Integers. (part I)

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Reading 8 : Recursion

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

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

Algorithm Analysis and Design

Lecture 6,

Lessons on Python Functions

CSC236 Week 4. Larry Zhang

Slides for Faculty Oxford University Press All rights reserved.

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

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Backtracking. Module 6. The Eight Queens Problem. CS 147 Sam Houston State University Dr. McGuire. Backtracking. Recursion Revisited

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

Chapter 1 Programming: A General Overview

Transcription:

Section 5.3 1 Recursion Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences Previously sequences were defined using a specific formula, e.g., a n = 2 n for n = 0,1,2,... This sequence can also be defined by giving the first term of the sequence, namely a 0 = 1, and a rule for finding a term of the sequence for the previous one, namely, a n+1 = 2a n for n = 0,1,2,... 2 1

Recursively Defined Functions A recursive or inductive definition of a function consists of two steps. BASIS STEP: Specify the value of the function at zero. RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers. A function f(n) is the same as a sequence a 0, a 1,, where a i, where f(i) = a i. This was done using recurrence relations in Section 2.4. 3 Fibonacci Numbers Example : The Fibonacci numbers are defined as follows: f 0 = 0 f 1 = 1 f n = f n 1 + f n 2 Find f 2, f 3, f 4, f 5. Solution : f 2 = f 1 + f 0 = 1 + 0 = 1 f 3 = f 2 + f 1 = 1 + 1 = 2 f 4 = f 3 + f 2 = 2 + 1 = 3 f 5 = f 4 + f 3 = 3 + 2 = 5 2

Recursively Defined Functions Example: Give a recursive definition of the factorial function n! (n is a nonnegative integer): Solution: f(0) = 1 f(n + 1) = (n + 1) f(n) 5 Recursively Defined Sets and Structures Recursive definitions of sets have two parts: basis step: specifies an initial collection of elements. recursive step: gives the rules for forming new elements in the set from those already known to be in the set. Sometimes the recursive definition has an exclusion rule, which specifies that the set contains nothing other than those elements specified in the basis step and generated by applications of the rules in the recursive step. assume the exclusion rule holds, even if it is not explicitly mentioned. structural induction is used to prove results about recursively defined sets. 6 3

Recursively Defined Sets and Structures Example : Subset of Integers S: BASIS STEP: 3 S. RECURSIVE STEP: If x S and y S, then x + y is in S. Initially 3 is in S, then 3 + 3 = 6, then 3 + 6 = 9, etc. All positive multiples of 3. Example: BASIS STEP: 0 N. RECURSIVE STEP: If n is in N, then n + 1 is in N. Solution: The natural numbers N. Initially 0 is in S, then 0 + 1 = 1, then 1 + 1 = 2, etc. Recursively Defined Sets and Structures Example : Give a recursive definition of The set of even positive integers. BASIS STEP: 2 S. RECURSIVE STEP: If x S, then x + 2 is in S. The set of positive integer powers of 2 BASIS STEP: 2 S. RECURSIVE STEP: If x S, then x * 2 is in S. 4

Strings Definition 1: The set Σ* of strings over the alphabet Σ: BASIS STEP: λ Σ* (λ is the empty string) RECURSIVE STEP: If w is in Σ* and x is in Σ, then wx Σ*. Example: Σ = {0,1}, the strings in Σ* are the set of all bit strings, λ,0,1, 00,01,10, 11, etc. Example: If Σ = {a,b}, show that aab is in Σ*. Since λ Σ* and a Σ, a Σ*. Since a Σ* and a Σ, aa Σ*. Since aa Σ* and b Σ, aab Σ*. Balanced Parentheses Example: Give a recursive definition of the set of balanced parentheses P. Solution: BASIS STEP: () P RECURSIVE STEP: If w P, then () w P, (w) P and w () P. Show that (() ()) is in P. 5

Section 5.4 11 Recursive Algorithms Definition 1: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. For the algorithm to terminate, the instance of the problem must eventually be reduced to some initial case for which the solution is known. 12 6

. 2014/10/23 Recursive Factorial Algorithm Example: Give a recursive algorithm for computing n!, where n is a nonnegative integer. Solution: Use the recursive definition of the factorial function. procedure factorial(n: nonnegative integer) if n = 0 then factorial(n):=1 else factorial(n):=n*factorial(n-1) 13 Proving Recursive Algorithms Correct Both mathematical and strong induction are useful techniques to show that recursive algorithms always produce the correct output. Example: Prove that the algorithm for computing the powers of real numbers is correct. procedure power(a: nonzero real number, n: nonnegative integer) if n = 0 then return 1 else return a power (a, n 1) {output is a n } Solution: Use mathematical induction on the exponent n. BASIS STEP: a 0 =1 for every nonzero real number a, and power(a,0) = 1. INDUCTIVE STEP: The inductive hypothesis is that power(a,k) = a k, for all a 0. Assuming the inductive hypothesis, the algorithm correctly computes a k+1, since power(a,k + 1) = a power (a, k) = a a k = a k+1. 14 7

Merge Sort Merge Sort works by iteratively splitting a list (with an even number of elements) into two sublists of equal length until each sublist has one element. Each sublist is represented by a balanced binary tree. At each step a pair of sublists is successively merged into a list with the elements in increasing order. The process ends when all the sublists have been merged. The succession of merged lists is represented by a binary tree. 15 Merge Sort Example: Use merge sort to put the list 8,2,4,6,9,7,10, 1, 5, 3 into increasing order. Solution: 16 8

Recursive Merge Sort Example: Construct a recursive merge sort algorithm. Solution: Begin with the list of n elements L. procedure mergesort(l = a 1, a 2,,a n ) if n > 1 then m := n/2 L 1 := a 1, a 2,,a m L 2 := a m+1, a m+2,,a n L := merge(mergesort(l 1 ), mergesort(l 2 )) {L is now sorted into elements in increasing order} procedure merge(l 1, L 2 :sorted lists) L := empty list while L 1 and L 2 are both nonempty remove smaller of first elements of L 1 and L 2 from its list; put at the right end of L if this removal makes one list empty then remove all elements from the other list and append them to L return L {L is the merged list with the elements in increasing order} 17 Merging Two Lists Example: Merge the two lists 2,3,5,6 and 1,4. Solution: 18 9