Divide and Conquer: Counting Inversions

Similar documents
Closest Pair Problem

Closest Pair of Points. Kleinberg and Tardos Section 5.4

Algorithms. Margaret M. Fleck. 18 October 2010

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Analysis of Binary Search algorithm and Selection Sort algorithm

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

6. Standard Algorithms

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Sample Questions Csci 1112 A. Bellaachia

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Biostatistics 615/815

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Shortest Path Algorithms

CSE 421 Algorithms: Divide and Conquer

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

4.2 Sorting and Searching

Lecture 3: Finding integer solutions to systems of linear equations

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Converting a Number from Decimal to Binary

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Quick Sort. Implementation

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Data Structures. Algorithm Performance and Big O Analysis

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Algorithm Design and Recursion

Data Structures. Topic #12

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Section IV.1: Recursive Algorithms and Recursion Trees

Adding and Subtracting Positive and Negative Numbers

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

TREE BASIC TERMINOLOGIES

Introduction to Data Structures

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Data Structures and Algorithms Written Examination

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

IB Maths SL Sequence and Series Practice Problems Mr. W Name

Use the Navigation Pane This section covers items like: Changing the size of the Navigation Pane Minimising and expand the Navigation Pane

Binary search algorithm

5.4 Closest Pair of Points

28 Closest-Point Problems

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structures and Data Manipulation

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Calculate Highest Common Factors(HCFs) & Least Common Multiples(LCMs) NA1

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Binary Heap Algorithms

Generating Elementary Combinatorial Objects

ML for the Working Programmer

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Why? A central concept in Computer Science. Algorithms are ubiquitous.

CS473 - Algorithms I

How to Create a Custom TracDat Report With the Ad Hoc Reporting Tool

Analysis of a Search Algorithm

The Crescent Primary School Calculation Policy

Test1. Due Friday, March 13, 2015.

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

15. Functions and Lists. Example 2: Sorting a List of Numbers. Example 1: Computing the Diameter of a Cloud of Points

Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

Common Data Structures

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

ON THE EMBEDDING OF BIRKHOFF-WITT RINGS IN QUOTIENT FIELDS

Lecture Note 1 Set and Probability Theory. MIT Spring 2006 Herman Bennett

5. Linear algebra I: dimension

>

Math Content by Strand 1

Operation Count; Numerical Linear Algebra

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

3 An Illustrative Example

Dynamic Programming. Lecture Overview Introduction

Randomized Sorting as a Big Data Search Algorithm

Pseudo code Tutorial and Exercises Teacher s Version

2.3. Finding polynomial functions. An Introduction:

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Chapter 6. Linear Programming: The Simplex Method. Introduction to the Big M Method. Section 4 Maximization and Minimization with Problem Constraints

Method for customization of the user/cell phone interface

Block encryption. CS-4920: Lecture 7 Secret key cryptography. Determining the plaintext ciphertext mapping. CS4920-Lecture 7 4/1/2015

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

THIS CHAPTER studies several important methods for sorting lists, both contiguous

University of Lille I PC first year list of exercises n 7. Review

6 March Array Implementation of Binary Trees

Lecture Notes 2: Matrices as Systems of Linear Equations

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Epipolar Geometry. Readings: See Sections 10.1 and 15.6 of Forsyth and Ponce. Right Image. Left Image. e(p ) Epipolar Lines. e(q ) q R.

Least-Squares Intersection of Lines

Zabin Visram Room CS115 CS126 Searching. Binary Search

APP INVENTOR. Test Review

Discrete Mathematics Problems

EQUATIONS and INEQUALITIES

Fractions Packet. Contents

3 Monomial orders and the division algorithm

Transcription:

Divide and Conquer: Counting Inversions

Rank Analysis Collaborative filtering matches your preference (books, music, movies, restaurants) with that of others finds people with similar tastes recommends new things to you based on purchases of these people Meta-search tools same query to many search engines synthesize result by looking for similarities of resulting rankings The basis: compare the similarity of two rankings

What's similar? Given numbers 1 to n (the things) rank these according to your preference You get some permutation of 1..n Compare to someone else's permutation Extreme similarity somebody else's ranking is exactly the same Extreme dissimilarity somebody else's ranking is exactly the opposite In the middle: count the number of out of place rankings

Simplify it Count the number of inversions of a ranking r 1, r 2,...,r n count the number of out of order pairs i<j r i >r j eg: 2 1 4 3 5 2 inversions: (2,1) (4,3) Why is this synonymous with comparing two different rankings? Because we can re-number, such that one of the rankings becomes 1,2,...,n

Visualizing inversions zero inversions 1 2 3 4 5 1 2 3 4 5 one inversion 2 1 3 4 5 1 2 3 4 5 5

Visualizing inversions how many? 3 2 1 4 5 enumerate them 1 2 3 4 5 how many? 5 2 3 4 1 1 2 3 4 5 6

How many inversions? What is the maximum number of inversions for a list of length n? reverse the numbers, all pairs are inversions " n % $ ' = # 2& n.(n 1) 2

Naive algorithm Suggestions? # test all pairs c=0 for i in 0 to n-1 for j in i+1 to n-1 compare r i and r j if inversion c++

Sort Does Bubble sort count inversions? Selection sort? Insertion sort? These are O(n 2 ) Do these sorts on: and see what happens 4 2 3 5 1 1 2 3 4 5

