# 1 Analysis of Algorithms Chapter - 02 Recurrences

• View
229

2

Tags:

Embed Size (px)

### Text of 1 Analysis of Algorithms Chapter - 02 Recurrences

• *Analysis of Algorithms

Chapter - 02Recurrences

Khloud Shawagfeh

• * Definition: A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. The worst-case running time T(n) of an algorithm could be described as by the recurrenceT(n) = aT(n/b) + f(n) where a1, b>1, and f(n) is a given function. Three methods will be discussed to solve the recurrences obtaining asymptotic bounds on the solution. Substitution method: We guess a bound and then use mathematical induction to prove that our guess is correct. Recursion-tree method: It converts the recurrence into a tree whose nodes represent the cost incurred at various levels of the recursion We use techniques for bounding summations to solve the recurrence. Master method: It provides bounds for recurrences of the above form. It requires memorization of three cases. Once you memorize the cases, determining asymptotic bounds for many recurrences is easy.Methods

Khloud Shawagfeh

• *Definition of Proof: The process or an instance of establishing the validity of a statement especially by derivation from other statements in accordance with principles of reasoning.

Proof by Induction: Let Pn be a statement for all the positive integers (n = 1, 2, 3, . . .). If the following two properties hold:P1 is true.Pk+1 is true if Pk is true for each positive integer k. Then Pn is true for all n.

Why Induction Works?Let S be the set of all numbers n for which Pn is false.Let k be the minimum number in S.k > 1 since by the first property of the induction definition, P1 is true.By the minimality of k, Pk1 is true and Pk is false.A contradiction to the second property of the induction definition.Induction

Khloud Shawagfeh

• * Prove that for any integer n 1:1 + 2 + 3 + + n = n(n+1)/2 . Define: L(n) = 1 + 2 + 3 + + n. R(n) = n(n+1)/2 .

Prove that L(n) = R(n) for n 1.

Verifying the Claim

First check for small values of n:A Summation Problem

nL(n)R(n)11=11.2/2=121+2=32.3/2=331+2+3=63.4/2=6:::101+2+3++10=5510.11/2=55

Khloud Shawagfeh

• * Idea: Compute the value of 2L(n).

Example: 2(1 + 2 + 3) = (1 + 2 + 3) + (3 + 2 + 1)= (1 + 3) + (2 + 2) + (3 + 1)= 3 4= 12= 2(34/2)

Hence2L(n) = (1 + + n) + (n + + 1) = (1 + n) + (2 + (n 1)) + + (n + 1) = (n + 1) + (n + 1) + + (n + 1) = n(n + 1)

This implies that: L(n) = n(n+1)/2 = R(n).

A Direct Proof

Khloud Shawagfeh

• * For n=1, L(1) = 1 and R(1) = (1.2)/2 = 1 So, for n=1, it is true.

Let us assume that it is true for n=k, that is, L(k) = R(k) =>1+2+3+..+k = k(k+1)/2

Now, if we can prove that it is true for n=k+1 also, then it can be said that it is true for all n. For n=k+1, L(k + 1) = 1 + 2 + + k + (k + 1)= L(k) + (k + 1)= R(k) + (k + 1)= k(k + 1)/2 + (k + 1)= (k + 1)(k/2 + 1)= (k + 1)(k + 2)/2= R(k + 1)

Hence L(n) = R(n), for all integer n.A Proof by Induction

Khloud Shawagfeh

• *Substitution Method

Khloud Shawagfeh

• * The method for solving recurrences entails two steps: Guess the form of the solution Use mathematical induction to find the constants and show that the solution works. This method is powerful, but it obviously can be applied only in cases when it is easy to guess the form of the solution. This method can be used to establish either upper or lower bounds on a recurrence.

Problem: Let us determine an upper bound on the following recurrence:T(1) = 0.T(n) = 2T(n/2) + n, for n>1. Solution: Compute the solution for small powers of 2:T(2) = 2T(1) + 2 = 2.T(4) = 2T(2) + 4 = 8.T(8) = 2T(4) + 8 = 24.T(16) = 2T(8) + 16 = 64.T(32) = 2T(16) + 32 = 160.Recurrence -1

