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

Similar documents
Recursive Definitions Structural Induction Recursive Algorithms

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

Recursively Defined Functions

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

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

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

Inference rule for Induction

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

Mathematical Induction

Recursive Definitions and Structural Induction

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

Recursion and Structural Induction

5.3 Recursive definitions and structural induction

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,

Mathematical Induction

Recursive definition of sets and structural induction

CSE 20 DISCRETE MATH WINTER

Figure 4.1: The evolution of a rooted tree.

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

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

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Midterm solutions. n f 3 (n) = 3

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

AXIOMS FOR THE INTEGERS

Reading for this lecture (Goodrich and Tamassia):

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

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

CSCI FOUNDATIONS OF COMPUTER SCIENCE

Slides for Faculty Oxford University Press All rights reserved.

Solutions to the Second Midterm Exam

Power Set of a set and Relations

CMSC351 - Fall 2014, Homework #2

9.5 Equivalence Relations

Basic Properties The Definition of Catalan Numbers

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

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

Encoding/Decoding and Lower Bound for Sorting

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Recursion vs Induction

Binary Search to find item in sorted array

Multiple-choice (35 pt.)

Analyzing Complexity of Lists

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

Discrete Structures. Fall Homework3

Discrete Mathematics. Kruskal, order, sorting, induction

Propositional Logic. Andreas Klappenecker

Glynda, the good witch of the North

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:

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

1.3. Conditional expressions To express case distinctions like

Ambiguous Grammars and Compactification

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

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

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Directed Graph and Binary Trees

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Computer Science 236 Fall Nov. 11, 2010

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

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS201 Discussion 7 MARKOV AND RECURSION

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/

Solving Linear Recurrence Relations (8.2)

These notes present some properties of chordal graphs, a set of undirected graphs that are important for undirected graphical models.

This chapter covers recursive definition, including finding closed forms.

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Algorithms Dr. Haim Levkowitz

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

DVA337 HT17 - LECTURE 4. Languages and regular expressions

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Lecture 9: Sorting Algorithms

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

END-TERM EXAMINATION

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Paths, Flowers and Vertex Cover

Framework for Design of Dynamic Programming Algorithms

Reasoning and writing about algorithms: some tips

CSC236H Lecture 5. October 17, 2018

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

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

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Indicate the option which most accurately completes the sentence.

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Lecture 1. 1 Notation

Multi-Way Search Trees

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Chapter 4: Trees. 4.2 For node B :

Lecture 6: Analysis of Algorithms (CS )

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

Lecture 15 : Review DRAFT

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

15.4 Longest common subsequence

Solutions for Algorithm Design Exercises and Tests

Integers and Mathematical Induction

Transcription:

Section 5.3 1

Recursion 2

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,... 3

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

Recursively Defined Functions Example: Suppose f is defined by: f(0) = 3, f(n + 1) = 2f(n) + 3 Find f(1), f(2), f(3), f(4) Solution: f(1) = 2f(0) + 3 = 2 3 + 3 = 9 f(2) = 2f(1)+ 3 = 2 9 + 3 = 21 f(3) = 2f(2) + 3 = 2 21 + 3 = 45 f(4) = 2f(3) + 3 = 2 45 + 3 = 93 5

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

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

Recursively Defined Functions Example: Give a recursive definition of: Solution: The first part of the definition is The second part is 8

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

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.

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 Σ*.

Strings Definition 2:

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. Why is ))(() not in P?

Rooted Trees Definition 3: The set of rooted trees, where a rooted tree consists of a set of vertices containing a distinguished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps: BASIS STEP: A single vertex r is a rooted tree. RECURSIVE STEP: Suppose that T 1, T 2,..., T n are disjoint rooted trees with roots r 1, r 2,..., r n, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T 1, T 2,..., T n, and adding an edge from r to each of the vertices r 1, r 2,..., r n, is also a rooted tree. 14

Full Binary Trees Definition 5: The set of full binary trees can be defined recursively by these steps. BASIS STEP: There is a full binary tree consisting of only a single vertex r. RECURSIVE STEP: If T 1 and T 2 are disjoint full binary trees, there is a full binary tree, denoted by T 1 T 2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree T 1 and the right subtree T 2. 15

Structural Induction structural induction is used to prove a property of the elements of a recursively defined set. BASIS STEP: Show that the result holds for all elements specified in the basis step of the recursive definition. RECURSIVE STEP: Show that if the statement is true for each of the elements used to construct new elements in the recursive step of the definition, the result holds for these new elements. The validity of structural induction can be shown to follow from the principle of mathematical induction. 16

Full Binary Trees Definition 6: The height h(t) of a full binary tree T is defined recursively as: BASIS STEP: The height of a full binary tree T consisting of only a root r is h(t) = 0. RECURSIVE STEP: If T 1 and T 2 are full binary trees, then the full binary tree T = T 1 T 2 has height h(t) = 1 + max(h(t 1 ),h(t 2 )). The number of vertices n(t) of a full binary tree T satisfies the following recursive formula: BASIS STEP: The number of vertices of a full binary tree T consisting of only a root r is n(t) = 1. RECURSIVE STEP: If T1 and T2 are full binary trees, then the full binary tree T = T1 T2 has the number of vertices n(t) = 1 + n(t1) + n(t2).

2. Structural Induction and Binary Trees Theorem 2: If T is a full binary tree, then n(t) 2h(T)+1 1. Proof: Use structural induction. BASIS STEP: The result holds for a full binary tree consisting only of a root, n(t) = 1 and h(t) = 0. Hence, n(t) = 1 2 0+1 1 = 1. RECURSIVE STEP: Assume n(t 1 ) 2 h(t1)+1 1 and also n(t 2 ) 2 h(t2)+1 1 whenever T 1 and T 2 are full binary trees. n(t) = 1 + n(t 1 ) + n(t 2 ) (by recursive formula of n(t)) 1 + (2 h(t1)+1 1) + (2 h(t2)+1 1) (by inductive hypothesis) 2 max(2 h(t1)+1,2 h(t2)+1 ) 1 = 2 2 max(h(t1),h(t2))+1 1 (max(2 x, 2 y )= 2 max(x,y) ) = 2 2 h(t) 1 (by recursive definition of h(t)) = 2 h(t)+1 1

Section 5.4 19

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

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

Recursive Binary Search Algorithm Example: Construct a recursive version of a binary search algorithm. Solution: Assume we have a1,a2,, an, an increasing sequence of integers. Initially i is 1 and j is n. We are searching for x. procedure binary search(i, j, x : integers, 1 i j n) m := (i + j)/2 if x = a m then return m else if (x < a m and i < m) then return binary search(i,m 1,x) else if (x > a m and j >m) then return binary search(m+1,j,x) else return 0 {output is location of x in a 1, a 2,,a n if it appears, otherwise 0} 22

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

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

Merge Sort Example: Use merge sort to put the list 8,2,4,6,9,7,10, 1, 5, 3 into increasing order. Solution: 25

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

Merging Two Lists Example: Merge the two lists 2,3,5,6 and 1,4. Solution: 27