Inversion Count using Policy Based Data Structure

We will use the g++ header file to compile the code in C++ compiler. g++ is a Linux based header file that is used in C++ to compile the code of policy based data structure. The policy based data structure are such structures used for high performance and flexibility of the code. As these data structures are so resourceful that we can use them for many functions like searching the index of an element, inserting the element into the index position, removing the element from the range of index, etc.

Example

Let’s take an example of inversion count −

Suppose the inner traversal of the constructed tree is 1,2,3,4,5 and when we traverse to invert it, the tree form becomes 5,4,3,2,1.

Let’s take the following tree structure as input

 < 5, 4, 3, 2, 1 >

The given structure tree has a length of 4.Now we will consider the following steps to understand the process of inversion.

Step 1 − The element starts with index[0] i.e., 5, and pairs with each element till the index[4] i.e, 1. Hence the total count between indexes 0 to 4 is 4.

(5…4), (5…3), (5…2), (5…1)

Step 2 − The element starts with index[1] i.e., 4,and pairs with each element till the index[4] i.e, 1. Hence the total count between indexes 1 to 4 is 3.

(4…3), (4…2), (4…1)

Step 3 − The element starts with index[2] i.e., 3, and pairs with each element till the index[4] i.e, 1. Hence the total count between indexes 2 to 4 is 2.

(3…2), (3…1)

Step 4 − The element starts with index[3] i.e., 2, and pairs with each element till the index[4] i.e, 1. Hence the total count between indexes 3 to 4 is 1.

(2…1)

This way we can write the inversion of a given constructed tree. So that the total number of inversions of count(4+3+2+1) is 10.

In this article, we are going to solve the inversion count using a policy based data structure.

Syntax

The following syntax used in the program −

vector <data_type> vector_variable_name


Parameters

data_type − The type of data used for vector.

vector_variable_name − The variable name used for the vector.

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;


Parameters

typedef − This is a reserved keyword used in C++ programs.

int − The type of data for inserting the array item.

null_type − This is a mapped policy and is used as a set. If we want the map, then the second argument must be mapped type.

less<int> − The comparison between two functions.

rb_tree_tag − The type of tree used for the red black tree which is used on basis of insertion and deletion.

tree_order_statistics_node_update − This is based on the header file ‘tree_policy.hpp’ which includes the various operation for tree based containers to update the node variants. Therefore, we will keep tracking the nodes in a subtree.

pbds − The variable name based on policy based data structure.

order_of_key()


Algorithm

• We will start the program with header files namely iostream and vector. Then we will mention the g++ based header file for policy based data structure(pbds).

• We will be using the necessary namespace based on GNU of policy based data structure i.e, ‘using namespace __gnu_pbds’. It will initialize the tree format based on pbds i.e, ‘typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; by using these we will keep track of the nodes in a subtree.

• We are defining function definition ‘inversion_Cnt’ of a double long datatype which accepts the parameter of vector integer and stores the address of the array element.

• We are storing ‘0’ to the variable ‘cnt’ to work on the inversion count of the total pair.

• Then initialize the object named pb to the policy based variable ‘pbds’ to work on the insertion of an array element and pairing the order.

• After initializing the variables, use for loop to iterate the array element. This array element will work on the inversion operation based on two following statements −

• cnt += i-pb.order_of_key(arr[i]); − This return the smallest in the second argument by counting the pair values like <5,4>,<5,3>, <5,2>, <5,1>, <4,3>,<4,2> and so on.

• pb.insert(arr[i]); − By using predefined function insert() we are adding the inversion of array element i.e, arr[i].

• We start the main function and declare the vector array input.

• Then we are calling the function ‘inversion_Cnt’ with help of the ‘count’ variable.

• Finally, the ‘count’ variable gives the total number of counts of inversion in an array.

Example

In this program, we are going to count the inversion of the number by using policy based data structure.

#include <iostream>
#include <vector>
// *******g++ header file*********
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
double long inversion_Cnt( vector<int>& arr) {
double long cnt = 0;
pbds pb;
for(int i = 0; i < arr.size(); i++) {
cnt += i-pb.order_of_key(arr[i]);
pb.insert(arr[i]); // add the array element
}
return cnt;
}
int main() {
vector<int> arr = {5, 4, 3, 2, 1}; // The inversion of following input array is <5,4>, <5,3>, <5,2>, <5,1>, <4,3>, <4,2>, <4,1>, <3,2>, <3,1>, <2,1>
double long count = inversion_Cnt(arr);
cout<<"Total number of inversion count using Policy based data structure is : "<<count<<endl;
return 0;
}


Output

Total number of inversion count using Policy based data structure is : 10


Conclusion

We explored the concept of Linux header files(g++) by performing the program based on inversion count. As we all know C++ program is used in Operating Systems and it has a tracker to record every information of the system. In the same way as this program, we see how subtree is tracking its every node.

Updated on: 10-May-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Advertisements