Khloud Shawagfeh

• *Guessing the Solution - Guess T(n) = n log2 n for n a power of 2.

A Proof by InductionFor n=1, T(1)=0, that is T(n)=nlog2 nSo, for n=1, it is true.

Let us assume that it is true for n=k/2, that is, T(k/2)=(k/2) log2 (k/2)

Now, if we can prove that it is true for n=k also, then it can be said that it is true for all n. For n=k,T(k) = 2T(k/2) + k = 2(k/2) log2(k/2) + k = k(log2 k 1) + k = k log2 k

Hence T(n) = n log2 n, for all integer n.Recurrence -1 (Contd.)

Khloud Shawagfeh

• *Recurrence -2 Problem: Let us determine an upper bound on the following recurrence:T(1) = a.T(n) = 2T(n/2) + bn.For some constants a, b (independent of n).

Solution: Compute the solution for small powers of 2: T(2) = 2T(1) + 2b = 2b + 2a. T(4) = 2T(2) + 4b = 8b + 4a. T(8) = 2T(4) + 8b = 24b + 8a. T(16) = 2T(8) + 16b = 64b + 16a. T(32) = 2T(16) + 32b = 160b + 32a.

Guessing the Solution Guess T(n) = bn log2 n + an, for n a power of 2

Khloud Shawagfeh

• * Verify the guess for small numbers: b 1 log2 1 + a 1 = a. b 2 log2 2 + a 2 = 2b + 2a. b 4 log2 4 + a 4 = 8b + 4a. b 8 log2 8 + a 8 = 24b + 8a. b 16 log2 16 + a 16 = 64b + 16a. b 32 log2 32 + a 32 = 160b + 32a.

A Proof by Induction For n=1, T(1)=a, that is T(n)=b.nlog2 n + a.n So, for n=1, it is true. Let us assume that it is true for n=k/2, that is, T(k/2)=b.(k/2) log2 (k/2) + a.(k/2) Now, if we can prove that it is true for n=k also, then it can be said that it is true for all n.

For n=k, T(k) = 2T(k/2) + bk = 2(b(k/2) log2(k/2) + a(k/2)) + bk = bk(log2 k 1) + ak + bk = bk log2 k + ak

Hence T(n) = b.n log2 n + a.n, for all integer n

Khloud Shawagfeh

• *Recurrence -3 Problem: Let us determine an upper bound on the following recurrence:T(1) = a.T(n) = T(n/2) + b.For some constants a, b (independent of n).

Solution: Compute the solution for small powers of 2: T(2) = T(1) + b = b + a. T(4) = T(2) + b = 2b + a. T(8) = T(4) + b = 3b + a. T(16) = T(8) + b = 4b + a. T(32) = T(16) + b = 5b + a.

Guessing the Solution Guess T(n) = b log2 n + a, for n a power of 2

Khloud Shawagfeh

• * Verify the guess for small numbers: b log2 1 + a = a. b log2 2 + a = b + a. b log2 4 + a = 2b + a. b log2 8 + a = 3b + a. b log2 16 + a = 4b + a. b log2 32 + a = 5b + a.

A Proof by Induction For n=1, T(1)=a, that is T(n)=b log2 n + aSo, for n=1, it is true.Let us assume that it is true for n=k/2, that is, T(k/2)=b log2 (k/2) + aNow, if we can prove that it is true for n=k also, then it can be said that it is true for all n. For n=k,T(k) = T(k/2) + b = (b log2(k/2) + a) + b = b(log2 k 1) + a + b = b log2 k + a

Hence T(n) = b log2 n + a, for all integer n

Khloud Shawagfeh