Do it to it 4 2 3 5 1 2 4 3 5 1 1 2 3 4 5 2 3 4 5 1 1 2 3 4 5 2 3 4 1 5 1 2 3 4 5 2 3 1 4 5 1 2 3 4 5 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5

Can we do better? Notice: there are potentially n*(n-1)/2 inversions Bubble and insertion sort count each individual inversion To do better we must not count each individual inversion Think of merge sort in merge sort we do not swap all elements that are out of order with each other, we make larger distance "swaps" if we can merge sort and keep track of the number of inversions we may get an O(n logn) algorithm

Eg: [ 4 2 3 5 1 ] sort [4 2 3 5 1] sort LEFT: [4 2 3] sort left: [4 2] à [2 4]:1 inversion sort right: [3] merge(left,right) à [2 3 4] 1 inversions (3 jumps over 4) sort RIGHT: [5 1] à [1 5] 1 inversion merge(left,right) à[1 2 3 4 5] 3 inversions (1 jumps over 2,3 & 4) Total inversions: 1+1+1+3=6 (go check the visualization)

The algorithm While merging in merge sort keep track of the number of inversions. When merging an element from left: no inversions added When merging an element from right: how many inversions added? left i... merge result right j... As many elements as are remaining in left, because the element from the right jumps over them

Counting Inversions: Algorithm Sort-and-Count(L) if list L has one element return 0 and the list L divide the list into two halves A and B (r A, A) Sort-and-Count(A) (r B, B) Sort-and-Count(B) (r B, R) Merge-and-Count(A, B) return r = r A + r B + r and the sorted list R Merge-and-Count(L,R) count = 0 while L and R not empty: append smallest of Li and Rj to result if Rj smallest add number of elements remaining in L to count if one list empty append the other one to result return count, result 14

Running time Just like merge sort, the sort and count algorithm running time satisfies: T(n) = 2 T(n / 2) + cn Running time is therefore O(n log n) 15

Repeated substitution Claim. If T(n) satisfies this recurrence, then T(n) = cn log 2 n.! # T(n) = " # $ c if n =1 2T(n / 2) + cn otherwise sorting both halves merging For n > 1: T(n) = 2T(n / 2) + cn = 4T(n / 4) + cn + 2n / 2 = 8T(n / 8) + cn + cn + 4cn / 4 = 2 log 2 n T(1) + cn ++ cn = O(nlog 2 n) log 2 n 16

mergesort: Recurrence Analysis f (n) = a f (n /b) + cn d a = b = d = O(?) f (n) = " O n d $ # O n d logn $ O n log b a % ( ) if a < b d ( ) if a = b d ( ) if a > b d & $ ' $ ( 17

i = 6 6 2 auxiliary array Total: 6 18

i = 6 6 2 auxiliary array Total: 6 19

i = 6 6 2 3 auxiliary array Total: 6 20

i = 5 6 2 3 auxiliary array Total: 6 21

i = 5 6 2 3 7 auxiliary array Total: 6 22

i = 4 6 2 3 7 auxiliary array Total: 6 23

i = 4 6 2 3 7 10 auxiliary array Total: 6 24

i = 3 6 2 3 7 10 auxiliary array Total: 6 25

i = 3 6 3 2 3 7 10 11 auxiliary array Total: 6 + 3 26

i = 3 6 3 2 3 7 10 11 auxiliary array Total: 6 + 3 27

i = 3 6 3 2 3 7 10 11 14 auxiliary array Total: 6 + 3 28

i = 2 6 3 2 3 7 10 11 14 auxiliary array Total: 6 + 3 29

i = 2 6 3 2 2 3 7 10 11 14 16 auxiliary array Total: 6 + 3 + 2 30

i = 2 6 3 2 2 3 7 10 11 14 16 auxiliary array Total: 6 + 3 + 2 31

i = 2 6 3 2 2 2 3 7 10 11 14 16 17 auxiliary array Total: 6 + 3 + 2 + 2 32

i = 2 6 3 2 2 2 3 7 10 11 14 16 17 auxiliary array Total: 6 + 3 + 2 + 2 33

i = 2 6 3 2 2 2 3 7 10 11 14 16 17 18 auxiliary array Total: 6 + 3 + 2 + 2 34

i = 1 6 3 2 2 2 3 7 10 11 14 16 17 18 auxiliary array Total: 6 + 3 + 2 + 2 35

i = 1 6 3 2 2 2 3 7 10 11 14 16 17 18 19 auxiliary array Total: 6 + 3 + 2 + 2 36

first half exhausted i = 0 6 3 2 2 2 3 7 10 11 14 16 17 18 19 auxiliary array Total: 6 + 3 + 2 + 2 37

i = 0 6 3 2 2 0 2 3 7 10 11 14 16 17 18 19 23 auxiliary array Total: 6 + 3 + 2 + 2 + 0 38

i = 0 6 3 2 2 0 2 3 7 10 11 14 16 17 18 19 23 auxiliary array Total: 6 + 3 + 2 + 2 + 0 39

i = 0 6 3 2 2 0 0 2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array Total: 6 + 3 + 2 + 2 + 0 + 0 40

i = 0 6 3 2 2 0 0 2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array Total: 6 + 3 + 2 + 2 + 0 + 0 = 13 41