150 likes | 341 Views
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
E N D
CSCI 3160 Design and Analysis of AlgorithmsTutorial 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 • Conquer: Solve the sub-problems recursively • Combine: Use the results of the sub-problems to construct the answer of the overall problem
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.
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)
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)
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)
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) +
Merge sort • Sort n integers in ascending order • Idea • What is the “magic sort”? • Think recursively; merge sort with less integers ?
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
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)
Selection algorithm • Can we find the k-th number without sorting the entire sequence? sort k
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 . . .
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)
End • Questions