• T(n) = 2T(n/2) + n = O(n lg n)Thus, we need to show that T(n) c n lg n with an appropriate choice of cInductive hypothesis: assume T(n/2) c (n/2) lg (n/2)Substitute back into recurrence to show that T(n) c n lg n follows, when c 1T(n) = 2 T(n/2) + n 2 (c (n/2) lg (n/2)) + n = cn lg(n/2) + n= cn lg n cn lg 2 + n= cn lg n cn + n cn lg n for c 1= O(n lg n)for c 1Recurrence -4

Khloud Shawagfeh

• Iteration MethodIteration method:

Expand the recurrence k times

Work some algebra to express as a summation

Evaluate the summation

Khloud Shawagfeh

• Iteration Method ExampleT(n) = n + 2T(n/2)T(n) = n + 2T(n/2) = n + 2(n/2 + 2T(n/4)) = n + n + 4T(n/4) = n + n + 4(n/4 + 2T(n/8)) = n + n + n + 8T(n/8) = in + 2iT(n/2i) = kn + 2kT(1) = nlgn + nT(1) = (nlgn)Assume: n = 2kn = 2kTaking lg on both sideslg n = lg (2k )lg n = klg2lg n = k x 1lg n = k

Khloud Shawagfeh

• T(n)= c + T(n-1)=c + c + T(n-2)=2c + T(n-2)=2c + c + T(n-3)=3c + T(n-3)kc + T(n-k) = ck + T(n-k)So far for n k we have T(n) = ck + T(n-k)

To stop the recursion, we should have n - k = 0 k = nT(n) = cn + T(0) = cn

Thus in general T(n) = O(n)

Khloud Shawagfeh

• T(n) = n + T(n-1) = n + n-1 + T(n-2)= n + n-1 + n-2 + T(n-3)= n + n-1 + n-2 + n-3 + T(n-4)= = n + n-1 + n-2 + n-3 + + (n-k+1) + T(n-k)=for n k

To stop the recursion, we should have n - k = 0 k = n

Khloud Shawagfeh

• The Master MethodBased on the Master theorem.T(n) = aT(n/b) + f(n)a 1, b > 1 are constants.f(n) is asymptotically positive.

Requires memorization of three cases.

Khloud Shawagfeh

• The Master TheoremTheorem:Let a 1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases:If f(n) = O(nlogba) for some constant > 0, then T(n) = (nlogba).If f(n) = (nlogba), then T(n) = (nlogbalg n).If f(n) = (nlogba+) for some constant > 0, and if, for some constant c < 1 and all sufficiently large n, we have af(n/b) c f(n), then T(n) = (f(n)).

Khloud Shawagfeh

• The Master TheoremGiven: a divide and conquer algorithm

An algorithm that divides the problem of size n into a subproblems, each of size n/b

Let the cost of each stage (i.e., the work to divide the problem + combine solved subproblems) be described by the function f(n)

Khloud Shawagfeh

• The Master Theoremif T(n) = aT(n/b) + f(n) where a 1 & b > 1then

Khloud Shawagfeh

• Understanding Master TheoremIn each of the three cases, we are comparing f(n) with nlogba , the solution to the recurrence is determined by the larger of the two functions.

In case 1, if the function nlogba is the larger, then the solution T(n) = (nlogba).

In case 3, if the function f(n) is the larger, then the solution is T(n) = (f(n)).

In case 2, if the two functions are the same size, then the solution is T(n) = (nlogba lg n) = (f(n) lg n).

Khloud Shawagfeh

• Understanding Master TheoremIn case 1, not only must f(n) be smaller than nlogba, it must be polynomially smaller. That is f(n) must be asymptotically smaller than nlogba by a factor of n for some constant > 0.

In case 3, not only must f(n) be larger than nlogba , it must be polynomially larger and in addition satisfy the regularity condition that:a f(n/b) c f(n).

Khloud Shawagfeh

• Understanding Master TheoremIt is important to realize that the three cases do not cover all the possibilities for f(n).

There is a gap between cases 1 and 2 when f(n) is smaller than nlogba but not polynomially smaller.

There is a gap between cases 2 and 3 when f(n) is larger than nlogba but not polynomially larger.

