Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
150 views6 pages

Discrete Math: Recursion & Sequences

The document discusses recursive definitions, recurrence relations, and methods for solving recurrence relations. Recursive definitions define objects or processes in terms of simpler versions of the same. Recurrence relations relate terms in a numerical sequence. The method of telescoping can be used to solve recurrence relations by rewriting them for subsequent terms until the initial condition is reached. Examples show finding maximum elements, merge sort, and solving recurrence relations for algorithms.

Uploaded by

raghavajay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views6 pages

Discrete Math: Recursion & Sequences

The document discusses recursive definitions, recurrence relations, and methods for solving recurrence relations. Recursive definitions define objects or processes in terms of simpler versions of the same. Recurrence relations relate terms in a numerical sequence. The method of telescoping can be used to solve recurrence relations by rewriting them for subsequent terms until the initial condition is reached. Examples show finding maximum elements, merge sort, and solving recurrence relations for algorithms.

Uploaded by

raghavajay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CmSc 175 Discrete Mathematics Lesson 20: Recursive Definitions.

Solving Recurrence Relations in Numerical Sequences

1. Recursive Definitions Basic idea: To define objects, processes and properties in terms of simpler objects, simpler processes or properties of simpler objects/processes. To define recursively objects we need the following: 1. The objects can be ordered in some way. 2. Each object in the sequence of objects can be defined in terms of the previous object(s). 3. There is a starting object that is defined explicitly (not in terms of some other object.)

2. Examples of Recursive Definitions A. Sequences 1. The sequence of odd numbers 1,3, 5, 7, ., 2n-1,. a1 = 1 an+1 = an + 2, n = 1, 2, 3, 2. The sequence of even numbers 2, 4, 6, 8, ., 2n, . a1 = 2 an+1 = an + 2, n = 1, 2, 3, 3. Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, F1 = 1 F2 = 1 F n+2 = F n+1 + Fn

B. Recursive functions Example: f(n) = n! 1. f(0) = 1 2. f(n) = n * f(n-1) i.e. 0! = 1 i.e. n! = n * (n-1)!

Using this definition we obtain the following sequence: 0! = 1 1! = 1 * 0! = 1 2! = 2 * 1! = 2 3! = 3 * 2! = 6 4! = 4 * 3! = 24 etc. The starting object is 0!, defined explicitly to be 1. Each next object is defined in terms of the previous one: 0! = 1 1! = 1 * 0! = 1 2! = 2 * 1! = 2 3! = 3 * 2! = 6 4! = 4 * 3! = 24 C. Recursive processes Binary search in a sorted array: 1. If the array consists of one element only, do comparison and report the result. 2. If the array has more elements, split the array into two and then apply the search either to the left or to the right half of the array.

3. Recurrence Relations in Numerical Sequences Recurrence relation is a formula that relates two or more (but finite number) successive terms in a sequence. It is part of the recursive definition of a sequence. Any recurrence relation is accompanied by initial condition (sometimes called terminating condition) which specifies the first term of the sequence. Example: The sequence 1, 3, 7, 15, 31, 63, is defined by the following initial condition and recurrence relation: a1 = 1 an = 2an-1 +1, n = 2, 3, 4, Note that this is equivalent to the following definition: a1 = 1 an+1 = 2an +1, n = 1, 2, 3, 4,
2

4. Solving Recurrence Relations The purpose of solving a recurrence relation is to find a formula for the general term of the sequence given by that recurrence relation.

There are various methods for solving recurrence relations. Here we will use a method called telescoping. The method consists in the following: - Re-write the recurrence relation for subsequent smaller values of n so that the left side of the re-written relation is equal to the first term of the right side of the previous relation. Re-write the relation until a relation involving the initial condition is obtained. Add the left sides and the right sides of the relations. Cancel the equal terms on the left and the right side. Add the remaining terms on the right side. The sum will give the general formula of the sequence

