Recursive Definitions

Similar documents
Counting Methods. CSE 191, Class Note 05: Counting Methods Computer Sci & Eng Dept SUNY Buffalo

Math.3336: Discrete Mathematics. Combinatorics: Basics of Counting

Counting. Spock's dilemma (Walls and mirrors) call it C(n,k) Rosen, Chapter 5.1, 5.2, 5.3 Walls and Mirrors, Chapter 3 10/11/12

Induction and recursion. Chapter 5

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

1. Consider the conditional E = p q r. Use de Morgan s laws to write simplified versions of the following : The negation of E : 5 points

Induction and Recursion

Counting. Mukulika Ghosh. Fall Based on slides by Dr. Hyunyoung Lee

Lecture 4: Counting, Pigeonhole Principle, Permutations, Combinations Lecturer: Lale Özkahya

Announcements. CSE 321 Discrete Structures. Counting. Counting Rules. Important cases of the Product Rule. Counting examples.

Introduction to Decision Sciences Lecture 10

ICS141: Discrete Mathematics for Computer Science I

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

With Question/Answer Animations

Counting. Math 301. November 24, Dr. Nahid Sultana

Department of Computer Science University at Albany, State University of New York Solutions to Sample Discrete Mathematics Examination II (Fall 2007)

Introduction to Decision Sciences Lecture 11

Homework 7 Solutions, Math 55

Applied Cryptography and Computer Security CSE 664 Spring 2017

Discrete Mathematics & Mathematical Reasoning Chapter 6: Counting

MATH 10B METHODS OF MATHEMATICS: CALCULUS, STATISTICS AND COMBINATORICS

PROBLEMS ON CONGRUENCES AND DIVISIBILITY

Definition: Let S and T be sets. A binary relation on SxT is any subset of SxT. A binary relation on S is any subset of SxS.

Counting Review R 1. R 3 receiver. sender R 4 R 2

2!3! (9)( 8x3 ) = 960x 3 ( 720x 3 ) = 1680x 3

An Introduction to Combinatorics

Recurrence Relations and Recursion: MATH 180

Algorithms: Background

Notes. Combinatorics. Combinatorics II. Notes. Notes. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

Chapter 8 Sequences, Series, and Probability

Slides for a Course Based on the Text Discrete Mathematics & Its Applications (5 th Edition) by Kenneth H. Rosen

Putnam Pigeonhole Principle October 25, 2005

F 2k 1 = F 2n. for all positive integers n.

My Favorite Problems, 2 Harold B. Reiter University of North Carolina Charlotte

Discrete Mathematics with Applications MATH236

Remainders. We learned how to multiply and divide in elementary

MATH 114 Fall 2004 Solutions to practice problems for Final Exam

Introduction to Number Theory. The study of the integers

6.8 The Pigeonhole Principle

International Mathematical Olympiad. Preliminary Selection Contest 2011 Hong Kong. Outline of Solutions

Math 230 Final Exam, Spring 2008

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

PRACTICE PROBLEMS: SET 1

Fall 2017 Test II review problems

PUTNAM TRAINING NUMBER THEORY. Exercises 1. Show that the sum of two consecutive primes is never twice a prime.

Solution: By direct calculation, or observe that = = ( ) 2222 = =

CHAPTER 8 Advanced Counting Techniques

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

Algorithms (II) Yu Yu. Shanghai Jiaotong University

Discrete Probability

Discrete Structures, Final Exam

1 Sequences and Summation

Combinatorics. But there are some standard techniques. That s what we ll be studying.

2 - Strings and Binomial Coefficients

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

Mat Week 8. Week 8. gcd() Mat Bases. Integers & Computers. Linear Combos. Week 8. Induction Proofs. Fall 2013

DISCRETE STRUCTURES AMIN WITNO

CS 210 Foundations of Computer Science

Discrete Structures for Computer Science: Counting, Recursion, and Probability

1. Induction on Strings

Carleton University. Final Examination Fall DURATION: 2 HOURS No. of students: 223

Recursion and Induction

Student Responsibilities Week 8. Mat Section 3.6 Integers and Algorithms. Algorithm to Find gcd()