If f(n) falls into one of these gaps, or if the regularity condition in case 3 fails to hold, the master method cannot be used to solve the recurrence.

Khloud Shawagfeh

• Using The Master MethodCase 1T(n) = 9T(n/3) + na=9, b=3, f(n) = nnlogb a = nlog3 9 = (n2) >> from log332Since f(n) = O(nlog3 9 - ) = O(n2-0.5) = O(n1.5)where =0.5case 1 applies:

Thus the solution is T(n) = (n2)

Khloud Shawagfeh

• Using The Master MethodCase 2T(n) = T(2n/3) + 1a=1, b=3/2, f(n) = 1nlogba = nlog3/21 = n0 = 1Since f(n) = (nlogba) = (1)case 2 applies:

Thus the solution is T(n) = (lg n)

Khloud Shawagfeh

• Using The Master MethodCase 3T(n) = 3T(n/4) + n lg na=3, b=4, f(n) = n lg nnlogba = nlog43 = n0.793 = n0.8Since f(n) = W(nlog43+) = W(n0.8+0.2)= W(n)where 0.2, and for sufficiently large n,a . f(n/b) = 3(n/4) lg(n/4) < (3/4) n lg n for c = 3/4 case 3 applies:

Thus the solution is T(n) = (n lg n)

Khloud Shawagfeh

• *Recursion-tree Method

Khloud Shawagfeh

• *Although the substitution method can provide a concise proof that a solution to a recurrence is correct, it is sometimes difficult to come up with a good guess.Drawing out a recursion-tree is a straight forward way to devise a good guess.In a recursion-tree, each node represents the cost of a single sub-problem somewhere in the set of recursive function invocations. We sum the costs within each level of the tree to obtain a set of per-level costs. Then we sum all the per-level cost to determine the total cost of all levels of the recursion.Recursion trees are particularly useful when the recurrence describes the running time of a divide-and-conquer algorithm.A recursion tree is best used to generate a good guess, which is then verified by the substitution method.In this section, we will use recursion-trees to generate good guess.The method

Khloud Shawagfeh

• *Let us see a recursion-tree would provide a good guess for the recurrence:

We start by focusing on finding an upper bound for the solution. Following figure shows the derivation of the recursion-tree, assuming n as exact power of 4. We create a recursion tree for T(n) = 3T(n/4) + cn2, c>0.Example-1

Khloud Shawagfeh

• *Part (a) of the figure shows T(n), which is expanded in Part (b) into an equivalent tree representing the recurrences. The cn2 term at the root represents the cost at the top level of recursion, and the three subtrees of the root represent the cost incurred by the subproblems of size n/4.Part (c) shows this process carried one step further by expanding each node with cost T(n/4) from part (b). The cost for each of the three children of the root is c(n/4)2.We continue expanding each node in the tree by breaking it into its constituent parts as determined by the recurrence.Since the subproblem sizes decrease as we get further from the root, we eventually must reach a boundary condition.How far from the root do we reach one?The subproblem size for a node at depth i is n/4i. Thus the subproblem size hits n=1, when n/4i=1, that is, i=log4 n.Thus the tree has levels (0, 1, 2, , log4 n). Explanation

Khloud Shawagfeh

• *Now, we determine the cost at each level of the tree.Each level has three times more nodes than the level above, So the number of nodes at depth i is 3i.Each node at depth i has a cost of c(n/4i), for i=0, 1, 2, .., log4n -1.Multiplying, we get that total cost over all nodes at depth i is 3ic(n/4i)2 = (3/16)i cn2.The last level at, depth log4n, has 3log4n = nlog43 nodes, each contributing cost T(1), for a total cost of nlog43 .T(1) , which is (nlog43).Now add up the costs over all levels to determine the cost for the entire tree.Explanation (Contd.)

Khloud Shawagfeh

• *Now, we can use the substitution method to verify that our guess was correct, that is T(n)=O(n2) is an upper bound for the recurrence

We want to show that T(n) dn2, for some d>0.Using the same constant c>0 as before, we haveExplanation (Contd.)

