Skip to content
Minh-Thien Nguyen
Go back

XOR Basis

Tiếng Việt

Table of contents

Open Table of contents

Introduction

The xor basis is a technique for problems about the xor sum of subarrays (contiguous or not) of an array of integers, or about appending elements to an array while answering queries.

The technique splits into two main parts:

Some concepts

Vector space

A set VV \ne \varnothing is a vector space if VV carries two operations: addition and multiplication by a scalar. The elements of VV are called vectors.

Some special vector spaces:

Linear independence and dependence

A set of vectors {v1,v2,,vn}\{v_1, v_2, \ldots, v_n\} is linearly independent if the equation

x1v1+x2v2++xnvn=Ox_1v_1 + x_2v_2 + \ldots + x_nv_n = O

has only the trivial solution x1=x2==xn=0x_1 = x_2 = \ldots = x_n = 0.

Conversely, if there exist x1,x2,,xnx_1, x_2, \ldots, x_n not all zero with

x1v1+x2v2++xnvn=O,x_1v_1 + x_2v_2 + \ldots + x_nv_n = O,

the set is linearly dependent.

The vector space Z2d\mathbb{Z}_2^d

Z2\mathbb{Z}_2: Zm\mathbb{Z}_m is the set of residues when dividing by mm, and the operations on Zm\mathbb{Z}_m also divide by mm. So Z2\mathbb{Z}_2 is the set of residues when dividing by 22, hence Z2={0,1}\mathbb{Z}_2 = \{0, 1\}.

Z2d\mathbb{Z}_2^d: a dd-dimensional vector space containing every vector with dd coordinates, each coordinate an element of Z2\mathbb{Z}_2.

From here on we only care about the vector space Z2d\mathbb{Z}_2^d. Every computation is taken modulo 2, and we treat \oplus (bitwise xor) as the ++ of Z2d\mathbb{Z}_2^d.

Linear span

A vector space VV is spanned by a set of vectors S={v1,v2,,vn}S = \{v_1, v_2, \ldots, v_n\} if it contains every vector xx expressible as a linear combination of the vectors in SS. span(S)={i=1ncivi|viS,ci{0,1}}\text{span}(S) = \left\{\sum_{i = 1}^n c_i v_i \, \middle| \, v_i \in S, c_i \in \{0, 1\} \right\} Then we say VV is spanned by SS, VV is generated by SS, or SS generates VV. Note: since we only work in Z2d\mathbb{Z}_2^d, each coefficient cic_i is also a binary value. So in the combination above each viv_i has exactly two states: present or absent. Example: find span({2,5})(\{2, 5\}), span({2,5,7})(\{2, 5, 7\}) and span({})(\{\})?

span({2,5})=span({2,5,7})={0,2,5,7}span({})={0}\text{span}(\{2, 5\}) =\text{span}(\{2, 5, 7\}) = \{0, 2, 5, 7\} \\ \text{span}(\{\})= \{0\}

Some important properties:

Basis

A set of vectors B={v1,v2,,vn}B = \{v_1, v_2, \ldots, v_n\} is a basis of a vector space VV if span(B)=V(B) = V and BB is linearly independent. Then nn is the dimension of VV, written dim(V)\text{dim}(V).

Example: consider the vector space V={0,2,5,7}V = \{0, 2, 5, 7\}. {2,5}\{2, 5\} is a basis of VV, but {2,5,7}\{2, 5, 7\} is not, because it is linearly dependent: 257=02 \oplus 5 \oplus 7 = 0. Naturally {2,7}\{2, 7\} and {5,7}\{5, 7\} are also bases of VV.

Note: the number of distinct elements in a vector space VV spanned by a basis B={v1,v2,,vn}B = \{v_1, v_2, \ldots, v_n\} is V=2n|V| = 2^n.

Matrices

Row space and column space

Consider an n×mn \times m matrix MM (nn rows and mm columns):

M=[v1,1v1,2v1,mv2,1v2,2v2,mvn,1vn,2vn,m] M = \begin{bmatrix} v_{1,1} & v_{1,2} & \cdots & v_{1,m} \\ v_{2,1} & v_{2,2} & \cdots & v_{2,m} \\ \vdots & \vdots & \ddots & \vdots \\ v_{n,1} & v_{n,2} & \cdots & v_{n,m} \end{bmatrix} RS(M)=span({row1,row2,,rown})CS(M)=span({col1,col2,,colm})RS(M) = \text{span}(\{row_1, row_2, \ldots, row_n\}) \\ CS(M) = \text{span}(\{col_1, col_2, \ldots, col_m\})

