I know this question has been discussed before, but i am interested in doing this using a Binary Indexed Tree. I found this link to show how to do it.I did not quite follow the explanation. Could someone please give me an explanation of why the following given there is true.
Create a BIT of size greater than n(no of elements). Iterate through array A (
let j be the index of loop),and for each element A[j] do:
1) Add j-sum(A[j]) to the number of inversions
2) add(A[j], 1) (i.e. add 1 to the position A[j] on BIT. This effectively
counts the number of time value A[j] is seen so far)
I dont get why this works.
One way to help calculate the inversion number is to look at each position in the permutation and count how many smaller numbers are to the right, and then add those numbers up. An inversion in a permutation is a pair of numbers such that the larger number appears to the left of the smaller one in the permutation.
How many inversions are there in the array arr = {1,5,4,2,3}? Explanation: The necessary condition for an inversion is arr[i]>arr[j] and i<j. So there are 5 inversions in the array.
In computer science and discrete mathematics, an inversion in a sequence is a pair of elements that are out of their natural order.
Explanation: Removing the 1st and 2nd array elements modifies the array to {4, 1}. Therefore, the maximum number of inversions is 1.
An inversion occurs when an element is larger than some element that follows it in the array.
We can count inversions by grouping them by second element. For example, in the array [4, 3, 1, 2], the element pairs (4, 3), (4, 1), (4, 2), (3, 1), and (3, 2) are inversions. We group them by second element, hence: [[(4, 1), (3, 1)], [(4, 2), (3, 2)], [(4, 3)]].
We consider each element in turn, and count how many inversions it is the second element of. In the example, the element 4 is the second element in 0 inversions, the element 3 in 1 inversion, and the elements 1 and 2 in 2 inversions each.
In order for any given element to be the second element of an inversion, there has to be a larger element somewhere before it in the array.
We perform the count efficiently by traversing the array from left to right and always keeping track of how many elements of each value have been encountered so far, using a BIT. Initially our frequency table will be [0, 0, 0, 0], since we've seen no elements at all. After we visit the 4, we update its frequency, giving [0, 0, 0, 1]. After visiting the 3, [0, 0, 1, 1], and so on.
Each time we visit a position, we use the BIT to find out how many elements visited so far are greater than it. So for example when we encounter the 1, the BIT currently contains [0, 0, 1, 1], representing that there were so far zero 1's and 2's, one 3, and one 4. By adding the values 0 + 1 + 1, we count the number of elements so far that are greater than 1.
Adding all these individual counts gives the total number of inversions.
Note that, in general, you must employ coordinate compression in order for this to be efficient. For example, if your initial array contains numbers like A = [92, 631, 50, 7], you shouldn't allocate a BIT with hundreds of elements. Instead, sort the array to determine that 7 < 50 < 92 < 631, which allows us to assign the ranks 7 => 1, 50 => 2, 92 => 3, 631 => 4; then replace each element by its rank, giving B = [3, 4, 2, 1]. The number of inversions of this array will be the same as in the original, since B[i] > B[j] if and only if A[i] > A[j].
(Note: A real programmer would probably use indices starting from zero.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With