Recursion and Structural Induction

Similar documents
Sets. Mukulika Ghosh. Fall Based on slides by Dr. Hyunyoung Lee

Recursively Defined Functions

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

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,

Recursive Definitions Structural Induction Recursive Algorithms

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

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

Mathematical Induction

5 Graphs

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

5.3 Recursive definitions and structural induction

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Recursive Definitions and Structural Induction

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Foundations of Computer Science Spring Mathematical Preliminaries

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:

HW Graph Theory SOLUTIONS (hbovik) - Q

Math 485, Graph Theory: Homework #3

Solution to Graded Problem Set 4

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula

MATHEMATICS 191, FALL 2004 MATHEMATICAL PROBABILITY Outline #1 (Countability and Uncountability)

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Slides for Faculty Oxford University Press All rights reserved.

Chapter 4: Trees. 4.2 For node B :

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

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

Paths, Flowers and Vertex Cover

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Analysis of Algorithms

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

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

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Graph Algorithms. Chromatic Polynomials. Graph Algorithms

Discrete mathematics , Fall Instructor: prof. János Pach

1.3. Conditional expressions To express case distinctions like

We assume uniform hashing (UH):

Inference rule for Induction

Product Cordial Sets of Trees

CS525 Winter 2012 \ Class Assignment #2 Preparation

Recursive Data Structures and Grammars

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

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

Exercise set 2 Solutions

Context-Free Grammars

CS 6783 (Applied Algorithms) Lecture 5

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

2009 HMMT Team Round. Writing proofs. Misha Lavrov. ARML Practice 3/2/2014

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

EDAA40 At home exercises 1

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Algorithms. AVL Tree

Functions 2/1/2017. Exercises. Exercises. Exercises. and the following mathematical appetizer is about. Functions. Functions

To illustrate what is intended the following are three write ups by students. Diagonalization

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

Chapter 3: Propositional Languages

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

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

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

CSE 20 DISCRETE MATH WINTER

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

Solutions to Homework 10

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

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

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

Lecture 1. 1 Notation

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

Ambiguous Grammars and Compactification

Graph Theory Questions from Past Papers

AXIOMS FOR THE INTEGERS

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

Mathematical Induction

ASSIGNMENT 4 SOLUTIONS

Solutions to Problem Set 1

1 Maximum Independent Set

2 Geometry Solutions

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Assignment 4 Solutions of graph problems

Proving Properties of Recursive Functions and Data Structures. CS 270 Math Foundations of CS Jeremy Johnson

Counting. Andreas Klappenecker

Propositional Logic. Andreas Klappenecker

Fall Recursion and induction. Stephen Brookes. Lecture 4

Properties of red-black trees

Figure 4.1: The evolution of a rooted tree.

Greedy Algorithms CHAPTER 16

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Evolution Module. 6.1 Phylogenetic Trees. Bob Gardner and Lev Yampolski. Integrated Biology and Discrete Math (IBMS 1300)

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103. Chapter 2. Sets

Recursive definition of sets and structural induction

Assigning Meaning to Programs, Floyd. Proving Termination with Multiset Orderings

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

An Annotated Language

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

Organizing Spatial Data

MATH 139 W12 Review 1 Checklist 1. Exam Checklist. 1. Introduction to Predicates and Quantified Statements (chapters ).

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

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.

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Transcription:

Recursion and Structural Induction Mukulika Ghosh Fall 2018 Based on slides by Dr. Hyunyoung Lee

Recursively Defined Functions Recursively Defined Functions Suppose we have a function with the set of non-negative integers as its domain. We can specify the function as follows: Basis step: Specify the value of the function at 0. Recursive or Inductive step: Give a rule for finding its value at an integer from its values at smaller integers. This is called a recursive or inductive definition. Mukulika Ghosh Parasol Lab - Texas A&M University 2/26

Recursively Defined Functions Examples Factorial We can define the factorial function n! as follows: Base step: 0! = 1 Recursive step: n! = n(n 1)! Fibonacci Numbers The Fibonacci numbers f n are defined as follows: Base step: f 0 = 0 and f 1 = 1 Recursive step: f n = f n 1 + f n 2 for n 2 Mukulika Ghosh Parasol Lab - Texas A&M University 3/26

Recursively Defined Functions One can define recursively defined functions for domains other than the non-negative integers. In general, a function f is called recursively defined if and only if at least one value f(x) is defined in terms of another value f(y), where x and y are distinct elements. Mukulika Ghosh Parasol Lab - Texas A&M University 4/26

