CSI Mathematical Induction. Many statements assert that a property of the form P(n) is true for all integers n.

Similar documents
Discrete Mathematics. Spring 2017

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

CS 220: Discrete Structures and their Applications. Mathematical Induction in zybooks

With Question/Answer Animations

Induction and Recursion

Induction and recursion. Chapter 5

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

ICS141: Discrete Mathematics for Computer Science I

Induction and recursion. Topics. Induction and Recursion Vojislav Kecman CMSC 302

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 1

Mathematical Induction. Rosen Chapter 4.1,4.2 (6 th edition) Rosen Ch. 5.1, 5.2 (7 th edition)

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Note 3

What can you prove by induction?

MATH 55 - HOMEWORK 6 SOLUTIONS. 1. Section = 1 = (n + 1) 3 = 2. + (n + 1) 3. + (n + 1) 3 = n2 (n + 1) 2.

Exercises. Template for Proofs by Mathematical Induction

1 Sequences and Summation

Proof Techniques (Review of Math 271)

Lecture 4: Probability, Proof Techniques, Method of Induction Lecturer: Lale Özkahya

Recursion: Introduction and Correctness

We want to show P (n) is true for all integers

Use mathematical induction in Exercises 3 17 to prove summation formulae. Be sure to identify where you use the inductive hypothesis.

Reading and Writing. Mathematical Proofs. Slides by Arthur van Goetham

CSC236H Lecture 2. Ilir Dema. September 19, 2018

CSE 311: Foundations of Computing. Lecture 14: Induction

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

CMPSCI 250: Introduction to Computation. Lecture 11: Proof Techniques David Mix Barrington 5 March 2013

Sum of Squares. Defining Functions. Closed-Form Expression for SQ(n)

Hence, the sequence of triangular numbers is given by., the. n th square number, is the sum of the first. S n

Binomial Coefficient Identities/Complements

CSC236 Week 3. Larry Zhang

EECS 1028 M: Discrete Mathematics for Engineers

Mathematical Induction

At the start of the term, we saw the following formula for computing the sum of the first n integers:

Discrete Mathematics for CS Fall 2003 Wagner Lecture 3. Strong induction

CS 173: Induction. Madhusudan Parthasarathy University of Illinois at Urbana-Champaign. February 7, 2016

Discrete Mathematics. Spring 2017

CS325: Analysis of Algorithms, Fall Midterm

Exercises 1 - Solutions

CSCE 222 Discrete Structures for Computing. Review for the Final. Hyunyoung Lee

Induction. Announcements. Overview. Defining Functions. Sum of Squares. Closed-form expression for SQ(n) There have been some corrections to A1

3.1 Induction: An informal introduction

MAT115A-21 COMPLETE LECTURE NOTES

Reading 5 : Induction

Carmen s Core Concepts (Math 135)

Lecture Notes on DISCRETE MATHEMATICS. Eusebius Doedel

Module 9: Mathematical Induction

Theory of Computation

n(n + 1). 2 . If n = 3, then 1+2+3=6= 3(3+1) . If n = 2, then = 3 = 2(2+1)

Problem Set 5 Solutions

Chapter 2 Section 2.1: Proofs Proof Techniques. CS 130 Discrete Structures

Mathematical Induction. Section 5.1

x P(x) x P(x) CSE 311: Foundations of Computing announcements last time: quantifiers, review: logical Inference Fall 2013 Lecture 7: Proofs

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Complete Induction and the Well- Ordering Principle

Mathematical Induction

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

Consider an infinite row of dominoes, labeled by 1, 2, 3,, where each domino is standing up. What should one do to knock over all dominoes?

0.Axioms for the Integers 1

Climbing an Infinite Ladder

PUTNAM TRAINING MATHEMATICAL INDUCTION. Exercises

Lecture 6 : Induction DRAFT

Math 10850, fall 2017, University of Notre Dame

Outline. We will cover (over the next few weeks) Induction Strong Induction Constructive Induction Structural Induction

21 Induction. Tom Lewis. Fall Term Tom Lewis () 21 Induction Fall Term / 14

I. Induction. Po-Shen Loh June 16, 2003

CPSC 121: Models of Computation. Module 9: Proof Techniques (part 2) Mathematical Induction

Putnam Greedy Algorithms Cody Johnson. Greedy Algorithms. May 30, 2016 Cody Johnson.

