• No results found

Counting Inversions.pdf

N/A
N/A
Protected

Academic year: 2020

Share "Counting Inversions.pdf"

Copied!
35
0
0

Loading.... (view fulltext now)

Full text

(1)

Counting Inversions- Divide and

Conquer

(2)

2

•Music site tries to match your song preferences with others.

– You rank n songs.

– Music site consults database to find people with similar tastes.

•Similarity metric: number of inversions between two rankings.

– My rank: 1, 2, …, n.

– Your rank: a1, a2, …, an.

– Songs i and j inverted if i < j, but ai > aj.

You Me

1 3 4 2 5 1 2 3 4 5

A B C D E

Songs

Counting Inversions

(3)

Visualizing inversions

(4)

4

Counting Inversions:

Divide-and-Conquer

Divide-and-conquer.

4 8 10 2

(5)

5

Counting Inversions:

Divide-and-Conquer

Divide-and-conquer.

– Divide: separate list into two pieces.

4 8 10 2

1 5 6 9 12 11 3 7

4 8 10 2

1 5 6 9 12 11 3 7

(6)

6

Counting Inversions:

Divide-and-Conquer

Divide-and-conquer.

– Divide: separate list into two pieces.

– Conquer: recursively count inversions in each half.

4 8 10 2

1 5 6 9 12 11 3 7

4 8 10 2

1 5 6 9 12 11 3 7

5 blue-blue inversions 8 green-green inversions

Divide: O(1).

Conquer: 2T(n / 2)

(7)

7

Counting Inversions:

Divide-and-Conquer

•Divide-and-conquer.

– Divide: separate list into two pieces.

– Conquer: recursively count inversions in each half.

– Combine: count inversions where ai and aj are in different halves, and return sum of three quantities.

4 8 10 2

1 5 6 9 12 11 3 7

4 8 10 2

1 5 6 9 12 11 3 7

5 blue-blue inversions 8 green-green inversions

Divide: O(1).

Conquer: 2T(n / 2)

Combine: ???

9 blue-green inversions

5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7

(8)

Counting Inversions

4 7 6 2 1 5 8 3

Divide into front and back halves

4 7 6 2 1 5 8 3

Count inversions within each half, and sort each half 2 4 6 7 1 3 5 8

2 4

(9)
(10)
(11)

11

10 14 18 19

3 7 2 11 16 17 23 25

Counting Inversions

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

auxiliary array

(12)

12

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

i = 6

two sorted halves

2 auxiliary array

Total: 6

6

(13)

13

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

2 auxiliary array

i = 6

Total: 6

6

(14)

14

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

2 3 auxiliary array

i = 6

Total: 6

6

(15)

15

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

2 3 auxiliary array

i = 5

Total: 6

6

(16)

16

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7

2 3 auxiliary array

i = 5

Total: 6

6

(17)

17

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7

2 3 auxiliary array

i = 4

Total: 6

6

(18)

18

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10

2 3 auxiliary array

i = 4

Total: 6

6

(19)

19

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10

2 3 auxiliary array

i = 3

Total: 6

6

(20)

20

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11

2 3 auxiliary array

i = 3

Total: 6 + 3

6 3

(21)

21

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11

2 3 auxiliary array

i = 3

Total: 6 + 3

6 3

(22)

22

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 auxiliary array

i = 3

Total: 6 + 3

6 3

(23)

23

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 auxiliary array

i = 2

Total: 6 + 3

6 3

(24)

24

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 auxiliary array

i = 2

Total: 6 + 3 + 2

6 3 2

(25)

25

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 auxiliary array

i = 2

Total: 6 + 3 + 2

6 3 2

(26)

26

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

(27)

27

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

(28)

28

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 auxiliary array

i = 2

Total: 6 + 3 + 2 + 2

6 3 2 2

(29)

29

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 auxiliary array

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

(30)

30

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 19 auxiliary array

i = 1

Total: 6 + 3 + 2 + 2

6 3 2 2

(31)

31

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 19 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2

first half exhausted

6 3 2 2

(32)

32

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 19 23 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0

6 3 2 2 0

(33)

33

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 19 23 25 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0 + 0

6 3 2 2 0 0

(34)

34

10 14 18 19

3 7 2 11 16 17 23 25

Merge and count step.

– Given two sorted halves, count number of

inversions where ai and aj are in different halves.

– Combine two sorted halves into sorted whole.

two sorted halves

7 10 11 14

2 3 16 17 18 19 23 25 auxiliary array

i = 0

Total: 6 + 3 + 2 + 2 + 0 + 0 = 13

6 3 2 2 0 0

(35)

The e-commerce application

Have you ever noticed on any e-commerce

website, they have this section of "You might

like", they have maintained an array for all the

user accounts and then whichever has the

References

Related documents

The perfect time out will have you any questions interview questions that position will bring questions do you in the team where social data points as an expert advice is.. What

6) Install and enable security software. This applies to antivirus software, spam filters, and anti-mal- ware. Not as broadly used for mobile devices, there is some available or it

• Organize creative Team fundraising events (i.e., car wash, bake sale, garage sale), with proceeds going to team totals!. • Place a collection jar in a common area for

The link on the OP is for 4.3 flash, but the files for 4.4 are slightly different and there needs to be a PAUSE in the batch file to allow the phone to reboot after a couple

 Interactive session log can also be included but requires additional options to be specified (discussed later in this paper).  Replies are sent to the ‘from’ e-mail and not

Use the present perfect progressive to talk about the duration of an activity that began in the past and continues to the present.. My grandmother has been decorating the house

He did see that woman again and later on in the evening left that person’s body and went back to take a look at his old dead body to see if it was right.. He sensed

All rights reserved by Synergy Airflow and Ventilation, LLC Page 10 Make sure that the ground outside of the crawlspace is sloped away from the home at a rate of 6 inches of fall