Basic Combinatorics. Math 40210, Section 01 Fall Homework 8 Solutions

Computer Organization I Test 1/Version 1 CMSC 2833 Autumn 2007

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

not to be republished NCERT REAL NUMBERS CHAPTER 1 (A) Main Concepts and Results

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

1 The Basic Counting Principles

CS1800 Discrete Structures Final Version A

Discrete Mathematics: Midterm Test with Answers. Professor Callahan, section (A or B): Name: NetID: 30 multiple choice, 3 points each:

Number Theory: Representations of Integers

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Binomial Coefficient Identities/Complements

Lecture 3: Miscellaneous Techniques

The Pigeonhole Principle

1 True/False. Math 10B with Professor Stankova Worksheet, Discussion #9; Thursday, 2/15/2018 GSI name: Roy Zhao

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

MATH 243E Test #3 Solutions

Carleton University. Final Examination Winter DURATION: 2 HOURS No. of students: 152

Assignment #2 COMP 3200 Spring 2012 Prof. Stucki

Undergraduate Notes in Mathematics. Arkansas Tech University Department of Mathematics. Introductory Notes in Discrete Mathematics Solution Guide

Logic. Facts (with proofs) CHAPTER 1. Definitions

Discrete Structures CRN Test 3 Version 1 CMSC 2123 Autumn 2013

8. Given a rational number r, prove that there exist coprime integers p and q, with q 0, so that r = p q. . For all n N, f n = an b n 2

Strong Induction (Second Principle) Example: There are two piles of cards, players take turn: each turn: one player removes any number of cards from

ALGEBRA+NUMBER THEORY +COMBINATORICS

Mathematical Background

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CSC 125 :: Final Exam December 15, 2010

BOUNDS ON ZIMIN WORD AVOIDANCE

Johns Hopkins Math Tournament Proof Round: Automata

Executive Assessment. Executive Assessment Math Review. Section 1.0, Arithmetic, includes the following topics:

Discrete Mathematics. Benny George K. September 22, 2011

CS Foundations of Computing

{ 10,12,17} { 4, 7,13}


Monty Hall Puzzle. Draw a tree diagram of possible choices (a possibility tree ) One for each strategy switch or no-switch

CSci 311, Models of Computation Chapter 4 Properties of Regular Languages

Discrete Mathematics (CS503)

Transcription:

Recursive Definitions Example: Give a recursive definition of a n. a R and n N. Basis: n = 0, a 0 = 1. Recursion: a n+1 = a a n. Example: Give a recursive definition of n i=0 a i. Let S n = n i=0 a i, then: Basis: n = 0, S 0 = 0 i=0 a i = a 0. Recursion: S n+1 = S n + a n+1. 1

Example: Give a recursive definition of GCD(a, b), a < b, a, b are integers not both equal to 0. Basis: a = 0, GCD(a, b) = b. Recursion: GCD(a, b) = GCD(b mod a, a). Example: Recursive definition of Fibonacci numbers: f 0 = 0, f 1 = 1 f n = f n 1 + f n 2. Recursive programming is not always efficient. Computing the Fibonacci numbers by iterative approach is more efficient than by recursive programming. 2

Lame s Theorem: Let a, b Z + with a b. Then the number of divisions used by the Euclidean algorithm to find GCD(a, b) is five times the number of decimal digits in b, or it is O(log b). Proof. Let a = r 0, b = r 1. Use n steps of the Euclidean algorithm to find gcd(a, b). r 0 = r 1 q 1 + r 2 r 1 = r 2 q 2 + r 3... r n 2 = r n 1 q n 1 + r n r n 1 = r n q n where q 1,..., q n 1 1 and q n 2 since r n < r n 1. Thus r n 1 = f 2 r n 1 2r n 2f 2 = f 3 r n 2 r n 1 + r n f 3 + f 2 = f 4... r 2 r 3 + r 4 f n 1 + f n 2 = f n b = r 1 r 2 + r 3 f n + f n 1 = f n+1. 3

