Recursive Definitions Structural Induction Recursive Algorithms

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

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

Recursively Defined Functions

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

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

Inference rule for Induction

Mathematical Induction

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

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

Recursive Definitions and Structural Induction

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

CSE 20 DISCRETE MATH WINTER

Recursion and Structural Induction

5.3 Recursive definitions and structural induction

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Mathematical Induction

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

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,

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

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

Recursive definition of sets and structural induction

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

AXIOMS FOR THE INTEGERS

Power Set of a set and Relations

Discrete Mathematics. Kruskal, order, sorting, induction

9.5 Equivalence Relations

Propositional Logic. Andreas Klappenecker

Figure 4.1: The evolution of a rooted tree.

Midterm solutions. n f 3 (n) = 3

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

Slides for Faculty Oxford University Press All rights reserved.

CSCI FOUNDATIONS OF COMPUTER SCIENCE

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

Reading for this lecture (Goodrich and Tamassia):

Solutions to the Second Midterm Exam

Discrete Mathematics Lecture 4. Harper Langston New York University

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Lecture 6,

Basic Properties The Definition of Catalan Numbers

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

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

Recursion vs Induction

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

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Discrete Structures. Fall Homework3

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

CMSC351 - Fall 2014, Homework #2

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

Recursion vs Induction. CS3330: Algorithms The University of Iowa

SECTION 5.1. Sequences

Glynda, the good witch of the North

Lecture 9: Sorting Algorithms

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

Recursion and Induction

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

1.3. Conditional expressions To express case distinctions like

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

Encoding/Decoding and Lower Bound for Sorting

Integers and Mathematical Induction

Computer Science 236 Fall Nov. 11, 2010

Lecture 15 : Review DRAFT

Binary Search to find item in sorted array

Reasoning and writing about algorithms: some tips

Multiple-choice (35 pt.)

Analyzing Complexity of Lists

DVA337 HT17 - LECTURE 4. Languages and regular expressions

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

Indicate the option which most accurately completes the sentence.

UNIT 1 BASICS OF AN ALGORITHM

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

Multi-Way Search Trees

Reduction & Recursion Overview

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Treewidth and graph minors

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

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

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

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

Section 1.7 Sequences, Summations Cardinality of Infinite Sets

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

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.

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

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

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

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

Lecture 1. 1 Notation

Ambiguous Grammars and Compactification

Foundations of Computer Science Spring Mathematical Preliminaries

Section 2.4 Sequences and Summations

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

COT 3100 Spring 2010 Midterm 2

NOTE ON MINIMALLY k-connected GRAPHS

Analysis of Algorithms

CS201 Discussion 7 MARKOV AND RECURSION

Fall Recursion and induction. Stephen Brookes. Lecture 4

H c. Show that n n (n!) 2 for all integers n n 1.

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

Solving Linear Recurrence Relations (8.2)

Transcription:

Chapter 4 1

4.3-4.4 Recursive Definitions Structural Induction Recursive Algorithms 2

Section 4.1 3

Principle of Mathematical Induction Principle of Mathematical Induction: To prove that P(n) is true for all positive integers n, we complete these steps: Basis Step: Show that P(1) is true. Inductive Step: Show that P(k) P(k + 1) is true for all positive integers k. To complete the inductive step, assuming the inductive hypothesis that P(k) holds for an arbitrary integer k, show that must P(k + 1) be true. Climbing an Infinite Ladder Example: BASIS STEP: By (1), we can reach rung 1. INDUCTIVE STEP: Assume the inductive hypothesis that we can reach rung k. Then by (2), we can reach rung k + 1. Hence, P(k) P(k + 1) is true for all positive integers k. We can reach every rung on the ladder. 4

5

Section 4.2 6

Strong Induction Strong Induction: To prove that P(n) is true for all positive integers n, where P(n) is a propositional function, complete two steps: Basis Step: Verify that the proposition P(1) is true. Inductive Step: Show the conditional statement [P(1) P(2) P(k)] P(k + 1) holds for all positive integers k. Strong Induction is sometimes called the second principle of mathematical induction or complete induction. 7

Section 4.3 8

Recursion 9

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

Recursively Defined Functions Definition: 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. 11

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 12

Recursively Defined Functions Example: Give a recursive definition of the factorial function n!: Solution: f(0) = 1 f(n + 1) = (n + 1) f(n) 13

Fibonacci Numbers Fibonacci (1170-1250) 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. 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: Solution: The first part of the definition is The second part is 15

Recursively Defined Sets and Structures Recursive definitions of sets have two parts: The basis step specifies an initial collection of elements. The 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. We will always assume that the exclusion rule holds, even if it is not explicitly mentioned. We will later develop a form of induction, called structural induction, to prove results about recursively defined sets. 16

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: The natural numbers N. BASIS STEP: 0 N. RECURSIVE STEP: If n is in N, then n + 1 is in N. Initially 0 is in S, then 0 + 1 = 1, then 1 + 1 = 2, etc.

Strings Definition: 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: If Σ = {0,1}, the strings in 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. Why is ))(() not in P?

Full Binary Trees Definition: 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. 20

Building Up Full Binary Trees 21

Structural Induction Definition: To prove a property of the elements of a recursively defined set, we use structural induction. 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. 22

Full Binary Trees Definition: The height h(t) of a full binary tree T is defined recursively as follows: 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 T 1 and T 2 are full binary trees, then the full binary tree T = T 1 T 2 has the number of vertices n(t) = 1 + n(t 1 ) + n(t 2 ).

2. Structural Induction and Binary Trees Theorem: If T is a full binary tree, then n(t) 2 h(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 25

Recursive Algorithms Definition: 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. 26

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

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

2. 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 ak = ak+1. 29

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

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

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} continued 32

Recursive Merge Sort Subroutine merge, which merges two sorted lists. 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} 33

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