Recursively Defined Functions Exercise Let F be the function such that F (n) is the sum of the first n positive integers. Give a recursive definition of F (n). Mukulika Ghosh Parasol Lab - Texas A&M University 5/26

Recursively Defined Sets Recursively Defined Sets An recursive definition of a set S has the following form: 1. Basis: Specify one or more initial elements of S. 2. Recursion/Induction: Give one or more rules for constructing new elements of S from old elements of S. 3. Closure: The set S consists of exactly the elements that can be obtained by starting with the initial elements of S and applying the rules for constructing new elements of S. Mukulika Ghosh Parasol Lab - Texas A&M University 6/26

Recursively Defined Sets The definition is similar to steps taken inductions. The closure condition is usually omitted, since it is always assumed in recursive definitions. Example: Natural numbers {0, 1, 2,...} can be defined as: Basis: 0 N Recursion: If n N then n + 1 N Mukulika Ghosh Parasol Lab - Texas A&M University 7/26

Why Recursive Definition Why Recursive Definition Recursive definition is popular in computer science. With recursive definition, there is no ambiguities about the contents of a set. Example Consider the set {0, 1, 3, 5, 7,...} can represent either set of odd primes or set of odd integers based on the contents so far. But a recursive definition: 0 S as basis and recursion as n + 2 S when n S remove such ambiguities. Mukulika Ghosh Parasol Lab - Texas A&M University 8/26

Another Example Another Example Let S be the set defined as follows: Basis: 0 S Recursion: If n S, then 2n + 1 S. The set can also be described as, S = {0, 1, 3, 7, 15, 31,...} = {2 n 1 n is a non-negative integer } since 2 0 1 = 0 and 2 n+1 1 = 2(2 n 1) + 1 Mukulika Ghosh Parasol Lab - Texas A&M University 9/26

Exercise Exercise Give the recursive definition of the sequence {a n }, n = 1, 2, 3,... if a n = n 2 Mukulika Ghosh Parasol Lab - Texas A&M University 10/26

Applications: Well-formed Formulas Applications: Well-formed Formulas We can define the set of well-formed formulas consisting of variables, numerals, and operators from the set {+,,, /} as follows: Basis: x is a well-formed formula if x is a numeral or a variable. Recursion: If F and G are well-formed formulas, then (F + G), (F G), (F G), and (F/G) are well-formed formulas. Examples: 42, x, (x + 42), (x y), (3/0), (x (y + z)) Application: Definition of grammars, logic etc. Mukulika Ghosh Parasol Lab - Texas A&M University 11/26

Applications: Lists Applications: Lists We can define the set L of finite lists of integers as follows. Basis: The empty list () is contained in L Recursion: If i is an integer, and l is a list in L, then (cons i l) is in L. Note: This is the Lisp style of lists, where (cons i l) prepends the data item i at the front of the list l Example: (cons 1 (cons 2 (cons 3 () ))) is the list (1 2 3) in Lisp. Application: Data structure Mukulika Ghosh Parasol Lab - Texas A&M University 12/26

Applications: Binary Trees Applications: Binary Trees We can define the set B of binary trees over an alphabet A as follows: Basis: <> B. Recursion: If L, R B and x A, then < L, x, R > B. Example: <<>, 1, <>> is a tree with one node (1) Example: <<<>, 1, <>>, r, <<>, 2, <>>> is tree with root r and two children (1 and 2). Application: Data structure Mukulika Ghosh Parasol Lab - Texas A&M University 13/26

Structural Induction Structural Induction Structural induction asserts a property about elements of an inductively defined set. The proof method directly exploits the inductive definition of the set. The method is more powerful than strong induction in the sense that one can prove statements that are difficult (or impossible) to prove with strong induction. Typically, though, it is simply used because it is more convenient than (strong) induction. Mukulika Ghosh Parasol Lab - Texas A&M University 14/26

Structural Induction In structural induction, the proof of the assertion that every element of an inductively defined set S has a certain property P proceeds by showing that: Basis: Every element in the basis of the definition of S satisfies the property P. Induction: Assuming that every argument of a constructor has property P, show that the constructed element has the property P. Mukulika Ghosh Parasol Lab - Texas A&M University 15/26

Example: Binary Trees Example: Binary Trees The set B of binary trees over an alphabet A is defined as follows: Basis: <> B. Recursion: If L, R B and x A, then < L, x, R > B We can now prove that every binary tree has a property P by arguing that Basis: P (<>) is true. Induction: For all binary trees L and R and x A, if P (L) and P (R), then P (< L, x, R >) Mukulika Ghosh Parasol Lab - Texas A&M University 16/26