Example: Consider the sequence 1, 3, 6, 10, 15, 21, 28, 36, Each term is obtained from the previous by adding the number that shows the position of the current term to the previous term, e.g. 10 is in position 4 and the previous term is 6: 10 = 6 + 4. We want to obtain a formula for the general term of this sequence. a1 = 1 an = an-1 +n, n = 2, 3, 4, Telescoping: an = an-1 +n an-1 = an-2 + (n 1) an-2 = an-3 + (n 2) . a4 = a3 + 4 a3 = a2 + 3 a2 = a1 + 2 Add the left and the right sides: an + an-1 + an-2 + an-3 + + a4 + a3 + a2 = an-1 + an-2 + an-3 + + a4 + a3 + a2 + a1 + 2 + 3 + 4 + + (n 1) + n Cancel equal terms on both sides: an = a1 + 2 + 3 + 4 + + (n 1) + n
3

Replace with the value given in the initial condition: an = 1 + 2 + 3 + 4 + + (n 1) + n = n(n+1)/2 Thus we have obtained the general formula for each term in the sequence: an = n(n+1)/2 5. Applications Solving recurrence relations is used in computer science to assess the running time (also called complexity ) of recursive algorithms. Example 1: Find recursively the max element in an array. Algorithm: If the array has 1 element, this is the maximum Else, a. Find the maximum in the array of the first n-1 elements. b. Compare with the n-th element and return the greater one int findmax(int [] array, int size) { if (size == 1) return array[0]; else { int max = findmax(array, size-1) if (max >= array[size-1]) return max; else return array[size-1]; } } Here we reason in the following way: The time to find the maximum in an array of size N is equal to the time to find the maximum in an array of size N-1 plus the time to do the comparison. Since the time to do the comparison does not depend on the size of the array, we consider it to be a constant and we use 1. When the array has only one element the time is a constant (again we use 1) Thus we come up with the following recurrence relation: T(1) = 1 // the time to process an array of 1 element is 1 T(N) = T(N-1) + 1

Telescoping: T(N) = T(N-1) + 1 T(N-1) = T(N-2) + 1 T(N-2) = T(N-3) + 1 . T(3) = T(2) +1 T(2) = T(1) + 1 T(1) = 1 Adding the left and the right side and canceling equal terms, we obtain: T(N) = 1 + 1 + + 1 There are N lines in the telescoping, thus the sum of the 1s is N: T(N) = N Example 2: Merge sort: The time to sort an array of N elements is equal twice the time to sort an array of N/2 elements, plus the time to merge the sorted halves of the array. T(1) = 1 T(N) = 2T(N/2) + N Here we do a helpful preprocessing divide both sides by N: T(N) / N = T(N/2) / (N/2) + 1 Telescoping: T(N) / N = T(N/2) / (N/2) + 1 T(N/2) / (N/2) = T(N/4) / (N/4) + 1 T(N/4) / (N/4) = T(N/8) / (N/8) + 1 . T(8)/8 = T(4)/4 + 1 T(4)/4 = T(2)/2 + 1 T(2)/2 = T(1) + 1 Adding the left and the right side and canceling equal terms, we obtain: T(N)/N = T(1) + 1 + . + 1 There are logN equations, thus 1 + + 1 = logN T(N)/N = T(1) + logN T(N)/N = LogN + 1 because T(1) = 1 T(N) = NlogN + N
5

Useful formulas: 1 + 2 + + N = N(N+1)/2 1 + 2 + 4 + 8 + + 2p = 2p+1 1 N = 2logN How much is: a) 1 + 2 + 3 + 4 + + LogN

b) 1 + 2 + 4 + 8 + + N

Exercises: Solve the recurrence relations with T(1) = 1 a) T(N) = T(N/2) + 1 b) T(N) = T(N/2) + N c) T(N) = 2T(N/2) + 1 d) T(N) = 2T(N 1) + 1 e) T(N) = 2T(N 1) + N

You might also like