1 / 29

Useful Formulae

Useful Formulae. 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(n 2 ). T(n) = O(n log n). Recurrence Relations.

Download Presentation

Useful Formulae

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Useful Formulae

  2. 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)

  3. 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

  4. 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

  5. Linear Recurrence Relations with Constant Coefficients

  6. Superposition of Solutions

  7. 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.)

  8. Example

  9. Multiple Roots • If a is the root of the characteristic equation with multiplicity m, there is a general solution

  10. 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.

  11. Examples

  12. 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.

  13. A Total Example

  14. Pseudo Nonlinear Recurrence • Range Transformations: on the values of the sequences • Domain Transformations: on the value of indices

  15. Example

  16. A Practical Example •  Mergesort • Split list in half • Sort both halves • Merge the two halves • Analysis: The recurrence relation is T(0) = 0

  17. Example Continued T(n) = 2T(n/2) + (n – 1)

  18. Example Continued

  19. 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) = nlog n + n + 1 = Q(n  log n)

  20. How to deal with this? • What if the master theorem cannot be applied? • Extend the recurrence and compute the sum of the terms.

  21. 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.

  22. 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).

  23. 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.

More Related