Example: Binary Trees Let f : B N be a function defined by: f(<>) = 0 1, if L = R =<> f(< L, x, R >) = f(l) + f(r), otherwise Let T in B be a binary tree. Then f(t ) yields the number of leaves of T. Mukulika Ghosh Parasol Lab - Texas A&M University 17/26

Example: Binary Trees Proof Basis: The empty tree has no leaves, so f(<>) = 0 is correct. Induction: Let L, R be trees in B, x A. Induction Hypothesis: Suppose that f(l) and f(r) denote the number of leaves of L and R, respectively. If L = R =<>, then < L, x, R >=<<>, x, <>> has one leaf, namely x, so f(< L, x, R >) = 1 is correct. If L and R are not both empty, then the number of leaves of the tree < L, x, R > is equal to the number of leaves of L and R. Hence, by induction hypothesis, we get f(< L, x, R >) = f(l) + f(r) as claimed. Mukulika Ghosh Parasol Lab - Texas A&M University 18/26

Example: Complete Binary Trees Example: Complete Binary Trees A binary tree is complete if and only if each node is either a leaf or has precisely two children. Recursive Definition Basis: There is a complete binary tree consisting of a single vertex r Recursion: If T 1 and T 2 are disjoint complete binary trees and r A is a node, then < T 1, r, T 2 > is a complete binary tree with root r and left subtree T 1 and right subtree T 2. The difference between binary trees and complete binary trees is in the basis step. Mukulika Ghosh Parasol Lab - Texas A&M University 19/26

Example: Complete Binary Trees Mukulika Ghosh Parasol Lab - Texas A&M University 20/26

Height of Complete Binary Tree Height of Complete Binary Tree Let T be a complete binary tree over an alphabet A. We define the height h(t ) of a complete binary tree as follows: Basis: For r A, we define h(r) = 0; that is, the height of a complete binary tree with just a single node is 0. Recursion: If L and R are complete binary trees and r A, then the tree < L, r, R > has height h(< L, r, R >) = 1 + max(h(l), h(r)) Mukulika Ghosh Parasol Lab - Texas A&M University 21/26

Number of Nodes in Complete Binary Tree Number of Nodes in Complete Binary Tree Let n(t ) denote the number of nodes of a complete binary tree over an alphabet A. Then Basis: For r A, we have n(r) = 1. Recursion: If L and R are complete binary trees and r A, then the number of nodes of < L, r, R > is given by n(< L, r, R >) = 1 + n(l) + n(r) Mukulika Ghosh Parasol Lab - Texas A&M University 22/26

Example of Structural Induction Example of Structural Induction Let T be a complete binary tree over an alphabet A. Then we have n(t ) 2 h(t )+1 1. Proof: Basis step: For r in A, we have n(r) = 1 and h(r) = 0, therefore, we have n(r) = 1 2 (0+1) 1 = 2 h(r)+1 1, as claimed. Mukulika Ghosh Parasol Lab - Texas A&M University 23/26

Example of Structural Induction Inductive step: Suppose that L and R are complete binary trees that satisfy n(l) 2 h(l)+1 1 and n(r) 2 h(r)+1 1. Then the tree T =< L, r, R > satisfies: n(t ) = 1 + n(l) + n(r) 1 + 2 h(l)+1 1 + 2 h(r)+1 1, by Induction Hypothesis 2max(2 h(l)+1, 2 h(r)+1 ) 1, since a + b 2max(a, b) = 2.2 max(h(l),h(r))+1 1 = 2.2 h(t ) 1 = 2 h(t )+1 1 Mukulika Ghosh Parasol Lab - Texas A&M University 24/26

Complete Binary Tree: Internal Vertices and Leaves Complete Binary Tree: Internal Vertices and Leaves The set of leaves and the set of internal vertices of a complete binary tree is defined recursively: Base: The root r is a leaf of the complete binary tree with exactly one vertex r. This tree has no internal vertices. Recursion: The set of leaves of the tree T =< L, r, R > is the union of the sets of leaves of L and of R. The internal vertices of T are the root r of T and the union of the set of internal vertices of L and the set of internal vertices of R. Mukulika Ghosh Parasol Lab - Texas A&M University 25/26

Exercise Exercise Let l(t ) be the number of leaves of a binary tree T, and i(t ), the number of internal vertices of T. Use structural induction to show that l(t ) = i(t )+1 holds for all complete binary trees. Mukulika Ghosh Parasol Lab - Texas A&M University 26/26