1 Chapter 3 - 1 Construction Techniques. 2 Section 3.1 Inductively Defined Sets To define a set S inductively is to do three things: Basis: Specify one.

Slides:



Advertisements
Similar presentations
Recursive Definitions and Structural Induction
Advertisements

Equivalence, Order, and Inductive Proof
1 Chapter Equivalence, Order, and Inductive Proof.
1 Chapter Facts about Functions. 2 Section 2.1 Functions: Definitions and Examples A function ƒ from A to B associates each element of A with exactly.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CSE115/ENGR160 Discrete Mathematics 03/31/11
Costas Busch - RPI1 Mathematical Preliminaries. Costas Busch - RPI2 Mathematical Preliminaries Sets Functions Relations Graphs Proof Techniques.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Courtesy Costas Busch - RPI1 Mathematical Preliminaries.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Induction and recursion
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Recursive Definitions and Structural Induction
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Induction and recursion
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 4: Induction and Recursion
Construction Techniques (2) - 1ICOM 4075 (Spring, 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez 2005.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
1 Chapter Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
1 Section 5.5 Solving Recurrences Any recursively defined function ƒ with domain N that computes numbers is called a recurrence or recurrence relation.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
1 Chapter Elementary Notions and Notations.
1 Section 10.3 Abstract Data Types as Algebras An abstract data type (ADT) is an algebra in which the carriers (i.e., the data sets) and the operations.
1 Chapter Facts about Functions. 2 Section 2.3 Properties of Functions Let ƒ : A → B be a function. There are three properties that ƒ might possess.
Construction Techniques (1) - 1ICOM 4075 (Fall, 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez 2005.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Mathematical Preliminaries. Sets Functions Relations Graphs Proof Techniques.
Tree Traversal.
Functions1 Elementary Discrete Mathematics Jim Skon.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Discrete Structures Trees (Ch. 11)
1 Section 13.2 The Church-Turing Thesis The Church-Turing Thesis: Anything that is intuitively computable can be be computed by a Turing machine. It is.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
1 Section 3.2 Recursively Defined Functions and Procedures A function ƒ is recursively defined if at least one value ƒ(x) is defined in terms of another.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
1 Section 4.3 Order Relations A binary relation is an partial order if it transitive and antisymmetric. If R is a partial order over the set S, we also.
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
1 Section 4.2 Equivalence Relations A binary relation is an equivalence relation if it has the three properties reflexive, symmetric, and transitive (RST).
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Discrete Structures Li Tak Sing( 李德成 ) mt263f ppt 1.
Functions (2) - 1ICOM 4075 (Spring, 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez ICOM 4075: Foundations.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 4: Induction and Recursion
Recursive Objects (Part 4)
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Mathematical Induction Recursion
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Principles of Computing – UFCFA3-30-1
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Section 3.1 Inductively Defined Sets
Chapter 20: Binary Trees.
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
Presentation transcript:

1 Chapter Construction Techniques

2 Section 3.1 Inductively Defined Sets To define a set S inductively is to do three things: Basis: Specify one or more elements of S. Induction: Specify one or more rules to construct elements of S from existing elements of S. Closure: Specify that no other elements are in S (always assumed). Note: The basis elements and the induction rules are called constructors.

3 Example 1 Find an inductive definition for S = {3, 16, 29, 42, …}. Solution: Basis: 3 ∈ S. Induction: If x ∈ S then x + 13 ∈ S. The constructors are 3 and the operation of adding 13. Also, without closure, many sets would satisfy the basis and induction rule. e.g., 3 ∈ Z and x ∈ Z implies x + 13 ∈ Z.

4 Example 2 Find an inductive definition for S = {3, 4, 5, 8, 9, 12, 16, 17, 20, 24, 33,…}. Solution: To simplify things we might try to “divide and conquer” by writing S as the union of more familiar sets as follows: S = {3, 5, 9, 17, 33, …} ⋃ {4, 8, 12, 16, 20, 24, …}. Basis: 3, 4 ∈ S. Induction: If x ∈ S then (if x is odd then 2x – 1 ∈ S else x + 4 ∈ S).

5 Example 3 Describe the set S defined inductively as follows: Basis: 2 ∈ S; Induction: x ∈ S implies x ± 3 ∈ S. Solution: S = {2, 5, 8, 11, … } ⋃ {–1, –4, – 7, –10, … }.

6 Example 4 Find an inductive definition for S = { ٨, ac, aacc, aaaccc, …} = {a n c n | n ∈ N}. Solution: Basis: ٨ ∈ S. Induction: If x ∈ S then axc ∈ S.

7 Example 5 Find an inductive definition for S = {a n+1 bc n | n ∈ N}. Solution: Basis: ab ∈ S. Induction: If x ∈ S then axc ∈ S.

8 Example 6 Describe the set S defined by: –Basis: a, b ∈ S –Induction: x ∈ S implies ƒ(x) ∈ S. Solution: S = {a, ƒ(a), ƒ(ƒ(a)), …} ⋃ {b, ƒ(b), ƒ(ƒ(b)), …}, which could also be written as S = {ƒ n (a) | n ∈ N} ⋃ {ƒ n (b) | n ∈ N} = {ƒ n (x) | x ∈ {a, b} and n ∈ N}.

9 Example 7 Describe the set S defined by: –Basis: ∈ S –Induction: x ∈ S implies cons(1, x) ∈ S. Solution: S = {,,, …}.

10 Infix notation cons(h, t) = h :: t. Associate to the right. e.g., x :: y :: z = x :: (y :: z). Example 8. Find an inductive definition for S = {<>,,, …}. Solution: –Basis: <> ∈ S. –Induction: x ∈ S implies a :: b :: x ∈ S.

11 Example 9 Find an inductive definition for S = {<>, >, >>, …}. Solution: Basis: <> ∈ S. Induction: x ∈ S implies x :: <> ∈ S.

12 Notation for Binary Trees Let t(L, x, R) denote the tree with root x, left subtree L, and right subtree R. Let <> denote the empty binary tree. If T = t(L, x, R), then root(T) = x, left(T) = L, and right(T) = R.

13 Example 10 Describe the set S defined inductively as follows: –Basis: t(<>,, <>) ∈ S. –Induction: T ∈ S implies t(T,, t(<>,, <>)) ∈ S. Solution (picture): The first few trees constructed from the definition are pictured as follows: and so on.

14 Example 11 Find an inductive definition for the set S of binary trees indicated by the following picture. and so on. Solution: Basis: t(<>,, <>) ∈ S. Induction: T ∈ S implies t(t(left(T),, <>),, t(<>,, right(T))) ∈ S.

15 Example 12 Find an inductive definition for the set S = {a}* × N. Solution: Basis: ( ٨, 0) ∈ S. Induction: (s, n) ∈ S implies (as, n), (s, n + 1) ∈ S.

16 Example 13 Find an inductive definition for the set S = {(x, –y) | x, y ∈ N and x ≥ y}. Solution: To get an idea about S we can write out a few tuples: (0, 0), (1, 0), (1, –1), (2, 0), (2, –1), (2, –2), and so on. We can also get an idea about S by graphing a few points, as indicated in the picture. One solution can be written as follows: –Basis: (0, 0) ∈ S. –Induction: (x, y) ∈ S implies (x + 1, y), (x + 1, y – 1) ∈ S. Notice that this definition constructs some repeated points. For example, (2, –1) is constructed twice.

17 Quiz (2 minutes) Try to find a solution that does not construct repeated elements. Solution: We might use two separate rules. One rule to construct the diagonal points and one rule to construct horizontal lines that start at the diagonal points. –Basis: (0, 0) ∈ S. –Induction: 1. (x, y) ∈ S implies (x + 1, y) ∈ S. 2. (x, –x) ∈ S implies (x + 1, – (x + 1)) ∈ S.

18 Section 3.2 Recursively Defined Functions and Procedures A function f is recursively defined if at least one value f(x) is defined in terms of another value f(y), where x ≠ y. Similarly, a procedure P is recursively defined if the action of P(x) is defined in terms of another action P(y), where x ≠ y.

19 Technique for recursive definitions Technique for recursive definitions when the argument domain is inductively defined. 1. Specify a value f(x), or action P(x), for each basis element x of S. 2. Specify rules that, for each inductively defined element x in S, define the value f(x), or action P(x), in terms of previously defined values of f or actions of P.

20 Example 1 Find a recursive definition for the function f: N → N defined by f(n) = … + 3n. Solution: Notice that N is an inductively defined set: 0 ∈ N; n ∈ N implies n + 1 ∈ N. So we need to give f(0) a value in N and we need to define f(n + 1) in terms of f(n). The given definition of f tells us to set f(0) = 0. To discover a definition for f(n + 1) we can write f(n + 1) = ( … + 3n) + 3(n + 1) = f(n) + 3(n + 1). So we have a recursive definition for f –f(0) = 0 –f(n + 1) = f(n) + 3(n + 1).

21 Two alternative definitions f(0) = 0 f(n) = f(n - 1) + 3n (n > 0). (if-then-else form): f(n) = if n = 0 then 0 else f(n - 1) + 3n.

22 Example 2 Find a recursive definition for cat : A* × A* → A* defined by cat(s, t) = st. Solution: Notice that A* is inductively defined: ٨ ∈ A*; a ∈ A and x ∈ A* imply ax ∈ A*, where ax denotes the string version of cons. We can define cat recursively using the first argument. The definition of cat gives cat( ٨, t) = ٨ t = t. For the recursive part we can write cat(ax, t) = axt = a(xt) = acat(x, t). So we have a definition: –cat( ٨, t) = t –cat(ax, t) = acat(x, t). If-then-else form using head and tail for strings: cat(s, t) = if s = ٨ then t else head(s)cat(tail(s), t).

23 Example 3 Find a definition for f: lists(Q) → Q defined by f( ) = x 1 + … + x n. Solution: The set lists(Q) is inductively defined: <> ∈ lists(Q); h ∈ Q and t ∈ lists(Q) imply h :: t ∈ lists(Q). To discover a recursive definition, we can use the definition of f as follows: f( ) = x 1 + … + x n = x 1 + (x 2 + … + x n ) = x 1 + f( ) = head( ) + f(tail( ). So if we let f(<>) = 0, we have a recursive definition: –f(<>) = 0 –f(h :: t) = h + f(t). If-then-else form: f(L) = if L = <> then 0 else head(L) + f(tail(L)).

24 Example 4 Given f: N → N defined recursively by –f(0) = 0 –f(1) = 0 –f(x + 2) = 1 + f(x). The if-then-else form for f can be written as follows: f(x) = if x = 0 or x = 1 then 0 else 1 + f(x - 2). What does f do? Answer: List a few values to get the idea. For example, map(f, ) =. So f(x) returns the floor of x/2. i.e., f(x) = ⌊ x/2 ⌋.

25 Example 5 Find a recursive definition for f: lists(Q) → Q defined by f( ) = x 1 x 2 + x 2 x 3 + … + x n-1 x n. Solution: Let f(<>) = 0 and f( ) = 0. Then for n ≥ 2 we can write f( ) = x 1 x 2 + (x 2 x 3 + … + x n-1 x n ) = x 1 x 2 + f( ). So we have the following recursive definition. –f(<>) = 0 –f( ) = 0 –f(h :: t) = h head(t) + f(t). If-then-else form: f(L) = if L = <> or tail(L) = <> then 0 else head(L) head(tail(L)) + f(tail(L)).

26 Example 6 Find a recursive definition for isin : A × lists(A) → {true, false} where isin(x, L) means that x is in the list L. Solution: isin(x, <>) = false isin(x, x :: t) = true isin(x, h :: t) = isin(x, t). If-then-else form: isin(x, L) = if L = <> then false else if x = head(L) then true else isin(x, tail(L)).

27 Example 7 Find a recursive definition for sub : lists(A) × lists(A) → {true, false} where sub(L, M) means the elements of L are elements of M. Solution: sub(<>, M) = true sub(h :: t, M) = if isin(h, M) then sub(t, M) else false. If-then-else form: sub(L, M) = if L = <> then true else if isin(head(L), M) then sub(tail(L), M) else false.

28 Example 8 Find a recursive definition for intree : Q × binSearchTrees(Q) → {true, false} where intree(x, T) means x is in the binary search tree T. Solution: intree(x, <>) = false intree(x, tree(L, x, R)) = true intree(x, tree(L, y, R)) = if x < y then intree(x, L) else intree(x, R). If-then-else form: intree(x, T) = if T = <> then false else if x = root(T) then true else if x < root(T) then intree(x, left(T)) else intree(x, right(T)).

29 Traversing Binary Trees The three standard procedures to traverse a binary tree are defined recursively as follows: preorder(T): if T ≠ <> then visit root(T); preorder(left(T)); preorder(right(T)) fi. inorder(T): if T ≠ <> then inorder(left(T)); visit root(T); inorder(right(T)) fi postorder(T): if T ≠ <> then postorder(left(T)); postorder(right(T)); visit root(T) fi.

30 Example 9 Traverse the following tree in each of the three orders. Solution: –Preorder: a b c d e –Inorder: b a d c e –Postorder: b d e c a a b c de

31 Example 10 Find a recursive definition for post : binaryTrees(A) → lists(A) where post(T) is the list of nodes from a postorder traversal of T. Solution: post(<>) = <> post(tree(L, x, R)) = cat(post(L), cat(post(R), )) where cat concatenates two lists and can be defined by, –cat(<>, L) = L –cat(h :: t, L) = h :: cat(t, L).

32 Example 11 Find a recursive definition for ƒ : binaryTrees(Q) → Q where ƒ(T) is the sum of the nodes in T. Solution: ƒ( <>) = 0 ƒ(tree(L, x, R)) = x + ƒ(L) + ƒ(R).

33 Infinite Sequences We can construct recursive definitions for infinite sequences by defining a value ƒ(x) in terms of x and ƒ(y) for some value y in the sequence. Example 12. Suppose we want to represent the infinite sequence ƒ(x) =. Solution: Use the definition to discover a solution as follows: ƒ(x) = = x :: = x :: ƒ(x 2 ). So define ƒ(x) = x :: ƒ(x 2 ).

34 More examples Example 13. What sequence is defined by g(x, k) = x k :: g(x, k + 1)? Answer: g(x, k) = x k :: g(x, k + 1) = x k :: x k+1 :: g(x, k + 2) =… =. Example 14. How do we obtain the sequence ? A Solution. Define ƒ(x) = h(x, 1), where h(x, k) = x k :: h(x, k + 2). Example 15. How do we obtain the sequence ? A Solution: Use h(x, 0) from Example 14.

35 The End of Chapter 3 - 1