Lecture 7, Feb 23 - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Lecture 7, Feb 23

Description:

Sum = ?(first term) = ?(f(n)). log a - c log b 0. log a - c log b = 0. log a - c log b 0 ... Sort and compute the number of inversions in each half. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 51
Provided by: jeffed
Category:
Tags: feb | halfterm | lecture

less

Transcript and Presenter's Notes

Title: Lecture 7, Feb 23


1
Lecture 7, Feb 23
2
CSC373Algorithm Design and AnalysisAnnouncements
3
Divide and Conquer
4
Evaluating T(n) aT(n/b)nc logkn
5
Sorting 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.

6
Recursive 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.

7
Four 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
8
Merge Sort
9
Merge Sort
Split Set into Two
10
Merge Sort
Merge two sorted lists into one
11
(No Transcript)
12
Merge Sort Sort
Time T(n)
2T(n/2) Q(n)
Q(n log(n))
13
Quick Sort
14
Quick Sort
Partition set into two using randomly chosen
pivot
15
Quick Sort
16
Quick Sort
17
(No Transcript)
18
Quick Sort
Let pivot be the first element in the list?
14
88
98
30
31
62
23
25
79
52
19
Quick Sort
14
If the list is already sorted, then the slit is
worst case unbalanced.
20
Quick Sort
Best Time
Worst Time
Expected Time
21
Quick Sort
T(n) 2T(n/2) Q(n) Q(n log(n))
Best Time
Worst Time
Q(n2)
Expected Time
22
Quick 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))
23
Quick 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))
24
Four 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
25
Kth 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
26
Kth Element Problem Specification
Partition set into two using randomly chosen
pivot
27
Kth Element Problem Specification
88
98
52
62
79
r5 elements on left
k3
28
Kth Element Problem Specification
88
98
52
62
79
r5 elements on left
k8
29
(No Transcript)
30
Kth Element Problem Specification
T(n)
Best Time
1 T(n/2) Q(n)
Q(n)
Worst Time
n0 1
Expected Time
31
Kth 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
32
Kth 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)
33
Counting 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

34
Counting 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.

35
Merge-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.

36
Merge-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.

37
The algorithm
O(n)
2 T(n/2)
O(n)
38
The running time
  • T(n) 2 T(n/2) O(n)
  • log a/log b 1
  • c 1
  • T(n) O(n log n)

39
Finding 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).

40
Designing 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.

41
Divide
  • 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.

42
Conquer
  • 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
    ??

43
Dealing 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.

44
Key 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.

45
Proof 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.

46
Summary of the algorithm
47
Summary of the algorithm 2
48
Running time of the algorithm
O(1)
2 T(n/2)
O(n)
49
Running time of the algorithm 2
O(n)
O(1)
50
Running 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)
Write a Comment
User Comments (0)
About PowerShow.com