So after n divisions assuming a b we have b f n+1 but by the previous result f n+1 > α n 1, n > 2, α = 1+ 5 2, so b > α n 1. Since we have ( ) 1 + 5 log 10 α = log 10 2 > 1/5 log 10 b > (n 1) log 10 α > (n 1)/5 or n 1 < 5 log 10 b. Suppose that b has k decimal digits. Then b < 10 k log 10 b < k. Hence n 1 < 5k or n 5k (since k is an integer) thus # of divisions 5k. Also 10 k 1 b k log 10 +1 so # of divisions is bounded above by 5(log 10 + 1) = O(log b). 4

Example: Recursive definition of sets 3 S x + y S if x S and y S Then S is the set of positive integers divisible by 3. Example: Recursive definition of the set of strings. A string over an alphabet is a finite sequence of symbols from. Let denote the set of all strings over. String x and y can be concatenated to form string xy. 5

Basis: λ = φ empty string. Recursion: wx if w and x. That is concatenating a string in and a symbol in. Example: string. Recursive definition of the reversal of a Basis: λ R = λ Recursion: (ux) R = xu R for x, u. Problem: Prove that (w 1 w 2 ) R = w R 2 wr 1. 6

Recursive Algorithms An algorithm is recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example: Recursive algorithm to compute gcd(a, b). if a = 0 then gcd(a, b) := b else gcd(a, b) := gcd(b mod a, a) Student should look at many other recursive algorithms in the Textbook. 7

Counting Suppose we want to find out how many passwords are possible if a password consists of 6, 7, or 8 characters (A-Z, and 0-9), and there is at least one digit in each password. Basic Counting Principles The sum rule: If the first task is done in n 1 ways, second task in n 2 ways and they are independent, then the number of ways to do the 2 tasks is n 1 + n 2. Example: The students can choose a project from 3 lists, the first list has 15 projects, the second list has 13 and the third list has 20. How many ways a student can choose a project from three lists? 8

The product rule: If a task can be broken into 2 sub-tasks. Sub-task 1 has n 1 ways to do and sub-task 2 has n 2 ways to do after sub-task 1. Then there are n 1 n 2 ways to do the task. Example: The chairs in this auditorium are labelled with a letter and a positive integer 10. What is the number of chairs in the auditorium? Example: How many different bit strings are there of length 8? Example: Each license plate has 3 letters followed by 3 digits. How many licence plates are possible? 9

Example: A password has 6, 7 or 8 characters. A character can be a letter A Z or a digit 0 9. There is at least one digit in each password. How many passwords are there? Let P = P 6 + P 7 + P 8 P 6 = 36 6 26 6 P 7 = 36 7 26 7 P 8 = 36 8 26 8 P = 2, 684, 483, 063, 360 Note: see more examples in the textbook. 10

The inclusion-exclusion principle: avoid over counting. A 1 A 2 = A 1 + A 2 A 1 A 2. Example: How many bit strings of length 8, either start with a 1 or (inclusive or) end with 00? Inclusion: number of bit strings of length 8 and start with a 1: 2 7 = 128 number of bit strings of length 8 and end with 00: 2 6 = 64 Total inclusion: 128 + 64 = 192 But we counted twice bit strings starting with 1 and ending with 00: Exclusion: 2 5 = 32 Result = 128 + 64 32 = 160 bit strings. 11

Example: How many bit strings of length 8 that have: a) 3 consecutive 0s? b) 4 consecutive 1s? c) 3 consecutive 0s or 4 consecutive 1s? 3 consecutive 0s case 1 2 3 4 5 6 7 8 1 0 0 0 x x x x x 2 5 = 32 2 1 0 0 0 x x x x 2 4 = 16 3 x 1 0 0 0 x x x 2 4 = 16 4 x x 1 0 0 0 x x 2 4 = 16 5 y y y 1 0 0 0 x (2 3 1) 2 = 14 6 z z z z 1 0 0 0 2 4 3 = 13 total 107 yyy - any binary string except 000 (case 1) zzzz - any binary string except 0000, 0001 (case 1) or 1000 (case 2) 12

