Documents
User Generated
Resources
Learning Center

Lecture 6 - CityU CS

VIEWS: 5 PAGES: 39

• pg 1
```									1
2
See the demo slide on the website.

3
Example 1: Whole Process

HGFEDCBA
Divide      HGFE    DCBA
Divide      HG F E DC BA
Divide      H G F E D C BA
Merge       GH EF    C D A B
Merge       EFGH     ABCD
Merge       ABCDEFGH

4
5
(T(n/2)=2T(n/4)+n/2)

6
Example 2: Whole Process

ALGORITHMS
Divide         ALGOR | JTHMS
Divide      A L |G OR |JT |H MS
Divide     A|L |G|O R |J|T|H|MS
Divide     A|L |G|O|R |J|T|H|M|S
Merge      A|L |G|O R |J|T|H|M S
Merge      A L |G O R |J T|H M S
Merge      A G L O R |H J M S T
Merge      A G H J L M O R S T

7
We can
assume that
the first rank is
1, 2, ,…n.

example

R1: g, q, p, r, m1, 2, 3, 4, 5
R2: q, p, r, g, m2, 3, 4, 1, 5

8
9
10
11
12
13
14
15
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).

16
17
18
19
20
21
22
23
24
25
26
2
(o.5 )

((0.5)2+(0.5)2)0.5=(0.52)0.5<

27
Can be done in O(n) time if we sort all the points
first.
28
29
Closest-Pair(p1, …, pn) {
xg: array sorted based on x in increasing order
yg: array sorted based on y in increasing order
Compute separation line L such that half the points are on one side and half on
the other side.
Create sorted xg1 (left half), xg2(right half), yg1(left half) and yg2 (right half). O(n)
1 = Closest-Pair(xg1,yg1) (2 = Closest-Pair(xg2,yg2)
( = min((1, 2))
Merge:
Delete all points further than  from separation line L (O(n) time)
Create array new-yg for remaining points sorted by y-coordinatein (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 .
}

30
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)

6

p6    p7
p2
4

p1
p5              p8
2
p4
p3

0        2    4    6        8   10        12   14    16

31
Find a line L1 such that 4 points are on one
side and the other 4 points are on the
other side.

6
L1
p6    p7
p2
4

p1
p5              p8
2
p4
p3

0        2    4    6        8        10        12   14    16

32
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.
δ2 =Closest- pair (Region 2) = dist(p3,p4) = 3
δ=min(δ1 ,δ2)=      2

6
L2        Reg 2             L1
2                                                   p6    p7
p1
4
p2
3
p5              p8
2
p4
p3
Reg 1
0         2         4         6        8        10        12   14    16

33
• Delete the points in Region 1 and 2 further than δ=                          2
from L2
• Compare the distance dist(p1,p3) with δ.
Here dist(p1,p3) = 13 > δ, δ is not updated.
• Closest-pair (Region 1 and 2) = dist(p1,p2) = 2 .

6
L2        Reg 2            L1
p6    p7
p1
4
13
p5              p8
2
p4
p3
Reg 1
0       2         4       6         8        10        12   14    16

34
• 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) = 10
δ2 =Closest- pair (Region 4) = dist(p7,p8) =                 2
δ= min(δ1 ,δ2)=      2

6
L2        Reg 2            L1 Reg 3              L3          Reg 4
2                                                    p6     p7
p1
4
p2                                            10                  2

p5                       p8
2
p4
p3
Reg 1
0         2         4       6         8      10            12   14          16

35
• Delete the points in Region 1 and 2 further than δ=                            2
from L3
• Compare the distance dist(p6,p8) with δ.
Here dist(p6,p8) = 5 >δ, δ 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.

6
L2        Reg 2            L1 Reg 3           L3     Reg 4
2                                                p6   1 p7
p1
4
p2
5
p5               p8
2
p4
p3
Reg 1
0         2         4       6         8      10        12    14     16

36
Consider Region 1 , 2 and Region 3, 4 as two large regions.

δ1 =Closest-pair (Region 1 and 2) = dist(p1,p2) =    2
δ2 =Closest- pair (Region 3 and 4) = dist(p6,p7) = 1
δ= min(δ1 ,δ2)= 1

6
L2        Reg 2            L1 Reg 3           L3     Reg 4
2                                                p6   1 p7
p1
4
p2
p5               p8
2
p4
p3
Reg 1
0         2         4       6         8      10        12    14     16

37
• 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.

6
L2        Reg 2            L1 Reg 3           L3     Reg 4
2                                                p6   1 p7
p1
4
p2
p5               p8
2

p3           p4
Reg 1
0         2         4       6         8      10        12    14     16

38
Closest pair of points
Question: How to handle the case, where two
points can have the same x-coordinate?

39

```
To top