A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU A quick Algorithm for Counting Inversions Based on Segment Tree 1 Maosheng ZHONG, 2Juan ZHOU School of Information Engineering, East China Jiaotong University, Nanchang 330013, China, [email protected] *2 School of Software, East China Jiaotong University, Nanchang, 330013, China, 1 [email protected] Abstract This paper deals with the creation of segment trees involved in counting inversions. A brief overview of inversions and segment trees, description definition and character is given in general. Specificity, the segment trees interval measure is described, such as how to build a segment tree. There are two main points as following: the first point is selecting a specific attribute, the numbers of elements have visited, used to interval measure. The second pivot is constructing mathematical models for counting inversions. Thus we can build a segment tree and design a fast algorithm for seeking inversions by updating and searching operation in segment trees. This approach is both efficient and flexible. Finally we select another specific attribute (elements values) as interval measure with such point of view, and a new mathematical model is given, then you can also design the appropriate segment tree based algorithm to compute a reversions tree. Keywords: counting inversions, permutation, segment tree, algorithm, interval measure. 1. Introduction Linear Algebra was the first to use the concept of counting inversions when giving the definition of N order determinant. Counting inversions measures the level of chaotic messiness of a permutation. And it mainly used in the permutation-group-related issues and some fields involving the issues of changing the sequence of elements in the full array. Inverse number can also be applied to solve many practical problems. Two examples —— the ″14- 15″Puzzle and the Optimization Cutting Off Problem have been solved in use of the number of inversions by Zhao Jing[1].JIANG Hua has used the inverse number in a new software watermarking algorithm, the results of simulation show that the algorithm is obviously effective in improving the program speed and data rate of hidden watermark[2]. In recent years, the problem has received renewed attention from the streaming community[3, 4] . Scholars carried out the work of studying efficient algorithm to counting inversions. Counting inversions in O(n lg n) time (e.g., by merge sort) is a textbook problem and segment tree is a data structure that is well know, but this paper is very clear exposition of how the segment tree as a tool to build mathematical models to counting inversions, This method is worthy of reference to address a variety of related engineering problems. Definition 1. Suppose there is a permutation of a[1], a[2],..., a[n] that is constituted by n elements. If i j and a[i] a[ j ] , then (a[i], a[ j ]) is called an inverted sequence pair or inversion. How to count the number of inversions in a permutation is our concern. Example 1. Given: n 8, a {3, 2,1,6, 4,7,8,5} , then t[1] 2, t[2] 1, t[3] 0, t[4] 1, t[5] 3, t[6] t[7] t[8] 0 . The the number of inversions is 7. Definition 2. The permutation is even if the number of inversions it contains is even. Otherwise it is odd. Counting inversions is generally divided into n parts to calculate[5]. Let t[i ] be the number of the inversions (a[ j ], i) , that is the number of the numbers on the left side of International Journal of Digital Content Technology and its Applications(JDCTA) Volume 9, Number 1, February 2015 i and the ones which are 64 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU bigger than i . Then the number of inversions is t[1] t[2] ... t[n] . Generally the computing t[i ] is: The elements in a permutation, c[1], c[2],...c[n] are all set zeros. To number a[ j ] c[ j ] be 1. And let i be in position p , counting the number of 1 on the which are bigger than i , let t[i ] . And then set c[ p] 1 . The next step is calculating t[i 1] . left of p is method of This computing method on definition needs n(n 1) / 2 times comparison which the time complexity 2 is O (n ) .Rotation and merge sort are used to calculate inversion number of permutation by ZHOU Shang-chao[6]. Arborescence Array is used to calculate inversion number by the author[5], and Arborescence Array has excellent space complexity. Paper [6] and [7] are very good whose time complexity reaches O (n log 2 n) . This paper proposes another kind of algorithm using new ideas, whose time complexity also reaches O ( n log 2 n) . It puts the elements in permutation to the leaves in segment tree in proper order, using the method of statistical measure to calculate interval number successfully. 2. Segment Tree Segment tree which is also called (integer) interval tree, is a kind of binary search tree which maintains the dynamic collection[8-9]. Each node in the tree is an interval [s,e]. For example, the segment tree defined in the interval [1,8] is as follows: the whole interval [1,8] is the tree’s root, which is called the 1 st node or the 1 st segment of the tree; It includes a left child node [1, 4] and [5,8] , which is the 2 nd node and the 3 rd node respectively; The 2 nd node [1, 4] has a left child node(the 4 th node [1, 2] ) and a right child node(the 5 th node [3, 4] ); If it goes on a right child node like this, at last 15 nodes will constitute this tree. As shown in figure 1: Figure 1. The interval tree of 15 nodes Segment tree is built on the basis of segment, which was originally used to deal with the vector of computational geometry and later widely applied in all kinds of large-scale data retrieval because it has excellent speed of interval search. And the time complexity of insert, search and update are all O (log 2 n ) , which is superior to the linked list and any other data structures. L is not a power of 2, then expand it to N 2k . The segment tree difined by the interval [1, N ] is as follows: The root of the tree(the whole segment) [1, N ] , is called the Definition 3. In interval [1, L] , if 1st node of the tree. There are N numbers or it is said that the length of the 1st node is N . It is said that the 1st node is the node of the 1 st generation, which uses s[1] 1, e[1] N to indicate. If s[i] e[i] , 65 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU then the ith node has two child nodes——left child node and right child node. The left child node of [s[i], e[i]] is the 2i th node, then: s[2i] s[i], e[2i] The right child node is the 2i 1 th node, then: ( s[i ] e[i]) 2 s[2i 1] e[2i ] 1, s[2i 1] e[i] N is the total length of the segment tree. And [1,n] is the total range of the segment tree. The segment trees in the interval [1, N ] mentioned below, all meet N 2k . Property 1. There are 2 N 1 nodes in the segment tree of the interval [1, N ] . The parent node i th node is the i/2 th node. There are 2 j1 nodes in the j th generation and the length of j 1 the interval is N/(2 ) . Especially, there are N nodes in the k 1 th generation and the length of the interval is 1. To the N th node, s[ N ] 1, e[ N ] 1 . That is to say, there is only one number of the in each interval s[ N i] i 1, e[ N in the latest generation.To the th i] i 1,…,To the 2 N 1 node, s[2 N 1] N i th N , e[2 N 1] node, N. 3. Interval Measure On the rectilinear coordinate, the segment is the part between two endpoints of the straight line. The length of any segment[s,e],length[s,e] equals to e-s. Point X falls on the segment[s,e]. Then we can say that segment[s,e] contains point X. Segment tree inherits these properties of segment very well. Definition 4. The inclusion relation between the leaf[h,h] and the interval[s,e]: When the leaf falls on the interval, it can be said that the interval contains the leaf. It can also be said that the leaf belongs to the interval. That is, if s h e , then the inclusion relation is [h, h] [ s, e] . Definition 5. The inclusion relation between nodes: If i s and j e , then [i, j ] [s, e] . Definition 6. Interval measure, is any interval[s,e] in interval tree[1,n]. It has a value of measure m, m[s,e] which is a nonnegative integer. But it is not changeless. Initially, the values of measure of all the intervals are all 0. In the algorithm using segment tree to solve problems, accessing to leaves is always needed and the leaves which have already been updated will no longer be updated. After every update of leaves [h,h], the value of measure of the leaves are updated m[h,h]=1. If meeting [h, h] [ s, e] , then the value of measure also adds 1. [h, h] [i, j ] and [i, j ] [ s, e] , then [h, h] [s, e] . (log2 n ) 1 1 nodes in the Next we will discuss about accessing to value of measure. There are 2 (log2 n ) 1 1 intervals. But actually, the total number of intervals segment tree[1,n], which represents 2 Property 2. Transitivity: If is much more than that. For example, in figure 1, the interval[5,8] is the node 3, while we cannot find the node corresponding to the interval[1,5]. When we design algorithm, we only need to store interval measure of tree’s nodes. The value of measure of the node k which is not a leaf——m, should meet: m = leftchild.m + rightchild.m 66 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU Among them, leftchid is the left child node of k, while rightchild is the right child node of k. Then, how to solve the value of interval measure of non-nodes? Obviously, we must solve it by summarizing the measure of tree’s nodes. There can be a variety of calculation methods, like m[1,5]=m[1,4]+m[5,5],m[1,5]=m[1,2]+m[3,4]+m[5,5],m[1,5]=m[1,1]+m[2,2]+m[3,3]+m[4,4]+m[ 5,5] and so on. Obviously, the first calculation formula is the fastest because it has only one addition. Therefore, it will be used in the algorithm design of this paper. 4. Construct Permuted Interval Tree The storage space of interval tree is 2N-1.The path from the roots to the leaves needs to pass through log 2 N nodes. If you want to access to each leaf, the total access time will be N log 2 N . The method of segment tree approach divide-and-conquer method [8-9]. Interval tree is completely binary [10], which can be indicated by one-dimensional array. And the relationship between the child nodes and the parent nodes is already reflected in its serial number, i.e., the parent node of the i-th node is the i/2-th node. Then we can increase other arrays according to the specific problem. Let the interval of i-node be [left,right] and increasing the array of num. num[i] represents the number of elements which have visited in the interval of i-node— —[left,right]. Initializing all the num[i] with 0 at the beginning. The num here is the interval measures. And then constructing interval tree. As shown in figure 2, in each leaf node, left=right and its value is exactly the value of the permuted element. Figure 2. Construct permuted interval tree The algorithm of constructing interval tree is as follows: void buildTree(int left, int right, int current) {// current is the current root node num[current] = 0; if(left == right) return ; int mid = (left+right)>>1; buildTree(left,mid,current<<1);// Set up the left subtree,“current <<1”equals to “current *2” buildTree(mid+1,right,current<<1|1); // Set up the right subtree //“current <<1|1”equals to “current*2+1” } 67 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU 5. Mathematical Modeling Counting inversions, we might as well consider from the position——Starting from the rightmost element, to the leftmost one. Accessing from the element whose position=n to the element whose position=1. It will search the interfal measures m[1,a[position]-1] in interval tree during each access, which is the number of inversions which are consist of this element and the right ones of it. For example, in figure 2, the interval of Node 2 is [1,4] and the interval of Node 12 is [5,5]. So it can be m[1,5]=m[1,4]+m[5,5]=num[2]+num[12]. As shown in figure 4, we need to calculate the number of elements which is in the right side of it in the replacement and smaller than it when accessing to the element 6. In terms of the spatial position in segment tree, the elements which are smaller than 6 is located in the leaves which are in the left side of it. That is to say, in figure 4, the number of elements which have already been visited and are in the left side of element 6, is the value of num in the interval [1,5]. Then an algorithm to solve m[1,5] can be designed. Because [i, i] [1, 4] [1, 4] [1,5], i 1, 2,3, 4 and m[1,4]=m[1,1]+m[2,2]+m[3,3]+m[4,4], when counting m[1,5] from the root node, accessing to the interval [1,4] and getting m[1,4], it cannot be searched any longer. The final inverse number is the total interval measure value of each access. Introducing an array variable b. b[i] means the number of the elements which is in the right side of the element i and smaller than i in the replacement. From the above analysis, the mathematical model of this problem is as follows: pn b[a[ position]] m[1, a[ position] 1] c[ p ], p position (1) (if (a[ p ] a[ position]) c[ p ] 1 el se c[ p ] 0) n n i 1 i 1 t (i) b[i ] 1 b[ a[ position]], i a[ position] (2) position n b a[position] =num[ ]+num[ ]⋯+num[ ], ( , , ⋯, intervals compose the intervals [1, a[position]-1]) (3) The process of calculating b[6] is as shown in figure 3-5: a {3, 2,1, 6, 4, 7,8,5} Figure 3. the state of the value of num after accessing to four elements(4,7,8,5) 68 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU a {3, 2,1, 6, 4, 7,8,5} Figure 4. when accessing to element 6, using the count function to calculate b[6]=num[2]+num[12]=2, via node 1,2 and node 1,3,6,12 a {3, 2,1, 6, 4, 7,8,5} Figure 5. Update state after access to the element 6 using the update function, Updating the value of num of node 1,3,6,13 5. High-speed algorithm of solving inverse number by counting interval measure of segment tree According to the above mathematical model, we can convert finding out the value of t into finding out the value of b. From right to left, to each position, count m[1, a[posion ]-1]. Then access to the leaf of element a[position] from the root and update the interval measure of the passing nodes. The algorithm is described as follows: void caculateReversNumber() { buildTree(1, n, 1); int ans = 0; for ( int posion=n; posion>0; --posion ) {//Handling position: posion. Its element is a[posion ] b[a[posion]]= count(1, a[posion ]-1, 1, n, 1); ans =ans+ b[a[posion]];//ans+= b[a[posion]]; 69 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU update(a[posion ], 1, n, 1);//Accessing to the leaf of the element a[i] from the root node and updating the value of num of passing nodes. Make its value be the number of elements which have already been accessed and fall in the interval of the nodes. } printf("the inverse number of a is %d . \n", ans); } int count(int searchLeft, int searchRight, int left, int right, int current) { //Get the value of num in the searching area [searchLeft , searchRight] //The interval of current node——current, is [left,right] if(searchLeft > searchRight) return 0; if(searchLeft <=left && right <= searchRight) return num[current];//Never search again int mid = (left+right)>>1; int ret = 0; if(searchLeft<= mid) ret += count(searchLeft, searchRight, left,mid,current<<1); if(searchRight > mid ) ret += count(searchLeft,searchRight, mid+1,right,current<<1|1); return ret; } void update( int pos, int left, int right, int current) { //Interval updating num[current]++; if(left == right) { return ; } int mid = (left+right)>>1; if(pos <= mid ) update(pos, left,mid,current<<1); else update(pos, mid+1,right,current<<1|1); } 6. Conclusion The most common application of segment tree is to record whether segments are covered, and inquire the total length of the covered segments at any time.Segment tree is widely used in a variety of problems.LIN Xian-li ingenious use segment tree to solve the loaction of a point in a set of rectangles problems and improve the algorithm complexity of time and space. In order to accelerate the load shedding process,segment trees have been utilized to reduce the cost on shedding evaluation in load shedding strategies on sliding window joins over data streams by HAN Dong-hong[12] .Philippe Salembiera [13]deals with the creation of visual segment trees involved in MPEG-7 Description Schemes which is both efficient and flexible.Gabriel Renaud [14] annotates of genomic coordinates and single nucleotide variations using segment trees rapidly. Inbok Lee[15] uses the segment tree for IP address lookup , significantly reducing the memory requirements. From these applications, we can treat segment tree as a kind of way of thinking——Start from some key points of the problem, abstract which one is the leaf, abstract the concept of the problem to the interval measure of segment tree, reckon the mathematic models, design interval access functions and update functions. So that we can find out efficient algorithm of solving problems. If we look at problems from different perspectives, we can get all kinds of schemes to solve problems. This paper starts from the position of elements. If we changed to another perspective— —thinking about the value of elements, we can design the segment tree of permutation a in order of the element value. Its mathematic model is as follows: 70 A quick Algorithm for Counting Inversions Based on Segment Tree Maosheng ZHONG, Juan ZHOU at[a[i ]] i , i 1..n 1 Inverse t[i ] in t[i ] find (at[i ]), i 1...n From this, we can get another described algorithm of solving the inverse number(inversed). It returns the value of measure to t[i] while updating the measure of each node, which is unlike the algorithm described in this paper——dividing into two steps: count and update. However, the efficiency of the algorithm of these two models are the same, which means their time complexity are both O (n log 2 n) . 7. Acknowledgment This work was supported by the National Natural Science Fund of China (No. 61462027). 8. References [1] ZHAO Jing,YAN Shang-an,YU Jian-min.The Applications of Inverted Sequence Number. Mathematics in Practice and Theory, vol.32,no.6,pp.963-967, 2002. [2] JIANG Hua,SHA Zong-lu,XUAN Ai-cheng. Software watermarking algorithm based on inverse number of expression.vol.39,no.12,pp. 3188-3190,2009 [3] Anupam Gupta and Francis Zane. Counting inversions in lists. In Proc. 14th ACM /SIAM Symposium on Discrete Algorithms (SODA), pp. 253-254, 2003. [4] Miklos Ajtai, T. S. Jayram, Ravi Kumar, and D. Sivakumar. Approximate counting of inversions in a data stream. In Proc. 34th ACM Symposium on Theory of Computing (STOC), pp.370-379, 2002. [5] Richard A.Brualdi. Introductory combinatorics. North-Holland: University of Michigan Press.2002 [6] ZHOU Shang-chao.A quick Algorithm for Inversion Number of Permutation . Journal of East China Jiaotong University. vol.24, no.1,pp. 87-89,2007. [7] ZHOU Juan,CAO Yi-qin,XIE Xin. Algorithm of inverse Number Based on Arborescence Array. Journal of East China Jiaotong University.vol.28, no.2, pp.45-49, 2011 [8] LIU R.,HUANG L.. Algorithm Art and Information science contest. Bejing:Tsinghua University Press, 2004 [9] WANG Xiao-dong. Design and Analysis of Computer Algorithms. Bejing: Publishing House of Electronics Industry,2006. [10] YAN Wei-min,WU Wei-min. Data Structures(C Language), Bejing: Tsinghua University Press .2007 [11] LIN Xian-li, FU Qing-xiang. Location of a Point in a Set of Rectangles .Journal of Fuzhou University(Natural Science),vol. 27, no.4, pp.24-28, 1999. [12] HAN Dong-hong,GONG Pi-zhen,XIAO Chuan, ZHOU Rui. Load Shedding Strategies on Sliding Window Joins over Data Streams.Journal of Computer Research and Development.vol. 48, no.1, pp.103-109, 2011. [13] Philippe Salembiera,Joan Llachb,Luis Garridoa Visual. Segment Tree Creation for MPEG-7 Description, SchemesPattern Recognition, no.35, pp.563-579, 2002. [14] Gabriel Renaud, Pedro Neves, Edson Luiz Folador, Carlos Gil Ferreira, Fabio Passetti.Segtor: Rapid Annotation of Genomic Coordinates and Single Nucleotide Variations Using Segment Trees[EB/OL]. http://www.plosone.org/article/ info%3Adoi%2F10.1371%2 Fjournal.pone.0026715 .Software for the Annotation of Genomic Coordinates. 2011,6(11). [15] Inbok Lee,Jeong-Shik Mun,and Sung-Ryul Kim. IP Address Lookup with the Visualizable Biased. Fuzzy Systems and Knowledge Discovery pt.12nd.NATL metting. pp. 1137-1140, 2005 71
© Copyright 2025 Paperzz