Discrete Structures for Computer Science

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Recursive Definitions and Structural Induction
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.
10.1 CompSci 102© Michael Frank Today’s topics RecursionRecursion –Recursively defined functions –Recursively defined sets –Structural Induction Reading:
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
CSE 321 Discrete Structures Winter 2008 Lecture 12 Induction.
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
CSE115/ENGR160 Discrete Mathematics 03/31/11
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Chapter Mathematical Induction
1 Section 3.4 Recursive Definitions. 2 Recursion Recursion is the process of defining an object in terms of itself Technique can be used to define sequences,
Induction and recursion
Induction and recursion
Recursive Definitions and Structural Induction
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Induction and recursion
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
Chapter 4: Induction and Recursion
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 16 Recursively Defined Sets and Structural Induction Spring
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Induction and recursion
Chapter 5 With Question/Answer Animations. Section 5.1.
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
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.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
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.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
CSE 311: Foundations of Computing Fall 2013 Lecture 18: Structural induction, regular expressions.
Chapter 5 Kenneth Rosen, Discrete Mathematics and its Applications, 7th edition, McGraw Hill Instructor: Longin Jan Latecki, Copyright.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSE 311 Foundations of Computing I Lecture 15 Strong Induction and Recursive Definitions Spring
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
Copyright © Zeph Grunschlag,
(Proof By) Induction Recursion
Chapter 4: Induction and Recursion
Induction and recursion
Induction and Recursion Chapter 5
Advanced Algorithms Analysis and Design
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Recursively Defined Functions
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Discrete Structures for Computer Science
CSE 311: Foundations of Computing
CSI Mathematical Induction
CSE 321 Discrete Structures
Closure Properties of Regular Languages
Chapter 4 Induction and Recursion
CS 250, Discrete Structures, Fall 2014 Nitesh Saxena
Copyright © Zeph Grunschlag,
Advanced Analysis of Algorithms
Mathematical Induction
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Induction and recursion
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
CSE 311 Foundations of Computing I
Presentation transcript:

Discrete Structures for Computer Science Presented By: Andrew F. Conn Lecture #15: Recursion and Structural Induction October 26th, 2016

There are many uses of induction in computer science! Proof by induction is often used to reason about: Algorithm properties (correctness, etc.) Properties of data structures Membership in certain sets Determining whether certain expressions are well-formed … To begin looking at how we can use induction to prove the above types of statements, we first need to learn about recursion

Sometimes, it is difficult or messy to define some object explicitly Recursive objects are defined in terms of themselves We often see the recursive versions of the following types of objects: Functions Sequences Sets Data structures Let’s look at some examples…

Recursive functions are useful When defining a recursive function whose domain is the set of natural numbers, we have two steps: Basis step: Define the behavior of f(0) Recursive step: Compute f(n+1) using f(0), …, f(n) Example: Let f(0) = 3, f(n+1) = 2f(n) + 3 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 … Doesn’t this look a little bit like strong induction?

Some functions can be defined more precisely using recursion Example: Define the factorial function F(n) recursively Basis step: F(0) = 1 Recursive step: F(n+1) = (n+1) × F(n) Note: F(4) = 4 × F(3) = 4 × 3 × F(2) = 4 × 3 × 2 × F(1) = 4 × 3 × 2 × 1 × F(0) = 4 × 3 × 2 × 1 × 1 = 24 Compare the above definition our old definition: F(n) = n × (n-1) × … × 2 × 1 The recursive definition avoids using the “…” shorthand!

It should be no surprise that we can also define recursive sequences Example: The Fibonacci numbers, {fn}, are defined as follows: f0 = 1 f1 = 1 fn = fn-1 + fn-2 Calculate: f2, f3, f4, and f5 f2 = f1 + f0 = 1 + 1 = 2 f3 = f2 + f1 = 2 + 1 = 3 f4 = f3 + f2 = 3 + 2 = 5 f5 = f4 + f3 = 5 + 3 = 8 This gives us the sequence {fn} = 1, 1, 2, 3, 5, 8, 13, 21, 34, … This is like strong induction, since we need more than fn-1 to compute fn. Fib numbers in nature

Recursion is used heavily in the study of strings Let: ∑ be defined as an alphabet Binary strings: ∑ = {0, 1} Lower case letters: ∑ = {a, b, c, …, z} We can define the set ∑* containing all strings over the alphabet ∑ as follows: Basis step: λ ∈ ∑* Recursive step: If w ∈ ∑* and x ∈ ∑, then wx ∈ ∑* Example: If ∑ = {0, 1}, then ∑ = {λ, 0, 1, 01, 11, …} λ is the empty string containing no characters