Announcements. Read Section 2.1 (Sets), 2.2 (Set Operations) and 5.1 (Mathematical Induction) Existence Proofs. Non-constructive

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CHAPTER 5 Induction and Recursion

n n P} is a bounded subset Proof. Let A be a nonempty subset of Z, bounded above. Define the set

Introduction to Metalogic

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

LECTURE NOTES DISCRETE MATHEMATICS. Eusebius Doedel

Note that r = 0 gives the simple principle of induction. Also it can be shown that the principle of strong induction follows from simple induction.

Notes on induction proofs and recursive definitions

4.1 Induction: An informal introduction

Foundations of Mathematics MATH 220 FALL 2017 Lecture Notes

Mathematical Induction. Defining Functions. Overview. Notation for recursive functions. Base case Sn(0) = 0 S(n) = S(n 1) + n for n > 0

Review 3. Andreas Klappenecker

CHAPTER 5. Section 5.1

Lecture 7 Feb 4, 14. Sections 1.7 and 1.8 Some problems from Sec 1.8

Math 230 Final Exam, Spring 2008

Mathematical Fundamentals

At least one of us is a knave. What are A and B?

Another Proof By Contradiction: 2 is Irrational

Review for Midterm 1. Andreas Klappenecker

CSE 2001: Introduction to Theory of Computation Fall Suprakash Datta

Finite and Infinite Sets

CSCE 222 Discrete Structures for Computing. Proofs. Dr. Hyunyoung Lee. !!!!! Based on slides by Andreas Klappenecker

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Note 3

Recurrence Relations and Recursion: MATH 180

Mathematical Induction. EECS 203: Discrete Mathematics Lecture 11 Spring

Steinhardt School of Culture, Education, and Human Development Department of Teaching and Learning. Mathematical Proof and Proving (MPP)

k 6, then which of the following statements can you assume to be

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

Models of Computation,

Transcription:

CSI 2101- Mathematical Induction Many statements assert that a property of the form P(n) is true for all integers n. Examples: For every positive integer n: n! n n Every set with n elements, has 2 n Subsets. Induction is one of the most important techniques for proving statements of that form. Dr-Zaguia-CSI2101-W08 1

Mathematical Induction For example, consider the following algorithm: sum = 0; for(i=1; i n; i++) { sum = sum + 2*i-1; } What is its output? n=1 1 n=2 1+3 = 4 n=3 1+3+5 = 9 n=4 1+3+5+7 = 16 We suspect that the output is n 2 but how to prove it? Dr-Zaguia-CSI2101-W08 2

Mathematical Induction Use induction to prove that the sum of the first n odd integers is n 2. What s the hypothesis? P(n) sum of first n odd integers = n 2. Base case (n=1): the sum of the Prove first a 1 base odd integer case (n=1) is 1 2. Since 1 = 1 2 Prove P(k) P(k+1) Inductive Step: show that (k>=1) P(k) P(k+1). How? Let k>=1. Assume P(k): the sum of the first k odd integers is k 2. That is assume that 1 + 3 + + (2k - 1) = k 2 And prove P(k+1): the sum of the first (k+1) odd integers is (k+1) 2. 1 + 3 + + (2k-1) + (2k+1) = k 2 + (2k + 1) = (k+1) 2. Therefore P(k+1) is true = k 2 By inductive hypothesis QED Dr-Zaguia-CSI2101-W08 3

Mathematical Induction What did we do? basic step: prove P(1) inductive step: assume P(n) and prove P(n+1) (i.e prove P(n) P(n+1)) Mathematical Induction is a rule of inference that tells us: P(1) k (P(k) P(k+1)) -------------------------- n P(n) Why Mathematical Induction works? It is enough to prove that this rule of inference is valid Dr-Zaguia-CSI2101-W08 4

Mathematical Induction Well-Ordered-Principle Definition: A set S is well-ordered if every non-empty subset of S has a least element. Given (we take as an axiom): the set of natural numbers (N) is well-ordered. Is the set of integers (Z) well ordered? Is the set of non-negative reals (R) well ordered? No. { x R : x > 1 } has no least element. No. { x Z : x < 0 } has no least element. Dr-Zaguia-CSI2101-W08 5