Rank of a matrix

One can prove that dim(RS(M))=(RS(M)) = dim(CS(M))(CS(M)); this common value is written dim(M)(M) and is known as the rank of MM, written rank(M)(M). (See the proof here)

Null space

To follow this part you need to be comfortable with matrix multiplication.

The null space of a matrix MM, written null(M)(M), is the set of vectors xRmx \in \mathbb{R}^m (written as columns) with:

Mx=OM \cdot x = O

Suppose:

M=[v1,1v1,2v1,mv2,1v2,2v2,mvn,1vn,2vn,m],x=[x1x2xm] M = \begin{bmatrix} v_{1,1} & v_{1,2} & \cdots & v_{1,m} \\ v_{2,1} & v_{2,2} & \cdots & v_{2,m} \\ \vdots & \vdots & \ddots & \vdots \\ v_{n,1} & v_{n,2} & \cdots & v_{n,m} \end{bmatrix} ,\quad x = \begin{bmatrix} x_1 \\ x_2 \\ \ldots \\ x_m \end{bmatrix}

Then (x1,x2,,xm)(x_1, x_2, \ldots, x_m) solves the system:

{v1,1x1+v1,2x2++v1,mxm=0v2,1x1+v2,2x2++v2,mxm=0vn,1x1+vn,2x2++vn,mxm=0 \begin{cases} v_{1, 1}x_1 + v_{1, 2}x_2 + \ldots + v_{1, m}x_m = 0 \\ v_{2, 1}x_1 + v_{2, 2}x_2 + \ldots + v_{2, m}x_m = 0 \\ \ldots \\ v_{n, 1}x_1 + v_{n, 2}x_2 + \ldots + v_{n, m}x_m = 0 \end{cases}

In particular: if the n×mn \times m matrix MM consists of mm vectors vZ2nv \in \mathbb{Z}_2^n and xZ2mx \in \mathbb{Z}_2^m (the vectors vv and xx written as columns):

M=[v1v2vm][v1,1v2,1vm,1v1,2v2,2vm,2v1,nv2,nvm,n],x=[x1x2xm] M = \begin{bmatrix} | & | & \ldots & | \\ v_1 & v_2 & \ldots & v_m \\ | & | & \ldots & | \end{bmatrix} \equiv \begin{bmatrix} v_{1,1} & v_{2,1} & \cdots & v_{m,1} \\ v_{1,2} & v_{2,2} & \cdots & v_{m,2} \\ \vdots & \vdots & \ddots & \vdots \\ v_{1,n} & v_{2,n} & \cdots & v_{m,n} \end{bmatrix} ,\quad x = \begin{bmatrix} x_1 \\ x_2 \\ \ldots \\ x_m \end{bmatrix}

then:

Mx=O{v1,1x1+v2,1x2++vm,1xm0(mod2)v1,2x1+v2,2x2++vm,2xm0(mod2)v1,nx1+v2,nx2++vm,nxm0(mod2) M \cdot x = O \\ \Longleftrightarrow \begin{cases} v_{1, 1}x_1 + v_{2, 1}x_2 + \ldots + v_{m, 1}x_m \equiv 0 \pmod 2 \\ v_{1, 2}x_1 + v_{2, 2}x_2 + \ldots + v_{m, 2}x_m \equiv 0 \pmod 2 \\ \ldots \\ v_{1, n}x_1 + v_{2, n}x_2 + \ldots + v_{m, n}x_m \equiv 0 \pmod 2 \end{cases}

Note that the null space is itself a vector space:

Ma=O,Mb=OM(a+b)=OMa=OM(ca)=O M\cdot a = O, M \cdot b = O \Longrightarrow M \cdot (a + b) = O \\ M \cdot a = O \Longrightarrow M \cdot (ca) = O

Nullity

The nullity of a matrix MM is the dimension of its null space:

nullity(M)=dim(null(M))\text{nullity}(M) = \text{dim}(\text{null}(M))

Rank-nullity theorem

For an n×mn \times m matrix MM:

rank(M)+nullity(M)=number of columns=m\text{rank}(M) + \text{nullity}(M) = \text{number of columns} = m

The proof is involved and needs several more concepts, so you can consult this one.


Share this post:

Previous Post
Auxiliary Tree/Virtual Tree