Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.

Similar presentations


Presentation on theme: "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11."— Presentation transcript:

1 CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11

2 Another Application: Collaborative Filtering A number of sites on the Web make use of a technique known as collaborative filtering, in which they try to match your preferences with those of other people Once the Web site has identified people with “similar” tastes to yours – based on a comparison of how you and they rate various things – it can recommend new things that these other people have liked

3 Collaborative Filtering E.g., Music site tries to match your song preferences with others –You rank n songs –Music site consults database to find people with similar tastes A core issue in applications like this is the problem of comparing two rankings. How to define similarity between two rankings?

4 Counting Inversions A natural method would be to label the songs (or any other objects being ranked) from 1 to n according to your ranking and then according to the other person’s rankings, and see how many pairs are “out of order”

5 Counting Inversions Similarity metric: number of inversions between two rankings –My rank: 1, 2, …, n –Your rank: a 1, a 2, …, a n –Songs i and j inverted if i a j You Me 14325 13245 ABCDE Songs Inversions 3-2, 4-2

6 Inversion Problem Let a 1,... a n be a permutation of 1.. n (a i, a j ) is an inversion if i a j Problem: given a permutation, count the number of inversions Can be done easily in O(n 2 ) time by brute force –Can we do better? 4, 6, 1, 7, 3, 2, 5

7 Inversion Problem We are inspired by the Mergesort – each iteration of the While loop takes constant time and in each iteration, we add some element to the output that will never be seen again; the number of iterations can be at most the sum of the initial lengths of A and B so the running time is O(n) Thus we will look at a recursive algorithm Sort- and-Count which has at its centre a Merge-and- Count process to merge and count the number of iterations in two sorted lists

8 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 a i and a j are in different halves, and return sum of three quantities 481021512113769 481021512113769 5 blue-blue inversions8 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 Total = 5 + 8 + 9 = 22

9 Problem: how do we count inversions between sub problems in O(n) time? Solution: Count inversions while merging (merging is just to help with the counting) 12347111215 568910131416 Standard merge algorithm: add to inversion count when an element is moved from the upper array to the solution

10 Merge-and-Count(A,B) (A,B previously sorted) Maintain a Current pointer into each list, initialized to point to the front elements Maintain a variable Count for the number of inversions, initialized to 0 While both lists are nonempty: Let a i and b j be the elements pointed to by the Current pointer Append the smaller of these two to the output list If b j is the smaller element then Increment Count by the number of elements remaining in A Endif Advance the Current pointer in the list from which the smaller element was selected EndWhile Once one list is empty, append the remainder of the other list to the output Return Count and the merged list

11 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves auxiliary array Total: i = 6

12 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. i = 6 two sorted halves 2 auxiliary array Total: 6 6

13 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 2 auxiliary array i = 6 Total: 6 6

14 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 6 Total: 6 6

15 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 5 Total: 6 6

16 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 5 Total: 6 6

17 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 4 Total: 6 6

18 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71023 auxiliary array i = 4 Total: 6 6

19 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71023 auxiliary array i = 3 Total: 6 6

20 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101123 auxiliary array i = 3 Total: 6 + 3 63

21 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101123 auxiliary array i = 3 Total: 6 + 3 63

22 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423 auxiliary array i = 3 Total: 6 + 3 63

23 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423 auxiliary array i = 2 Total: 6 + 3 63

24 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142316 auxiliary array i = 2 Total: 6 + 3 + 2 632

25 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142316 auxiliary array i = 2 Total: 6 + 3 + 2 632

26 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

27 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

28 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181617 auxiliary array i = 2 Total: 6 + 3 + 2 + 2 6322

29 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181617 auxiliary array i = 1 Total: 6 + 3 + 2 + 2 6322

30 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142318191617 auxiliary array i = 1 Total: 6 + 3 + 2 + 2 6322

31 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 71011142318191617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 first half exhausted 6322

32 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231819231617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 6322 0

33 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 7101114231819231617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 6322 0

34 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181923251617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 + 0 6322 0 0

35 101418193716172325211 Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different halves. n Combine two sorted halves into sorted whole. two sorted halves 710111423181923251617 auxiliary array i = 0 Total: 6 + 3 + 2 + 2 + 0 + 0 = 13 6322 0 0

36 Use the merge algorithm to count inversions 141112 23715 589166101314 Indicate the number of inversions for each element detected when merging

37 Counting Inversions: Combine Counting inversions between two sorted lists –O(1) per element to count inversions x x x x x x x x y y y y y y y y z z z z z z z z z z z z z z z z

38 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 a i and a j are in different halves, and return sum of three quantities 481021512113769 481021512113769 Divide: O(1) Conquer: 2T(n / 2) Combine: O(n)

39 Since the Merge-and–Count Procedure takes O(n) time, the running time T(n) of the full Sort- and-Count procedure satisfies: T(n) < 2T(n/2) + cn 2<n and T(2) < c So running time T(n) of full Sort-and-Count is O(n log n)

40 Counting Inversions: Implementation Sort-and-Count(L) { If list L has one element return 0 and the list L Else Divide the list into two halves A and B (r A, A)  Sort-and-Count(A) (r B, B)  Sort-and-Count(B) (r, L)  Merge-and-Count(A, B) Endif Return r = r A + r B + r and the sorted list L }


Download ppt "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11."

Similar presentations


Ads by Google