Proof that Mathematical Induction Works By contradiction using the Well-Ordered-Principle. Assume that Mathematical Induction does not work. We assume that both hypothesis, i.e. the basic step P(1) and the induction step (P(k) P(k+1)) are both true but there still exists c such that P(c). Let S be the set of all elements x for which P(x). By the well ordered principle, S has a smallest element a. Because P(1), we know that a 1. Therefore we can consider b = a-1. Because a was the smallest element of S, b is not in S. Therefore P(b) holds. By modus ponens using the induction step, we get P(a), which is a contradiction. Dr-Zaguia-CSI2101-W08 6

Writing a Proof by Induction State the hypothesis very clearly: P(n) is true for all integers n b state the property P in English Identify the base case P(b) holds because Inductive Step - Assuming the inductive hypothesis P(k) for k>=b, prove that P(k+1) holds; i.e., P(k) P(k+1) Conclusion: By induction we have shown that P(k) holds for all k>b (b is what was used for the base case). Dr-Zaguia-CSI2101-W08 7

Mathematical Induction Another example: Use induction to prove that the 1 + 2 + 2 2 + + 2 n = 2 n+1 1, for all non-negative integers n. n 0 P(n) is true, where P(n): 1 + 2 + 2 2 + + 2 n = 2 n+1 1 1 - Base case n = 0 1 0 = 2 1-1. Prove P(0) 2 Inductive Hypothesis Prove P(k) P(k+1) Assume P(k) for some k>=0, that is, 1 + 2 + 2 2 + + 2 k = 2 k+1 1 and prove P(k+1). 1 + 2 + 2 2 + + 2 k + 2 k+1 = (2 k+1-1)+ 2 k+1 1 = 2*2 k+1 1 = 2 k+2-1 = 2 k+1 Therefore P(k+1) is true 1 By inductive hypothesis Dr-Zaguia-CSI2101-W08 8

Mathematical Induction Another example: Prove P(n) using induction where P(n): a set S with n elements has 2 n subsets. Example: if S={1, 2, 3} then S has 8=2 3 subsets, these are {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} Proof by induction that P(n) is true for all n 0 1- Base case P(0): a set S with 0 elements has 2 0 =1 subsets. S={}, then S has a unique subset {} and thus P(0) is true Dr-Zaguia-CSI2101-W08 9

Mathematical Induction 2- Inductive Step: k P(k) P(k+1), i.e, assuming P(k) is true for some k>=0, we must show that P(k+1) is true. Let k>=0. Assume that any set with k elements has 2 k subsets. Let S be a set with k+1 elements. Thus, S = T {a}, where T is a set with k elements. For each subset X of T there are exactly two subsets of S, namely X and X {a}. Because there are 2 k subsets of S (inductive hypothesis), there are 2 2 k = 2 k+1 subsets of T. T x S S {a} x x {a} Therefore P(k+1) is true QED Generating subsets of a set S with k+1 elements from a set T with k elements Dr-Zaguia-CSI2101-W08 10

Mathematical Induction: Deficient Tiling A 2 n x 2 n sized grid is deficient if all but one cell is tiled. P(n): all 2^n x 2^n sized deficient grids can be tiled with right triominoes, which are pieces that cover three squares at a time, like this: 2 n 2 n We want to show that for all n 1, P(n) is true Dr-Zaguia-CSI2101-W08 11

Base Case: Mathematical Induction: Deficient Tiling P(1) - Is it true for 2 1 x 2 1 grids? YES Dr-Zaguia-CSI2101-W08 12

Mathematical Induction: Deficient Tiling Inductive Step: Let k>=1. We assume that we can tile a 2 k x 2 k deficient board using our designer tiles. We use this to prove that we can tile a 2 k+1 x 2 k+1 deficient board using our designer tiles. Dr-Zaguia-CSI2101-W08 13

Mathematical Induction: Deficient Tiling 2 k 2 k 2 k?? 2 k+1 2 k OK!! (by IH)? Dr-Zaguia-CSI2101-W08 14

Mathematical Induction: Deficient Tiling 2 k 2 k 2 k 2 k OK!! (by IH) OK!! (by IH) OK!! (by IH) OK!! (by IH) 2 k+1 Dr-Zaguia-CSI2101-W08 15

Mathematical Induction: Deficient Tiling Dr-Zaguia-CSI2101-W08 16

Mathematical Induction: Deficient Tiling So, we can tile a 2 k x 2 k deficient board using our designer tiles. What does this mean for 2 2k mod 3? = 1 (also do direct proof by induction) Dr-Zaguia-CSI2101-W08 17