This recursive definition allows us to easily define important string operations Definition: The concatenation of two strings can be defined as follows: Basis step: if w ∈ ∑*, then w⋄λ ∈ ∑* Recursive step: if w1 ∈ ∑*, w2 ∈ ∑*, and x ∈ ∑, then w1⋄(w2x) = (w1⋄w2)x Example: Concatenate the strings “Hello” and “World” Hello⋄World = (Hello⋄Worl)d = (Hello⋄Wor)ld = (Hello⋄Wo)rld = (Hello⋄W)orld … = HelloWorld

This recursive definition allows us to easily define important string operations Definition: The length l(w) of a string can be defined as follows: Basis step: l(λ) = 0 Recursive step: l(wx) = l(w) + 1 if w ∈ ∑* and x ∈ ∑ Example: l(1001) = l(100) + 1 = l(10) + 1 + 1 = l(1) + 1 + 1 + 1 = l(λ) + 1 + 1 + 1 + 1 = 0 + 1 + 1 + 1 + 1 = 4

We can define sets of well-formed formulae recursively This is often used to specify the operations permissible in a given formal language (e.g., a programming language) Example: Defining propositional logic Basis step: T, F, and s are well-formed propositional logic statements (where s is a propositional variable) Recursive step: If E and F are well-formed statements, so are (¬E) (E ∧ F) (E ∨ F) (E → F) (E ↔ F)

✔ Example Question: Is ((p ∧ q) → (((¬r) ∨ q) ∧ t)) well-formed? Basis tells us that p, q, r, t are well-formed 1st application: (p ∧ q), (¬r) are well-formed 2nd application: ((¬r) ∧ q) is well-formed 3rd application: (((¬r) ∨ q) ∧ t) 4th application: ((p ∧ q) → (((¬r) ∨ q) ∧ t)) is well-formed ✔

Group work! Problem 1: Construct a recursive definition of the sequence {an} where the nth term is a natural number computed by adding the (n-1)st term to twice the (n-3)rd term. Assume that the first three terms of this sequence are 1, 1, 1. Problem 2: Construct a recursive definition of the function r() that reverses a string. E.g., r(cat) = tac.

Like other forms of induction, structural induction requires that we consider two cases Basis step: Show that the result holds for the objects specified in the basis case of the recursive definition Recursive step: Show that if the result holds for the objects used to construct new elements using the recursive step of the definition, then it holds for the new object as well. To see how this works, let’s revisit string length…

Recall from earlier… Definition: The length l(w) of a string can be defined as follows: Basis step: l(λ) = 0 Recursive step: l(wx) = l(w) + 1 if w ∈ ∑* and x ∈ ∑ Example: l(1001) = l(100) + 1 = l(10) + 1 + 1 = l(1) + 1 + 1 + 1 = l(λ) + 1 + 1 + 1 + 1 = 0 + 1 + 1 + 1 + 1 = 4

Prove that l(xy) = l(x) + l(y) for x,y ∈ ∑* P(n) ≡ l(xy) = l(x) + l(y) whenever x ∈ ∑* and l(y) = n ✔ Base case: P(0): l(xλ) = l(x) = l(x) + 0 = l(x) + l(λ) I.H.: Assume that P(k) holds for an arbitrary integer k Inductive step: We will now show that P(k) → P(k+1) Consider the string xya, where x,y ∈ ∑*, a ∈ ∑ and l(y) = k l(xya) = l(xy) + 1 by the recursive definition of l() = l(x) + l(y) + 1 by the I.H. Since l(ya) = l(y) + 1 by the recursive defintion of l(), we have that l(xya) = l(x) + l(ya), where ya is a string of size k+1 Conclusion: Since we have proved the base case and the inductive case, the claim holds by structural induction ❏

Many common data structures used in computer science have recursive definitions Example: Rooted Trees Base step: A single node is a rooted tree Recursive step: If T1, T2, …, Tn are disjoint rooted trees with roots r1, r2, …, rn then introducing a new root r connected to r1, r2, …, rn forms a new rooted tree. Define nodes, edges, children, parents, etc

Example Rooted Trees … … … Base case: One application: Two applications: … … …

Many common data structures used in computer science have recursive definitions Example: Extended binary trees Base step: The empty set is an extended binary tree Recursive step: If T1 and T2 are disjoint extended binary trees with roots r1 and r2, then introducing a new root r connected to r1 and r2 forms a new extended binary tree.

Example Extended Binary Trees Base case: ∅ Step 1: Step 2: Step 3: … …

Many common data structures used in computer science have recursive definitions Example: Full binary trees Base step: A single root node r is a full binary tree Recursive step: If T1 and T2 are disjoint full binary trees with roots r1 and r2, then introducing a new root r connected to r1 and r2 forms a new full binary tree. Diff between full binary trees and extended binary trees is all in the base case

Example Full Binary Trees Base case: Step 1: Step 2: …

Trees are used to parse expressions (((3 + 6) × 7) – (4 + 2)) × 8 456 × 8 - 7 + 2 4 6 3 57 63 6 9

