Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Divide and Conquer

Similar presentations


Presentation on theme: "Topic: Divide and Conquer"— Presentation transcript:

1 Topic: Divide and Conquer
4. Merge Sort The problem:  Given a list of n numbers, sort them in non-decreasing order idea: Split each part into 2 equal parts and sort each part using Mergesort, then merge the two sorted sub-lists into one sorted list. The algorithm: procedure MergeSort (low, high) begin if then call MergeSort (low, mid); call MergeSort (mid+1, high); call Merge (low, mid, high); end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

2 Topic: Divide and Conquer
procedure Merge (low, mid, high) begin while (i  mid and j  high) if A(i) < A(j), then U(k) A(i); i++; else U(k) A(j); j++; k++; if (i > mid) move A(j) through A(high) to U(k) through U(high); else move A(i) through A(mid) to U(k) through U(high); for p  low to high A(p)  U(p); end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

3 Topic: Divide and Conquer
Analysis: Worst-Case for MergeSort is: Best-Case & Average-Case? Merge sort pays no attention to the original order of the list, it will keep divide the list into half until sub-lists of length 1, then start merging. Therefore Best-Case & Average-Case are the same as the Worst-Case O(nLogn) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

4 Topic: Divide and Conquer
5. Quick Sort The problem: Same as Merge Sort Compared with Merge Sort: Quick Sort also use Divide-and-Conquer strategy Quick Sort eliminates merging operations Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

5 Topic: Divide and Conquer
How it works? Use partition Eg: Sort array 65, 70, 75, 80, 85, 60, 55, 50, 45 45 85 Pivot item 65 Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

6 Topic: Divide and Conquer
The algorithm: procedure QuickSort (p, q) begin if p < q, then call Partition (p, q, pivotposition); call QuickSort (p, pivotposition –1); call QuickSort (pivotposition+1, q); end; procedure Partition (low, high, pivotposition) v  A(low); j  low; for i  (low + 1) to high if A(i) < v, j++; A(i)  A(j); pivotposition  j; A(low)  A(pivotposition); Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

7 Topic: Divide and Conquer
Analysis: Worst-Case: Call Partition O(n) times, each time takes O(n) steps. So O(n2), and it is worse than Merge Sort in the Worst-Case. Best-Case: Split the list evenly each time. Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

8 Topic: Divide and Conquer
Average-Case: Assume pivot is selected randomly, so the probability of pivot being the kth element is equal, k. C(n) = # of key comparison for n items average it over all k, (CA(n) is average performance) multiply both side by n Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

9 Topic: Divide and Conquer
replace n by n-1 subtract (2) from (1) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

10 Topic: Divide and Conquer
divide n(n + 1) for both sides  Best-Case = Average-Case Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

11 Topic: Divide and Conquer
6. Selection The problem: Given a list of n elements find the kth smallest one Note: special cases: when k = 1, min when k = n, max The solving strategy: If use sorting strategy like MergeSort, Both Best-Case and Worst-Case take O(n Log n) If use Quick Sort, Best-Case is O(n) (when pivot is the kth smallest one) Worst-Case will call partition O(n) times, each time takes O(n), so Worst-Case is O(n2). Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

12 Topic: Divide and Conquer
Select1 – the first algorithm idea: use “partition” (from Quick Sort) repeatedly until we find the kth element. For each iteration: If pivot position = k  Done! If pivot position < k  to select (k-i)th element in the 2nd sub-list. If pivot position > k  to select kth element in the 1st sub-list. i = pivot position pivot 1st sub-list 2nd sub-list Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

13 Topic: Divide and Conquer
procedure Select1 (A, n, k) // A is array, n is # of array, k is key begin m  1; j  n; loop call Partition (m, j, pivotposition); case: k = pivotposition: return A(k); k < pivotposition: j  pivotposition – 1; else: m  pivotposition + 1; end loop; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

14 Topic: Divide and Conquer
By choosing pivot more carefully, we can obtain a selection algorithm with Worst-Case complexity O(n) How: Make sure pivot v is chosen s.t. at least some fraction of the elements will be smaller than v and at least some other fraction of elements will be greater than v. Example: 35 (n) elements divide into 7 (n/r) subsets of size 5 (r) Elements  mm Non- decreasing Order (all columns) mm (median of medians) The middle row is in non-decreasing order Elements  mm Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

15 Topic: Divide and Conquer
By choosing pivot more carefully, we can obtain a selection algorithm with Worst-Case complexity O(n) Use mm (median of medians) rule to choose pivot procedure Select2 (A, n, k) begin if n  r, then sort A and return the kth element; divide A into subset of size r each, ignore excess elements, and let be the set of medians of the subsets; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

16 Topic: Divide and Conquer
Select ; use “Partition” to partition A using v as the pivot; // Assume v is at pivotposition case: k = pivotposition: return (v); k < pivotposition: let S be the set of elements A (1,…, pivotposition –1), return Select2 (S, pivotposition –1, k); else: let R be the set of element A (pivotposition+1,…, n), return Select2 (R, n- pivotposition, k-pivotposition);   end case; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

17 Topic: Divide and Conquer
Analysis: How many Mi’s  (or ) mm? At least How many elements  (or ) mm? How many elements > mm (or < mm)? At most Assume r = 5 Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

18 Topic: Divide and Conquer
Therefore, procedure Select2 the Worst-Case complexity is: where c is chosen sufficiently large so that T(n)  cn for n < 24. Proof: Use induction to show T(n)  20  cn (n  24) IB (induction base): Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

19 Topic: Divide and Conquer
IH (induction hypothesis): Suppose IS (induction solution): When n = m;  T(n) = O(n) complexity of Select2 of n elements Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer


Download ppt "Topic: Divide and Conquer"

Similar presentations


Ads by Google