Title: Lecture 7, Feb 23
1Lecture 7, Feb 23
2CSC373Algorithm Design and AnalysisAnnouncements
3Divide and Conquer
4Evaluating T(n) aT(n/b)nc logkn
5Sorting Problem Specification
- Precondition The input is a list of n values
with the same value possibly repeated. - Post condition The output is a list consisting
of the same n values in non-decreasing order.
6Recursive Sorts
- Given list of objects to be sorted
- Split the list into two sublists.
- Recursively sort the two sublists.
- Combine the two sorted sublists into one entirely
sorted list.
7Four Recursive Sorts
Size of Sublists
n/2,n/2
n-1,1
Minimal effort splitting Lots of effort
recombining Lots of effort splittingMinimal
effort recombining
8Merge Sort
9Merge Sort
Split Set into Two
10Merge Sort
Merge two sorted lists into one
11(No Transcript)
12Merge Sort Sort
Time T(n)
2T(n/2) Q(n)
Q(n log(n))
13Quick Sort
14Quick Sort
Partition set into two using randomly chosen
pivot
15Quick Sort
16Quick Sort
17(No Transcript)
18Quick Sort
Let pivot be the first element in the list?
14
88
98
30
31
62
23
25
79
52
19Quick Sort
14
If the list is already sorted, then the slit is
worst case unbalanced.
20Quick Sort
Best Time
Worst Time
Expected Time
21Quick Sort
T(n) 2T(n/2) Q(n) Q(n log(n))
Best Time
Worst Time
Q(n2)
Expected Time
22Quick Sort
T(n) 2T(n/2) Q(n) Q(n log(n))
Best Time
T(n) T(0) T(n-1) Q(n)
Worst Time
Q(n2)
Expected Time
T(n) T(1/3n) T(2/3n) Q(n)
Q(n log(n))
23Quick Sort
Expected Time
T(n) T(1/3n) T(2/3n) Q(n)
Q(n log(n))
Top work Q(n)
Q(n)
2nd level
Q(n)
3rd level
levels
Q(log(n))
24Four Recursive Sorts
Size of Sublists
n/2,n/2
n-1,1
Minimal effort splitting Lots of effort
recombining Lots of effort splittingMinimal
effort recombining
25Kth Element Problem Specification
- Precondition The input is a list of n values
and an integer k. - Post condition The kth smallest in the list.
(or the first k smallest)
k3
Output 25
26Kth Element Problem Specification
Partition set into two using randomly chosen
pivot
27Kth Element Problem Specification
88
98
52
62
79
r5 elements on left
k3
28Kth Element Problem Specification
88
98
52
62
79
r5 elements on left
k8
29(No Transcript)
30Kth Element Problem Specification
T(n)
Best Time
1 T(n/2) Q(n)
Q(n)
Worst Time
n0 1
Expected Time
31Kth Element Problem Specification
T(n)
Best Time
1 T(n/2) Q(n)
Q(n)
T(n) 1 T(n-1) Q(n)
Worst Time
Q(n2)
Expected Time
32Kth Element Problem Specification
T(n)
Best Time
1 T(n/2) Q(n)
Q(n)
T(n) 1 T(n-1) Q(n)
Worst Time
Q(n2)
T(n) 1 T(2/3n) Q(n)
Expected Time
Q(n)
33Counting inversions
- Given a list of numbers. a1, a2, , an.
- Goal count the number of pairs (i,j) such that
iltj but aigtaj. - The naïve algorithm works in O(n2).
- Example input 1 4 3 2
- output 3
34Counting inversions - idea
- Divide the list into two halves
- a1, a2, , am and am1, a2, , an.
- Sort and compute the number of inversions in each
half. - Merge the two halves together, counting the
inversions between elements from the two halves. - We would like to do the merging in linear time.
35Merge-and-Count
- We count an inversion (a,b), when we insert b
into the merged result C. - When we insert from A, add nothing to the number
of the inversions.
36Merge-and-Count
- When we insert bj from B, every element remaining
in A is bigger than and contributes 1 to the
number of inversions. - The entire counting is done in linear time.
37The algorithm
O(n)
2 T(n/2)
O(n)
38The running time
- T(n) 2 T(n/2) O(n)
- log a/log b 1
- c 1
- T(n) O(n log n)
39Finding the Closest Pair of Points
- Given a set of points on the plane.
- The goal is to find the pair that is closest
together. - The naïve algorithm works in time O(n2) (checking
all the pairs). - We would like to find an algorithm that works in
time O(n log n).
40Designing the algorithm
- Denote the points by Pp1, p2, , pn.
- pi(xi,yi).
- For simplicity assume that all the coordinates
are different. - For P, keep the orderings Px and Py of points in
increasing x and y coordinate order,
respectively.
41Divide
- Find L such that half of the points are to the
left of L (Q), and hald are to the right (R). - Done in O(log n) time.
42Conquer
- We recursively determine the closest pair of
points (q0,q1) in Q, and (r0,r1) in R.
- Let ?min(d(q0,q1), d(r0,r1)).
- What if there are q in Q and r in R with d(q,r)lt
??
43Dealing with points near L
- So need to restrict attention to a narrow band
near L. - Denote this set of points by S.
- By a single pass through Py, we can order our set
S and to get Sy in O(n) time.
44Key observation
- We will only need to look at a constant number of
potential candidates for each point in S. - The number 15 can be reduced.
45Proof ok Key observation
- No two points are in the same box.
- q and r with d(q,r)lt ? cannot be separated by
more than 3 layers of boxes. - If q is in the bottom row, r must be in one of
the squares on the picture.
46Summary of the algorithm
47Summary of the algorithm 2
48Running time of the algorithm
O(1)
2 T(n/2)
O(n)
49Running time of the algorithm 2
O(n)
O(1)
50Running time 3
- The running time is given by
- T(n) 2 T(n/2) O(n).
- log a/log b 1
- c 1
- T(n) O(n log n)