Download presentation
Presentation is loading. Please wait.
Published byMilo Hawkins Modified over 9 years ago
1
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH
2
Merge Sort Sort the first half of the array using merge sort. Sort the second half of the array using merge sort. Merge the first half of the array with the second half.
3
Merge Algorithm Merge is an operation that combines two sorted arrays. Assume the result is to be placed in a separate array called result (already allocated). The two given arrays are called front and back. front and back are in increasing order. For the complexity analysis, the size of the input, n, is the sum n front + n back.
4
Merge Algorithm For each array keep track of the current position. REPEAT until all the elements of one of the given arrays have been copied into result: – Compare the current elements of front and back. – Copy the smaller into the current position of result (break the ties however you like). – Increment the current position of result and the array that was copied from. Copy all the remaining elements of the other given array into result.
5
Merge Algorithm - Complexity Every element in front and back is copied exactly once. Each copy is two accesses, so the total number of accessing due to copying is 2n. The number of comparisons could be as small as min(n front, n back ) or as large as n-1. Each comparison is two accesses.
6
Merge Algorithm - Complexity In the worst case the total number of accesses is 2n +2(n-1) = O(n). In the best case the total number of accesses is 2n + 2min(n front,n back ) = O(n). The average case is between the worst and best case and is therefore also O(n).
7
Merge Sort Algorithm Split anArray into two non-empty parts anyway you like. For example, front = the first n/2 elements in anArray back = the remaining elements in anArray Sort front and back by recursively calling MergeSort. Now you have two sorted arrays containing all the elements from the original array. Use merge to combine them, put the result in anArray.
8
MergeSort Call Graph (n=7) Each box represents one invocation of MergeSort. How many levels are there in general if the array is divided in half each time? 0~6 0~23~6 3~4 3~34~4 5~6 5~56~6 1~2 1~12~2 0~0
9
MergeSort Call Graph (general) Suppose n = 2 k. How many levels? How many boxes on level j? What values is in each box at level j? n n/2 n/4 11111111
10
MergeSort: Complexity Analysis Each invocation of mergesort on p array positions does the following: – Copies all p positions once (#accesses = O(p)) – Calls merge (#accesses = O(p)) Observe that p is the same for all invocations at the same level, therefore total number of accesses at a given level j is O((#invocations at level j) * p j ).
11
MergeSort: Complexity Analysis The total number of accesses at level j is O((#invocations at level j) * p j ) = O(2 j-1 * n/2 j-1 ) = O(n) In other words, the total number of accesses at each level is O(n). The total number of accesses for the entire mergesort is the sum of the accesses for all the levels. (#levels) * O(n) = O(log n) * O(n) = O(n log n)
12
MergeSort: Complexity Analysis Best case:O(n log n) Worst case:O(n log n) Average case:O(n log n)
13
Quick Sort Quicksort can be seen as a variation of mergesort in which front and back are defined in a different way.
14
Quicksort Algorithm Partition anArray into two non-empty parts. – Pick any value in the array, pivot. – small = the elements in anArray < pivot – large = the elements in anArray > pivot – Place pivot in either part, so as to make sure neither part is empty. Sort small and large by recursively calling QuickSort. You could use merge to combine them, but because the elements in small are smaller than elements in large, simply concatenate small and large, and put the result into anArray.
15
Quicksort: Complexity Analysis Like mergesort, a single invocation of quicksort on an array of size p has complexity O(p): – p comparisons = 2*p accesses – 2*p moves (copying) = 4*p accesses Best case: every pivot chosen by quicksort partitions the array into equal-sized parts. In this case quicksort is the same big-O complexity as mergesort – O(n log n)
16
Quicksort: Complexity Analysis Worst case: the pivot chosen is the largest or smallest value in the array. Partition creates one part of size 1 (containing only the pivot), the other of size p-1. n 1n-1 n-21 1 1
17
Quicksort: Complexity Analysis Worst case: There are n-1 invocations of quicksort (not counting base cases) with arrays of size: p = n, n-1, n-2, …, 2 Since each of these does O(p), the total number of accesses is O(n) + O(n-1) + … + O(1) = O(n 2 ) Ironically the worst case occurs when the list is sorted (or near sorted)!
18
Quicksort: Complexity Analysis The average case must be between the best case O(n log n) and the worst case is O(n 2 ). Analysis yields a complex recurrence relation. The average case number of comparisons turns out to be approximately 1.386*n*log n – 2.846*n. Therefore the average case time complexity is O(n log n).
19
Quicksort: Complexity Analysis Best case O(n log n) Worst case O(n2) Average case O(n log n) Note that the quick sort is inferior to insertion sort and merge sort if the list is sorted, nearly sorted, or reverse sorted.
20
Closest Pair Of Points Given n points in 2D, find the pair that are closest.
21
Applications We plan to drill holes in a metal sheet. If the holes are too close, the sheet will tear during drilling. Verify that two holes are closer than a threshold distance (e.g., holes are at least 1 inch apart.)
22
Air Traffic Control 3D --- Locations of airplanes flying in the neighborhood of a busy airport are known. We want to be sure that no two planes get closer than a given threshold distance.
23
Simple Solution For each of the n(n-1)/2 pairs of points, determine the distance between the points in the pair. Determine the pair with the minimum distance. O(n 2 ) time
24
Divide and Conquer Solution When n is small, use simple solution. When n is large, – Divide the point set into two roughly equal parts A and B. – Determine the closest pair of points in A. – Determine the closest pair of points in B. – Determine the closest pair of points such that one point is in A and the other in B. – From the three closest pairs computed, select the one with least distance.
25
Example Divide so that points in A have x coordinate <= that of points in B.
26
Example Find the closest pair in A. Let d 1 be the distance between the points in this pair.
27
Example Find the closest pair in B. Let d 2 be the distance between the points in this pair.
28
Example Let d = min{d 1,d 2 }. Is there a pair with one point in A and the other in B, and their distance < d?
29
Example Candidates lie within d of the dividing line. Call these regions R A and R B, respectively.
30
Example Let q be a point in R A. q need to be paired only with those points in R B that are within d of q.y.
31
Example Points that are to be paired with q are in d x 2d rectangle of R B (comparing region of q). Points in this rectangle are at least d apart. (What is the maximum number of points in this rectangle?)
32
Example So the comparing region of q has at most 6 points. So number of pairs to check is <= 6 * |R A | = O(n).
33
Time Complexity Create a sorted list of points (by x-coordinate): O(n log n) time Create a sorted list of points (by y-coordinate): O(n log n) time Using these two lists, the required pairs of points from R A and R B can be constructed in O(n) time. Let n < 4 define a small instance.
34
Time Complexity Let t(n) be the time to find the closest pair excluding the time to create the two sorted lists. t(n) = c, n < 4, where c is a constant. when n >= 4, t(n) = t(ceil(n/2)) + t(floor(n/2)) + dn, where d is a constant. To solve the recurrence, assume n is a power of 2 and use repeated substitution. t(n) = O(n log n)
35
Closest Pair Of Points Given n points in 2D, find the pair that are closest. C++ routines are given on pp. 695-699 of the text. The complete program is also available at http://www.mhhe.com/engcs/compsci/sahni/source.mhtml.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.