42
1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get >=40 marks in total for Q1+Q2. To pass outcome 2, you need to give the algorithm for Q3. You have MANY chances to pass the three outcomes. You pass outcome 1, 2 or 3 for the whole course, if you pass each outcome ONCE. The chances are Assignment 1-4 plus mid-term in Week 7. I want every student to pass the three outcomes before the final exam. Do not worry about this.

1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

1

Comments for Assignment 1Q1 is basic part and is also part of outcome 1.Q2 is for outcome 1.Q3 is for outcome 2.

To pass outcome 1, you need to get >=40 marks in total for Q1+Q2.

To pass outcome 2, you need to give the algorithm for Q3.You have MANY chances to pass the three outcomes.You pass outcome 1, 2 or 3 for the whole course, if you pass each

outcome ONCE.The chances are Assignment 1-4 plus mid-term in Week 7.I want every student to pass the three outcomes before the final

exam. Do not worry about this.

Page 2: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

2

Page 3: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

3

Page 4: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

4

See the demo slide on the website.

Page 5: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

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

Page 6: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

6

Page 7: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

7

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

Page 8: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

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

Page 9: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

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

Page 10: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

10

Page 11: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

11

Page 12: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

12

Page 13: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

13

Page 14: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

14

Page 15: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

15

Page 16: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

16

Page 17: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

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

Page 18: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

18

Page 19: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

19

Page 20: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

20

Page 21: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

21

Page 22: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

22

Page 23: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

23

Page 24: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

24

Page 25: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

25

Page 26: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

26

Page 27: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

27

Page 28: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

28

Page 29: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

29

Can be done in O(n) time if we sort all the points first.

Page 30: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

30

Page 31: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

31

Closest-Pair(p1, …, pn) {xg: array sorted based on x in increasing orderyg: array sorted based on y in increasing orderCompute separation line L such that half the points are on one side and half on th

e 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 .}

Page 32: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

32

The closest Pair (see the program )• Combine (The last step)

(0,0)

x

y

10 20 30 40 50 60 70 80

10

20

30

40

50

60

70

80

(0,5)

(5,50)

(17,40)

(10,4)

(12,60)

(69,55)

(74,43)

(65,14)

(60,1) (78,2)(31,0) (48,0)

(37,8)(30,13) (43,9)

(42,19)(38,20)

(50,25)

(41,30)(39,30)

(36,40) (46,40)

(40,50)(49,55)

(34,60)

(44,65)

(33,70)

(47,75)

Page 33: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

33

The closest Pair (see the program )• Combine (The last step)

(0,0)

x

y

10 20 30 40 50 60 70 80

10

20

30

40

50

60

70

80

(0,5)

(5,50)

(17,40)

(10,4)

(12,60)

(69,55)

(74,43)

(65,14)

(60,1) (78,2)(31,0) (48,0)

(37,8)(30,13) (43,9)

(42,19)(38,20)

(50,25)

(41,30)(39,30)

(36,40) (46,40)

(40,50)(49,55)

(34,60)

(44,65)

(33,70)

(47,75)*

*

*

1st:blue. 2nd:green,

3rd: orange

Page 34: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

34

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

Page 35: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

35

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

Page 36: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

36

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

Page 37: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

37

• 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

Page 38: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

38

• 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

Page 39: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

39

• 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

Page 40: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

40

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

Page 41: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

41

• 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

Page 42: 1 Comments for Assignment 1 Q1 is basic part and is also part of outcome 1. Q2 is for outcome 1. Q3 is for outcome 2. To pass outcome 1, you need to get

42

Closest pair of points

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