4 consecutive 1s case 1 2 3 4 5 6 7 8 1 1 1 1 1 x x x x 2 4 = 16 2 0 1 1 1 1 x x x 2 3 = 8 3 0 1 1 1 1 1 x x 2 3 = 8 4 x x 0 1 1 1 1 x 2 3 = 8 5 x x x 0 1 1 1 1 2 3 = 8 total 48 3 consecutive 0s and 4 consecutive 1s case 1 2 3 4 5 6 7 8 1 1 1 1 1 0 0 0 1 2 0 0 0 0 1 1 1 1 3 0 0 0 1 1 1 1 1 4 0 0 0 1 1 1 1 0 5 1 1 1 1 0 0 0 0 6 1 1 1 1 1 0 0 0 7 0 1 1 1 1 0 0 0 8 1 0 0 0 1 1 1 1 Total 8.

3 consecutive 0s or 4 consecutive 1s By inclusion-exclusion the answer is 107 + 48 8 = 147 Example: a) How many bit strings of length 10 containing 5 consecutive 0s? b) How many bit strings of length 10 containing exactly 5 consecutive 0s and not more?

Counting functions Example: How many functions are there from m elements to n elements? Example: How many one-to-one functions are there from m elements to n elements? Tree diagrams: a useful technique for counting small number of cases. You need different tools for different problems. Example: How many bit strings of length 4 that do not have consecutive 1s? Inclusion-exclusion is difficult to apply in this problem. 13

Example: A playoff between 2 teams consists of at most 5 games. The first team that wins 3 games wins the playoff. In how many different ways the playoff can occur? 14

The Pigeonhole Principle If k + 1 or more objects are placed in k boxes then there is at least one box containing 2 or more objects. Example: For 366 persons (assume 365 days in a year) at least 2 have the same birthday. The Generalized Pigeonhole Principle: If N objects are placed in k boxes then there is at least one box containing at least N k objects. Applications Example: Show that among any n + 1 positive integers 2n there must be one integer that divides one of the others. Proof. Write each of n + 1 positive integers as a j = 2 k jq j, j = 1,..., n + 1, where k j 0 and q j is odd. Integers q j, j = 1,..., n+1 are odd, positive and < 2n. Since there are only n such integers we have by the pigeonhole principle that at least n+1 n = 2 of the integers q j, j = 1,..., n + 1 must be equal, e. g., q i = q j = q, i j. Then a i = 2 k iq, a j = 2 k jq, hence a i a j if k i k j or a j a i if k i > k j 15

Theorem: Every sequence of n 2 +1 distinct real numbers contains a subsequence of length n + 1 that is either strictly increasing or strictly decreasing. Proof. Let a 1,..., a n 2 +1 be a sequence of n2 + 1 distinct real numbers. Associate with a k a pair of (i k, d k ), where i k is the length of the longest increasing subsequence starting at a k, and d k is the length of the longest decreasing subsequence starting at a k. Suppose to the contrary that there are no increasing or decreasing subsequences of length n + 1. Then i k, d k are positive integers less or equal to n, k = 1,..., a n 2 +1. Hence, by the product rule there are up to n 2 different ordered pairs (i k, d k ). Since there are n 2 + 1 pairs (i k, d k ) by the pigeonhole principle at least two of these ordered pairs are equal. That means there exist a s and a t, with s < t such that i s = i t and d s = d t. This is impossible. Indeed, because all the terms in the sequence are different, either a s < a t or a s > a t. If a s < a t, then since i s = i t, an increasing subsequence of length i s + 1 can be built starting at a s, by taking a s followed by an increasing sequence of length i t = i s starting at a t. This is contradiction. Similarly, if a s > a t, then we can obtain a decreasing subsequence of length d s + 1 by starting at a s, then 16

continuing to a t followed by a decreasing subsequence of length d t = d s starting at a t. This leads to a contradiction. Example: Assume a group of 6 people each pair consists of 2 friends or 2 enemies. Show that there are either 3 mutual friends or 3 mutual enemies. This is an example of a Ramsey theory problem. Proof. Let the group of 6 people be {A, B, C, D, E, F }. Choose A. In the remaining group of 5 people {B, C, D, E, F by the pigeonhole principle there are either three friends of A or three enemies of A, since 5 3 = 3. Suppose that B,C,D are enemies of A. If any two of these three people are enemies, say C,D then A,C,D form a group of three mutual enemies. Otherwise A,C,D form a group of three mutual friends. The proof in a latter case proceeds similarly.