Mathematical Induction Exercises Prove that a chess knight (horse) can visit every square in an infinite chessboard. Prove that n lines separate the plane into (n 2 +n+2)/2 regions if no two lanes are parallel and no three lines pass through the same point. Prove that ½ * ¾ * * (2n-1)/2n < 1/ 3n can t really prove it directly, but can prove a stronger statement ½ * ¾ * * (2n-1)/2n < 1/ (3n+1) sometimes called inductive loading Dr-Zaguia-CSI2101-W08 18

Mathematical Induction Common Errors What is wrong with the following proof of All horses are of the same colour Base step: One horse has one colour Induction step: Assume k horses have the same colour, we show that k+1 horses have the same colour. Take the first k horses of our k+1 horses. By induction hypothesis they are of the same colour. The same holds for the last k horses. As these two sets overlap, they both must be of the same colour, i.e. all k+1 horses are of the same colour. last k horses first k horses Dr-Zaguia-CSI2101-W08 19

Strong Induction P(n): a chocolate bar of size nx1 squares needs exactly n-1 breaks to break it into the basic squares. Base case (n=1): you need 0 breaks and n-1 =0. Thus P(1) true Inductive Step: show that (k>=1) P(k) P(k+1). Assume P(k) for some k>=1: a chocolate bar of size kx1 squares needs exactly k-1 breaks to break it into the basic squares. and deduce P(k+1). We break the (k+1)x1 chocol. bar into 2 pieces with sizes m and (k+1)-m. Using the inductive hypothesis..??? How can we use the inductive hypothesis P(k)??? We have a problem! Use strong induction. Dr-Zaguia-CSI2101-W08 20

Strong Induction Normal induction: To prove that P(n) is true for all positive integers n: Base step: prove P(1) Induction step: prove P(n) P(n+1) Strong induction: To prove that P(n) is true for all positive integers n: Base step: prove P(1) Induction step: prove P(1) P(2) P(n) P(n+1) Dr-Zaguia-CSI2101-W08 21

Strong Induction P(n): a chocolate bar of size nx1 squares needs exactly n-1 breaks to break it into the basic squares. Using Strong induction: Base case (n=1): you need 0 breaks and n-1 =0. Thus P(1) true Inductive Step: show that (k>=1) P(1) P(2) P(k) P(k+1). We break the (k+1)x1 chocolate bar into 2 pieces with sizes m and (k+1)-m. Since both m and (k+1-m) are grether than 0 and less than k+1, then by the strong induction hypothesis, we need m-1 breaks for the first piece and (k+1-m)-1= k-m for the second piece. So in total we will need (m-1) + (k-m) + 1= k = (k+1)-1 breaks for the chocolate bar with size k+1 The break used the first time Dr-Zaguia-CSI2101-W08 22

Strong Induction Prove the following using strong induction; P(n): each postage of n cents with n at least 18 can be paid by 4c and 7c stamps. Show that if in a round-robin tournament there exists a cycle of player A beats player B, then there must be a cycle of length 3. Theorem: Every simple polygon of n sides can be triangulated into n-2 triangles. Theorem: Every triangulation of a simple polygon of n 4 sides has at least two triangles in the triangulation with two edges on the sides of the polygon. Theorem: Show that there is a rational number between any two real numbers. Dr-Zaguia-CSI2101-W08 23

Recursive definition (note 2009: topic delayed until after midterm) Recursion is the general term for the practice of defining an object in terms of itself or of part of itself This may seem circular, but it isn t necessarily. An inductive proof establishes the truth of P( n+1) recursively in terms of P(n). There are also recursive algorithms, definitions, functions, sequences, sets, and other structures Dr-Zaguia-CSI2101-W08 24

Recursive definition Recursive (Inductive) Definition of a Function: Define f(1) (perhaps also f(2), f(3) f(k) for some constant k) Define f(n+1) using f(i) for i smaller then n+1 Example 1: f(1) = 2, f(n+1) = 2f(n) What is the explicit value of f(n)? Example 2: g(1) = 1, g(n+1) = (n+1)g(n) What is the explicit value of g(n)? We can guess the solution and then use proof by induction to do a formal check Dr-Zaguia-CSI2101-W08 25