Trees are used to enable fast searches Consider the set S = {56, 22, 34, 89, 99, 77, 16} 34 < 56 262 > 56 56 22 89 262 > 89 34 > 22 16 34 77 99 Question: Is 34 ∈ S? Question: Is 262 ∈ S? YES! NO!

As with other recursively defined objects, we can define many properties of trees recursively Definition: Given a tree T, we can define the height of T recursively, as follows: Basis step: If T consists only of the root node r, then h(T) = 0 Recursive step: If T consists of a root r that connects to subtrees T1, …, Tn, then h(T) = 1 + max(h(T1), …, h(Tn)) Example: What is the height of this tree T? h(T) = 1 + max(L, R) = 3 h(L) = 1 + h(L1) = 2 h(R) = 1 + h(R1) = 1 h(L1) = 1 + max(h(L11), h(L12)) = 1 h(R1) = 0 h(L11) = 0 h(L12) = 0

If T is a full binary tree, then the number of nodes in T (denoted n(T)) is less than or equal to 2h(T)+1-1 Claim: n(T) ≤ 2h(T)+1-1 Base case: T contains only a root node. In this case n(T) = 1 and h(T) = 0. Note that 20+1-1 = 1, so the claim holds. I.H.: Assume that claim holds for a tree of height k Inductive step: Show that the claim holds for trees of height k+1 Let T1 and T2 be disjoint full binary trees of height k By the I.H., n(T1) ≤ 2h(T1)+1-1 and n(T2) ≤ 2h(T2)+1-1 Let r be a unique root element, and let T be the tree formed using r as a root, T1 as the left subtree of r, and T2 as the right subtree of r r T2 T1

If T is a full binary tree, then the number of nodes in T (denoted num(T)) is less than or equal to 2h(T)+1-1 r T2 T1 Inductive step (cont): We have that n(T) = 1 + n(T1) + n(T2) by recursive formula of n(T) ≤ 1 + 2h(T1)+1 - 1 + 2h(T2)+1 - 1 by I.H. ≤ 2h(T1)+1 + 2h(T2)+1 – 1 ≤ 2 × max(2h(T1)+1, 2h(T2)+1) – 1 sum of 2 terms ≤ twice larger term ≤ 2 × 2max(h(T1), h(T2))+1 – 1 max(2x, 2y) = 2max(x,y) ≤ 2 × 2h(T) – 1 by recursive def’n of h(T) ≤ 2h(T)+1 – 1 Conclusion: Since we have proved the base case and the inductive case, the claim holds by structural induction ❏

Group work! Problem 3: Use structural induction to prove that checking whether some number is contained in a binary search tree T involves at most 2h(T)-1 comparison operations.

Final Thoughts Structural induction can be used to prove properties of recursive Functions Sequences Sets Data structures Next time, we start learning about counting and combinatorics (Section 5.1)

Recursively defined sets are also used frequently in computer science Simple example: Consider the following set S Basis step: 3 ∈ S Recursive step: if x ∈ S and y ∈ S, then x + y ∈ S Claim: The set S thus contains every multiple of 3. Intuition: 3 ∈ S, 6 ∈ S (since 3 and 3 are in S), 9 ∈ S (since 3 and 6 are in S), … We’ll show how we can prove this claim during the next lecture… Start with a “toy” set, work on to more realistic things

Recall from last lecture… Simple example: Consider the following set S Basis step: 3 ∈ S Recursive step: if x ∈ S and y ∈ S, then x + y ∈ S Claim: The set S thus contains exactly the set of all positive multiples of 3. Let the set A contain all positive multiples of 3. To prove the above claim, we need to show A ⊆ S and S ⊆ A. We can do this using structural induction!

The set S contains exactly the set of all positive integer multiples of 3 First, we’ll prove that A ⊆ S P(n) ≡ 3n ∈ S ✔ Base case: P(1): 3 ∈ S by definition I.H.: Assume that P(k) holds for an arbitrary integer k Inductive step: We will now show that P(k) → P(k+1) 3k ∈ S by I.H. 3 ∈ S by base case 3k + 3 ∈ S by recursive definition of S 3(k+1) ∈ S Conclusion: Since we have proved the base case and the inductive case, A ⊆ S ❏

The set S contains exactly the set of all positive integer multiples of 3 Now, we’ll prove that S ⊆ A P(n): All numbers generated using n applications of the recursive definition of S are divisible by 3. ✔ Base case: P(0): 3 ∈ S by base case for S, and 3 | 3. I.H.: Assume that P(k) holds for an arbitrary integer k Inductive step: We will now show that P(k) → P(k+1) Assume that x and y are elements of S, x is generated using k applications of the recursive definition of S, and y is generated using no more than k applications of the recursive definition of S Therefore, x+y ∈ S after k+1 applications of S’s definition Since 3|x and 3|y by the I.H., 3|(x+y) Conclusion: Since we have proved the base case and the inductive case, S ⊆ A ❏ Since S ⊆ A and A ⊆ S, the claim holds.