Title: Divide%20and%20Conquer%20Applications
1Divide and ConquerApplications
- Sanghyun Park
- Fall 2002
- CSE, POSTECH
2Merge 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.
3Merge 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 nfront nback.
4Merge 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.
5Merge 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 beas small as
min(nfront, nback) or as large as n-1.Each
comparison is two accesses.
6Merge Algorithm - Complexity
- In the worst casethe total number of accesses
is2n 2(n-1) O(n). - In the best casethe total number of accesses
is2n 2min(nfront,nback) O(n). - The average case is between the worst and best
case and is therefore also O(n).
7Merge Sort Algorithm
- Split anArray into two non-empty parts anyway you
like.For example,front the first n/2 elements
in anArrayback 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.
8MergeSort Call Graph (n7)
- Each box represents one invocation of MergeSort.
- How many levels are there in generalif the array
is divided in half each time?
06
02
36
34
56
12
00
33
44
55
66
11
22
9MergeSort Call Graph (general)
- Suppose n 2k. How many levels?
- How many boxes on level j?
- What values is in each box at level j?
n
n/2
n/2
n/4
n/4
n/4
n/4
1
1
1
1
1
1
1
1
10MergeSort Complexity Analysis
- Each invocation of mergesort on p array
positionsdoes 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) pj).
11MergeSort Complexity Analysis
- The total number of accesses at level j
isO((invocations at level j) pj) O(2j-1
n/2j-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)
12MergeSort Complexity Analysis
- Best case O(n log n)
- Worst case O(n log n)
- Average case O(n log n)
13Quick Sort
- Quicksort can be seen as a variation of
mergesortin which front and back are defined in
a different way.
14Quicksort Algorithm
- Partition anArray into two non-empty parts.
- Pick any value in the array, pivot.
- small the elements in anArray lt pivot
- large the elements in anArray gt 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.
15Quicksort Complexity Analysis
- Like mergesort,a single invocation of quicksort
on an array of size phas complexity O(p) - p comparisons 2p accesses
- 2p moves (copying) 4p 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)
16Quicksort 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
1
n-1
n-2
1
1
1
17Quicksort Complexity Analysis
- Worst caseThere are n-1 invocations of
quicksort (not counting base cases) with arrays
of sizep n, n-1, n-2, , 2Since each of
these does O(p),the total number of accesses
isO(n) O(n-1) O(1) O(n2)Ironically
the worst case occurs when the list is sorted (or
near sorted)!
18Quicksort Complexity Analysis
- The average case must be betweenthe best case
O(n log n) and the worst case is O(n2). - Analysis yields a complex recurrence relation.
- The average case number of comparisons turns out
to be approximately 1.386nlog n 2.846n. - Therefore the average case time complexity isO(n
log n).
19Quicksort 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.
20Closest Pair Of Points
- Given n points in 2D, find the pair that are
closest.
21Applications
- 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.)
22Air 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.
23Simple 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(n2) time
24Divide 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.
25Example
- Divide so thatpoints in A have x coordinate lt
that of points in B.
26Example
- Find the closest pair in A.
- Let d1 be the distance between the points in this
pair.
27Example
- Find the closest pair in B.
- Let d2 be the distance between the points in this
pair.
28Example
- Let d mind1,d2.
- Is there a pair with one point in A and the other
in B,and their distance lt d?
29Example
- Candidates lie within d of the dividing line.
- Call these regions RA and RB, respectively.
30Example
- Let q be a point in RA.
- q need to be paired only with those points in
RBthat are within d of q.y.
31Example
- Points that are to be paired with q are in d x 2d
rectangle of RB (comparing region of q). - Points in this rectangle are at least d
apart.(What is the maximum number of points in
this rectangle?)
32Example
- So the comparing region of q has at most 6
points. - So number of pairs to check is lt 6 RA O(n).
33Time 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 RA and RB can be constructed in O(n)
time. - Let n lt 4 define a small instance.
34Time Complexity
- Let t(n) be the time to find the closest
pairexcluding the time to create the two sorted
lists. - t(n) c, n lt 4, where c is a constant.
- when n gt 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)
35Closest 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
athttp//www.mhhe.com/engcs/compsci/sahni/source.
mhtml.