1 / 15

CSCI 3160 Design and Analysis of Algorithms Tutorial 7

CSCI 3160 Design and Analysis of Algorithms Tutorial 7. Fei Chen. Outline. Divide and conquer Master theorem Merge sort Selection algorithm. Divide and conquer. A three-step problem-solving paradigm Divide : Break the problem into sub-problems

alvaro
Download Presentation

CSCI 3160 Design and Analysis of Algorithms Tutorial 7

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. CSCI 3160 Design and Analysis of AlgorithmsTutorial 7 Fei Chen

  2. Outline Divide and conquer Master theorem Merge sort Selection algorithm

  3. Divide and conquer • A three-step problem-solving paradigm • Divide: Break the problem into sub-problems • Conquer: Solve the sub-problems recursively • Combine: Use the results of the sub-problems to construct the answer of the overall problem

  4. Master theorem • Solve recurrence relations in the form T(n) = a∙T(n/b) + O(nd) where a > 0, b > 1, d ≥ 0 are all constants.

  5. Master theorem • Example 1: T(n) = 2 T(n/2) + n3 • a = 2, b = 2, d = 3 • logba = log22 = 1 < 3 = d • 1st case: T(n) = O(nd) = O(n3)

  6. Master theorem • Example 2: T(n) = 16 T(n/4) + n2 • a = 16, b = 4, d = 2 • logba = log416 = 2 = d • 2nd case: T(n) = O(nd log n) = O(n2 log n)

  7. Master theorem • Example 3: T(n) = 9 T(n/3) + n • a = 9, b = 3, d = 1 • logba = log39 = 2 > 1 = d • 3rd case: T(n) = O(nlogba) = O(n2)

  8. Exercise • Solve the following recurrence relations: • T(n) = 3 T(n/2) + n • T(n) = 3 T(n/2) + n2 • T(n) = 3 T(n/3) + n/2 • T(n) = 3 T(n/3) +

  9. Merge sort • Sort n integers in ascending order • Idea • What is the “magic sort”? • Think recursively; merge sort with less integers ?

  10. Merge sort • In words • If there is only one element, no action • Otherwise • Recur on the first half • Recur on the second half • Merge the two halves

  11. Analysis • Space complexity = O(n) • Time complexity • Let T(n) be the cost to merge-sort n integers • Base case is when n = 1 • For larger n, T(n) ≤ 2 T(n/2) + O(n) • Use Master theorem with a = 2, b = 2, d = 1 • 2nd case: T(n) = O(n log n)

  12. Selection algorithm • Can we find the k-th number without sorting the entire sequence? sort k

  13. Selection algorithm • Idea • Pick a random pivot v • Repeat until |SL|, |SR| ≤ 3n/4 • If k ≤ |SL|, recur on SL for the k-th number else if k ≤ |SL|+|Sv|, return v else recur on SR for the (k-|SL|-|Sv|)-th number SL Sv SR . . .

  14. Analysis • Space complexity = O(n) • Time complexity • Let T(n) be the cost of the algorithm on n integers • Base case is when n = 1 • For larger n, T(n) ≤ T(3n/4) + O(n) • In expectation, it takes 2 (=O(1)) trials to get a good v • In each trial, we run a loop to obtain the smaller sets • Use Master theorem with a = 1, b = 4/3, d = 1 • 1st case: T(n) = O(n)

  15. End • Questions

More Related