see the demo slide on the website
DESCRIPTION
See the demo slide on the website. Example 1: Whole Process. H G F E D C B A Divide H G F E D C B A Divide H G F E D C B A Divide H G F E D C B A Merge G H E F C D A B Merge E F G H A B C D - PowerPoint PPT PresentationTRANSCRIPT
1
AnnouncementChange of Venue for Tutorial Session #T01 and #TA1
from Week 6 to Week 13 (except in Week 8):
Section Day Time
T01 & TA1 Friday 15:00 - 15:50
New Venue:
Week 6: 9 Oct - Room 1406, Academic 2
Week 7: 16 Oct - Room 1406, Academic 2
Week 8: 23 Oct - Room 2308, Academic 2
Week 9: 30 Oct - Room 1406, Academic 2
Week 10: 6 Nov - Room 1406, Academic 2
Week 11: 13 Nov - Room 1406, Academic 2
Week 12: 20 Nov - Room 1406, Academic 2
Week 13: 27 Nov - Room 1406, Academic 2
2
3
4
See the demo slide on the website.
5
Example 1: Whole Process
H G F E D C B A
Divide H G F E D C B A
Divide H G F E D C B A
Divide H G F E D C B A
Merge G H E F C D A B
Merge E F G H A B C D
Merge A B C D E F G H
6
7
(T(n/2)=2T(n/4)+n/2)
8
Example 2: Whole Process
A L G O R I T H M SDivide A L G O R | J T H M SDivide A L | G O R | J T | H M SDivide A | L | G | O R | J | T | H | M SDivide A | L | G | O | R | J | T | H | M | SMerge A | L | G | O R | J | T | H | M SMerge A L | G O R | J T | H M SMerge A G L O R | H J M S TMerge A G H J L M O R S T
9
R1: g, q, p, r, m1, 2, 3, 4, 5
R2: q, p, r, g, m2, 3, 4, 1, 5
We can assume that the first rank is 1, 2, ,…n.
example
10
11
12
13
14
15
16
17
Merge and Count Process: Another example.
1, 2, 8, 10, 11, 12; 3, 4, 5, 6, 7, 9;
4 4 4 4 4 3 # of inversions
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
Time for merge : O(n).
18
19
20
21
22
23
24
25
26
27
28
2
)5.(o
((0.5)2+(0.5)2)0.5=(0.52)0.5<
29
Can be done in O(n) time if we sort all the points first.
30
Sort the points according to the x-coordinates and get a list ListX={p1, …, pn}.Closest-Pair(p1, …, pn) {Compute separation line L such that half the points List1X={p1, p2, …, P0.5n} are on
one side and the other half List2X={p0.5n+1, …pn} on the other side.
(1, List1Y) = Closest-Pair(List1X) ;(2, LIst2Y)= Closest-Pair(List2X)( = min((1, 2))Merge: merge List1Y and list2Y to form ListY. (O(n) time) Delete all points further than from separation line L (O(n) time)Create array new-yg for remaining points sorted by y according to ListY (O(n) time).
for (i=0; i<= size of new-yg; i++) for (j=1; j<=11; j++) (the nested loop takes O(n) time) if (d(new-yg[i], new-yg[i+j]<) then =d(new-yg[i], new-yg[j];return (, ListY).}
32
Example 1• Input: points p1, p2,…,p8 in a plane. • p1=(1,4), p2=(2,5), p3=(4,2), p4=(7,2), p5=(10,3), p6=(13,4),p7=(14,4), p8=(15,3)
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
33
Find a line L1 such that 4 points are on one side and the other 4 points are on the other side.
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1
34
Consider the left four points p1,p2,p3,p4. Find a line L2 such that 2 points are on one side and the other 2 points are on the other side.δ1 =Closest-pair (Region 1) = dist(p1,p2) = . δ2 =Closest- pair (Region 2) = dist(p3,p4) = 3δ=min(δ1 ,δ2)=
2
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2
3
2
2
35
• Delete the points in Region 1 and 2 further than δ= from L2
• Compare the distance dist(p1,p3) with δ. Here dist(p1,p3) = > δ, δ is not updated.• Closest-pair (Region 1 and 2) = dist(p1,p2) = .
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2
2
2
13
13
36
• For the four points p5,p6,p7,p8, we use a line L3 to divide them and use similar method to find
δ1 =Closest-pair (Region 3) = dist(p5,p6) =
δ2 =Closest- pair (Region 4) = dist(p7,p8) =
δ= min(δ1 ,δ2)=
2
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2 L3
10 2
10
2
2
Reg 3 Reg 4
37
• Delete the points in Region 1 and 2 further than δ= from L3
• Compare the distance dist(p6,p8) with δ. Here dist(p6,p8) = >δ, δ is not updated. • Compare the distance dist(p6,p7) with δ. Here dist(p6,p7) = 1 < δ, δ is updated.• Closest-pair (Region 3 and 4) = dist(p6,p7) = 1.
2
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2 L3Reg 3 Reg 4
2
5
1
5
38
Consider Region 1 , 2 and Region 3, 4 as two large regions.
δ1 =Closest-pair (Region 1 and 2) = dist(p1,p2) =
δ2 =Closest- pair (Region 3 and 4) = dist(p6,p7) = 1
δ= min(δ1 ,δ2)= 1
2
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2 L3Reg 3 Reg 4
1
2
39
• Delete the points in Region 1, 2, 3 and 4 further than δ= 1 from L1
• Here only one point p4 is left, δ is not updated.
• Closest-pair (Region 1, 2, 3 and 4) = dist(p6,p7) = 1.
2
p2
p3
p1
p4
p7
p8
p6
p5
2 4 6 8 10 12 14 16
2
4
6
0
L1L2
Reg 1
Reg 2 L3Reg 3 Reg 4
1
40
Closest pair of points
Question: How to handle the case, where two points can have the same x-coordinate?
For those points with the same x-coordinates, sort them based on y-coordinates.