Recursive definition Example 1: f(1) = 2, f(n+1) = 2f(n) What is the explicit value of f(n)? Proof by induction that f(n) = 2 n for every n 1. (what is P(k)?) Base step: n=1 f(1)= 2 = 2 1. True Induct. step: Let k>=1.assume that f(k) = 2 k and deduce that f(k+1)=2 k+1 By definition of the function f, f(k+1) = 2 f(k). By induction hypothesis f(k) = 2 k and therefore f(k+1) = 2*2 k = 2 k+1. This finishes the inductive step. Example 2: g(1) = 1, g(n+1) = (n+1)g(n) Proof by induction that g(n) = n! for every n 1. (what is P(k)?) Base step: n=1 g(1)= 1 = 1!. True. Induct. step: Let k >=1. Assume that g(k) = k! and deduce that g( k+1)=(k+1)! By definition of the function g, g(k+1) = (k+1) f(k). By induction hypothesis g(k) = k! and therefore g(k+1) = (k+1)*k!= (k+1)!. This finishes the inductive step. Dr-Zaguia-CSI2101-W08 26

Recursive definition Example 3: f(n) Fibonacci numbers f(1) = 1, f(2) = 1, f(n+1) = f(n) + f(n-1) How fast do the Fibonacci numbers grow? Theorem: n 3, f(n) > α (n-2) where α=(1+ 5)/2 Proof: By induction. How can we prove this? (what is P(k)?) (~1.61,2.61) base step: n = 3: f(3) = 2 > α, f(4) = 3 >(3+ 5)/2 = α 2 induction step: Let n >= 4. note that α 2 = α+1, since α is a root of x 2 - x-1 = 0. Therefore α n-1 = α 2 α n-3 = (α+1)α n-3 = α n-2 +α n-3 by induction hypothesis, f(n) < α n-2, f(n-1)<α n-3, therefore as f(n +1) = f(n)+f(n-1), also f(n+1)<α n-2 +α n-3 = α n-1 Dr-Zaguia-CSI2101-W08 27

Careful with Recursive Definitions The function defined has to be well defined it is defined for each element of its domain (often positive integers) it is defined unambiguously (no two different values) Consider: F(n) = 1+F( n/2 ) for n 1 and F(1) = 1 F(n) = 1+F(n-2) for n 1 and F(1) = 0 Problems F(n) = 1+F(n/3) for even n 3, and F(1) = F(2) = 1 F(n) = 1+F(F(n-1)) for n 2 and F(1) = 2 Dr-Zaguia-CSI2101-W08 28

Recursively Defined Sets and Structures An infinite set S may be defined recursively, by giving: A small finite set of base elements of S. A rule for constructing new elements of S from previously -established elements. Implicitly, S has no other elements but these. Example: S is the subset of Z (integers) defined by: BASIS STEP: 3 S, and RECURSIVE STEP: if x,y S then x+y S. What is S? Dr-Zaguia-CSI2101-W08 29

Recursively Defined Sets and Structures Example: Set of strings Σ* over alphabet Σ: Base step: the empty string γ Σ* Induction step: If w Σ* and x Σ then also wx Σ* Dr-Zaguia-CSI2101-W08 30

Recursively Defined Sets and Structures Example: Let Σ be a set of symbols (the alphabet) and Σ* be a set of strings over this alphabet. Concatenation (denoted by. ) of two strings is recursively defined as follows: Base step: If w Σ* then, w.γ = w, where γ is the empty string Induction step: If w 1 Σ* and w 2 Σ* and x Σ, then w 1.(w 2 x)=(w1.w2)x Well-formed formulae of propositional logic: Base step: T, F and s, where s is a propositional variable, are wellformed formulaes Induction step: If E and F are well-formed formulae, then also ( E), (E F), (E F), (E F) and (E F) are well formed formulae how would you define well-formed arithmetic expressions? Dr-Zaguia-CSI2101-W08 31

Recursively Defined Sets and Structures The set of full binary trees can be defined recursively: Basic step: There is a full binary tree consisting only of a single vertex r. Recursive step: If T1 and T2 are disjoint full binary trees, there is a full binary tree denoted by T1.T2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree T1 and the right subtree T2. We define The height h(t) of a full binary tree T recursively Basic step: The height of the full binary tree consisting of only a root r is h(t)=0. Recursive step: If T1 and T2 are full binary tree, then the full binary tree T=T1.T2 has height h(t)= 1 + max(h(t1), h(t2)). Dr-Zaguia-CSI2101-W08 32