Khloud Shawagfeh

• *Let us see a recursion-tree would provide a good guess for the recurrence:T(n) = T(n/3) + T(2n/3) + O(n).

Let c>0 be the constant factor for the term O(n). Example-2

Khloud Shawagfeh

• *Now, when we add the values across the levels of the recursion-tree, we get a value of cn for every level.The longest path from root to a leaf isn(2/3)n (2/3)2n .. 1.Since (2/3)kn =1, when k = log3/2n,The height of the tree is log3/2n.Intuitively, we expect the solution to the recurrence be O(cn.log3/2n) = O(n.lg n)We can use the substitution method to verify that our guess was correct, that is T(n)=O(n.lg n) is an upper bound for the given recurrence .We want to show that T(n) dn.lg n, for some d>0.Using the same constant c>0 as before, we haveExplanation (Contd.)

Khloud Shawagfeh

********************* ##### Asymptotic Notation, Recurrences: Substitution, sourav/Lecture-02.pdf Asymptotic Notation, Recurrences: Substitution, Iteration, Master Method Lecture 2 L2.2 Solving recurrences •The
Documents ##### Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes
Documents ##### Parallel Algorithms Examples Examples Concepts & Definitions Concepts & Definitions Analysis of Algorithms Analysis of Algorithms
Documents ##### 1 Recurrences Algorithms Jay Urbain, PhD [email protected] Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of
Documents ##### Recurrences David Kauchak cs161 Summer 2009. Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Documents ##### Randomised Algorithms · PDF file CMPT231 Lecture 2: ch4-5 Divide and Conquer, Recurrences, Randomised Algorithms cmpt231.seanho.com/lec2 59a262d 1/43
Documents ##### Asymptotic Notation, Recurrences: Substitution, sourav/Lecture-02.pdfAsymptotic Notation, Recurrences: Substitution, Iteration, Master Method Lecture 2 L2.2 Solving recurrences •The
Documents ##### Design and Analysis of Algorithms - huang/teaching/CSE5311/CSE5311_Lecture4.pdf · PDF file Master Theorem The master method applies to recurrences of the form T(n) = a T (n/b) +
Documents ##### Introduction to · PDF file Day 3 Introduction to Algorithms L2.2 Solving recurrences • The analysis of merge sort from Lecture 1 required us to solve a recurrence. • Recurrences
Documents ##### CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis
Documents ##### Introduction to Algorithms · PDF file 2019-09-12 · Introduction to Algorithms 6.046J/18.401J LECTURE 2 Asymptotic Notation • O-, Ω-, and Θ-notation Recurrences • Substitution
Documents ##### The analysis of divide and conquer algorithms require us to solve gprovan/CS4407/Lyz-  · PDF file The master method The master method applies to recurrences of the form T(n) = aT(n/b)
Documents ##### Recurrences and Tridiagonal solvers: From Classical Tricks ... · PDF file Recurrences and Tridiagonal solvers: From Classical Tricks to Parallel Treats ... Recurrences and Tridiagonal
Documents ##### Recurrences 2008. 1. 11 : 1 Chapter 3. Growth of function Chapter 4. Recurrences
Documents ##### Algorithms and Data Structures - ece250/materials/notes/Lecture5-Solving... · PDF file Algorithms and Data Structures Solving Recurrences (Master Theorem – Case1) Materials from
Documents ##### CSE 2331 / 5331 Foundations II: Data Structures and Algorithms Topic 2: -- Analyzing algorithms -- Solving recurrences
Documents ##### Lecture 3. Recurrences / Heapsort - · PDF file 2020. 3. 8. · Recurrences / Heapsort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press,
Documents ##### Algorithms Merge Sort Solving Recurrences The Master Theorem
Documents ##### Randomized Algorithms and Probabilistic Analysis of Algorithms
Documents ##### Link Analysis Ranking: Algorithms, Theory, and · PDF fileLink Analysis Ranking: Algorithms, Theory, and Experiments ... Link Analysis Ranking: Algorithms, Theory and Experiments
Documents Documents