Useful Formulae
This document presents essential formulae and methods for analyzing algorithms, focusing on recurrence relations and their solutions. It includes iterative and characteristic equation methods to define sequences and solve linear recurrence relations. Furthermore, practical examples, such as MergeSort and polygon triangulation, are analyzed with their corresponding recurrence relations. The text emphasizes improving algorithm efficiency through strategic problem splitting and proposes solutions when applying the Master Theorem is not feasible. The content is vital for understanding algorithmic analysis and design.
Useful Formulae
E N D
Presentation Transcript
Analysis of Algorithms for (i = 0; i <= n; i++) { j = i; do j = j - 2 while (j > 0); } for (i = 0; i <= n; i++) { j = i; do j = j div 2 while (j > 0); } T(n) = O(n2) T(n) = O(n log n)
Recurrence Relations • A sequence of numbers can be defined by a recurrence relation and a set of initial conditions • Examples: • an = an-1 + 2 ; a0 = 5 gives the sequence a0 = 5, a1 = 7, a2 = 9, a3 = 11 • an = an-1 + an-2 ; a0 = 1 a1 = 1 gives the sequence a0 = 1, a1 = 1, a2 = 2, a3 = 3, a4 = 5, a5 = 8
How to solve a recurrence relation? • Ad Hoc Method -- Calculate the first few values of the recurrence -- Look for regularity -- Guess a suitable general form -- Prove by mathematical induction e.g., T(n) = 2T(n - 1) + 1, T(1) = 1 • Iterated Method -- Converts the recurrence into a summation -- Use bounded summation to solve the recurrence e.g., T(n) = 3T(n/4) + n, T(1) = 1 Characteristic Equation or Annihilator Method
Obtaining a General Solution • A general solution is of the form g(n)=anwhere a is some constant. To determine a, substitute g(n)=an into the recurrence. c0an - c1an-1 - ... - ckan-k = 0, divide by an-k to get c0ak - c1ak-1 - ... - ck = 0, • Solving this polynomial equation gives all possible a's. (This is called the characteristic equation.)
Multiple Roots • If a is the root of the characteristic equation with multiplicity m, there is a general solution
Getting a Particular Solution • Mainly try and error. However, some very good suggestions do exist: • Sometimes your initial guess "degenerates" and gives contradictory conditions. Then try a solution of higher degree.
A Total Example • Once the general and particular solutions have been found, they are added together to give the total solution. • The initial conditions are then used to determine the constants of the general solution.
Pseudo Nonlinear Recurrence • Range Transformations: on the values of the sequences • Domain Transformations: on the value of indices
A Practical Example • Mergesort • Split list in half • Sort both halves • Merge the two halves • Analysis: The recurrence relation is T(0) = 0
Example Continued T(n) = 2T(n/2) + (n – 1)
Example Continued (3) Impose the initial conditions on A2n + n 2n + 1 (n=0) A + 0 + 1 = U(0) = 0 A = -1 So U(n) = n 2n + 2n + 1 = T(2n) Replace n by log n gives T(n) = nlog n + n + 1 = Q(n log n)
How to deal with this? • What if the master theorem cannot be applied? • Extend the recurrence and compute the sum of the terms.
Polygon Triangulation • Given a polygon in the plane, add diagonals so that each face is a triangle None of the diagonals are allowed to cross. • Triangulation is an important first step in many geometric algorithms.
Polygon Triangulation • The simplest algorithm might be to try each pair of points and check if they see each other. If so, add the diagonal and recur on both halves, for a total of O(n3). • However, Chazelle gave an algorithm which runs in T(n) =2T(n/2)+O( ) time. Since = O(n1-e) by case 1 of the Master Theorem, Chazelle's algorithm is linear, i.e., T(n) =O(n).
Application to Algorithm Design • To improve the efficiency of an algorithm • if logba > c, try to decrease the ratio of a/b (i.e., split the problem into fewer subproblems with larger size. • if logba ≤ c, try to find a better way for splitting/merge subproblems.