Recursively Defined Sets and Structures Give recursive definition of a rooted tree a binary tree internal and leaf vertices of a tree length of a string Dr-Zaguia-CSI2101-W08 33

Structural Induction (note 2009: topic delayed until after midterm) Prove that every well-formed formula of propositional logic has equal number of left and right parenthesis Base step: T, F and propositional variables do not contain parenthesis (so their number is equal) Induction step: in every way to construct well-formed formula, analyse number of parenthesis assuming ok for building blocks Structural induction of P(x) for every element x of a recursively defined set S: Base step: prove P(x) for each element x of the base step definition of S Induction step: for every way to construct an element x of S from elements y 1, y 2,.. y k, show that P(y 1 ) P(y 2 ) P(y k ) P(x) Dr-Zaguia-CSI2101-W08 34

Structural Induction Theorem: Let T be a full binary tree with n(t) vertices and height h(t), then n(t) 2 h(t)+1-1 [Note: P(k) becomes now P(T)!!!] Proof using structural induction: Basis step: for the full binary tree consisting of just the root r, n(t)=1 and h(t)=0, thus n(t)=1 2 0+1-1 =1. Inequality is true. Inductive step: We assume that n(t1) 2 h(t1)+1-1 and n(t2) 2 h(t2)+1-1 for two full binary trees T1 and T2. According to the recursive formulae: n(t)=n(t1)+n(t2)+1 and h(t)= 1 + max(h(t1), h(t2)), thus n(t)=n(t1)+n(t2)+1 (2 h(t1)+1-1) + (2 h(t2)+1-1) +1 (2 h(t1)+1 + 2 h(t2)+1 ) -1 2*max (2 h(t1)+1, 2 h(t2)+1 ) -1 2*2 max (h(t1)+1, h(t2)+1) -1 2*2 h(t) -1 = 2 h(t)+1-1 Recursive definition of height Dr-Zaguia-CSI2101-W08 35

Structural Induction Exercises Let l(x) denote the length of a string x. Prove that l(x.y) =l(x)+l(y). Every quantified formula has an equivalent one which is in prenex normal form. Dr-Zaguia-CSI2101-W08 36

Recursive Algorithms Dr-Zaguia-CSI2101-W08 37

Recursion and Iteration What about the Fibonacci sequence? int fibrec(int n) { if (n <=2 ) return 1; else return fib(n-1)+fib(n-2) } can we do it iteratively? int fibiter(int n) { int a = b = c = 1; for(i=2; i<n; i++) { c = a+b; a = b; b = c; } return c } Dr-Zaguia-CSI2101-W08 38

Recursion and Iteration Search for an element in a list We traverse sequentially the array starting from the first cell until we find x or we finish the array procedure search(a: series; i, j: integer; x: item to be found) if a i = x return i if i = j return 0 return search(i+1, j, x) No real advantage in using recursion here location := i while (location j) and (S[location] x) do location := location+1 if location > j then location := 0 Dr-Zaguia-CSI2101-W08 39

Recursion and Iteration When the list is already sorted, we can use a faster search: Binary search procedure binarysearch(a, x, i, j) {Find location of x in a, i and <=j} m := (i + j)/2 {Go to halfway point.} if x = a m return m if x<a m i<m return {If it s to the left,} binarysearch(a,x,i,m 1){Check that ½} else if a m <x m<j return {If it s to right,} binarysearch(a,x,m+1,j){check that ½} else return 0 {No more items, failure.} Dr-Zaguia-CSI2101-W08 40

Recursion and Iteration Complexity of sequential search: T SS (n) = T(n-1) + n Complexity of sequential search: T BS (n) = T(n/2) + 1 Since 1 = n/2 k then k = log n and thus Binary search needs log n + 1 comparisons. Sequential search needs at most n comparisons Number of recursive calls Number of comparisons par call T BS (n) = (k+1) * 1 = k+1 Dr-Zaguia-CSI2101-W08 41

Recursion and Iteration Comparing both search algorithms: Size Sequential Binary 128 128 8 1024 1024 11 1,048,576 1,048,576 21 4,294,967,296 4,294,967,296 33 Binary search is much faster however the list must be sorted Dr-Zaguia-CSI2101-W08 42