1 / 18

Running times continued

Explore the running times of merge sort and other divide-and-conquer algorithms, including recurrence analysis and the Master Theorem. Also, learn about counting inversions and finding the closest pair of points.

Download Presentation

Running times continued

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. Running times continued • some running times are more difficult to analyze • e.g., recursive algorithms • Example: MergeSort

  2. Running times continued Merging two sorted lists Input: Output: Two arrays A = {a1, a2, …, am}, B = {b1, b2, …, bn}, in increasing order Array C containing A [ B, in increasing order • MERGE(A,B) • i=1; j=1; k=1; • am+1=1; bn+1=1; • while (k · m+n) do • if (ai < bj) then • ck=ai; i++; • else • ck=bj; j++; • k++; • RETURN C={c1, c2, …, cm+n} Running time ?

  3. Running times continued Sorting Input: Output: An array X = {x1, x2, …, xn} X sorted in increasing order MergeSort – a divide-and-conquer algorithm • MERGESORT(X,n) • if (n == 1) RETURN X • middle = n/2 (round down) • A = {x1, x2, …, xmiddle} • B = {xmiddle+1, xmiddle+2, …, xn} • As = MERGESORT(A,middle) • Bs = MERGESORT(B,n-middle) • RETURN MERGE(As,Bs)

  4. A recurrence Running time of MergeSort: T(n) How to bound T(n) ? -> “unrolling the recurrence”

  5. A recurrence Running time of MergeSort: T(n) How to bound T(n) ? -> “substitution / induction”

  6. Master Theorem • Let a ¸ 1 and b>1 be constants, f(n) be a function and for positive integers we have a recurrence for T of the form • T(n) = a T(n/b) + f(n), • where n/b is rounded either way. • Then, • If f(n) = O(nlog a/log b - e) for some constant e> 0, then • T(n) = (nlog a/log b). • If f(n) = (nlog a/log b), then T(n) = (nlog a/log blog n). • If f(n) = (nlog a/log b + e) for some constant e > 0, and if af(n/b) · cf(n) for some constant c < 1 (and all sufficiently large n), then • T(n) = (f(n)).

  7. Master Theorem • Let a ¸ 1 and b>1 be constants, f(n) be a function and for positive integers we have a recurrence for T of the form • T(n) = a T(n/b) + f(n), • where n/b is rounded either way. • Then, • If f(n) = O(nlog a/log b - e) for some constant e> 0, then • T(n) = (nlog a/log b). • If f(n) = (nlog a/log b), then T(n) = (nlog a/log blog n). • If f(n) = (nlog a/log b + e) for some constant e > 0, and if af(n/b) · cf(n) for some constant c < 1 (and all sufficiently large n), then • T(n) = (f(n)).

  8. Master Theorem • Let a ¸ 1 and b>1 be constants, f(n) be a function and for positive integers we have a recurrence for T of the form • T(n) = a T(n/b) + f(n), • where n/b is rounded either way. • Then, • If f(n) = O(nlog a/log b - e) for some constant e> 0, then • T(n) = (nlog a/log b). • If f(n) = (nlog a/log b), then T(n) = (nlog a/log blog n). • If f(n) = (nlog a/log b + e) for some constant e > 0, and if af(n/b) · cf(n) for some constant c < 1 (and all sufficiently large n), then • T(n) = (f(n)).

  9. More on sorting Other O(n log n) sorts ? Can do better than O(n log n) ?

  10. A lower-bound on sorting: (n log n) Every comparison-based sort needs at least (n log n) comparisons, thus its running time is (n log n).

  11. Divide-and-conquer algorithms Counting inversions Input: A permutation a1,a2,…,an of 1,2,…,n Output: # inversions, i.e. (i,j) pairs where i<j but ai>aj

  12. Divide-and-conquer algorithms Counting inversions Input: A permutation a1,a2,…,an of 1,2,…,n Output: # inversions, i.e. (i,j) pairs where i<j but ai>aj

  13. Divide-and-conquer algorithms Counting inversions Input: A permutation a1,a2,…,an of 1,2,…,n Output: # inversions, i.e. (i,j) pairs where i<j but ai>aj

  14. Divide-and-conquer algorithms Closest Pair of Points Input: n points in the plane (x1,y1),…,(xn,yn) Output: distance of the closest pair of points, i.e., mini,j (i j) distance of (xi,yi) and (xj,yj)

  15. Divide-and-conquer algorithms Closest Pair of Points Input: n points in the plane (x1,y1),…,(xn,yn) Output: distance of the closest pair of points, i.e., mini,j (i j) distance of (xi,yi) and (xj,yj)

  16. Divide-and-conquer algorithms Closest Pair of Points Input: n points in the plane (x1,y1),…,(xn,yn) Output: distance of the closest pair of points, i.e., mini,j (i j) distance of (xi,yi) and (xj,yj)

  17. Divide-and-conquer algorithms Closest Pair of Points Input: n points in the plane (x1,y1),…,(xn,yn) Output: distance of the closest pair of points, i.e., mini,j (i j) distance of (xi,yi) and (xj,yj)

  18. Divide-and-conquer algorithms Closest Pair of Points Input: n points in the plane (x1,y1),…,(xn,yn) Output: distance of the closest pair of points, i.e., mini,j (i j) distance of (xi,yi) and (xj,yj)

More Related