Olympiad in Informatics

Linear Basis Study Notes

Overview

A linear basis should count as something within the scope of linear algebra. In OI, linear bases are used to solve problems involving bitwise operations or vector representation.

I suggest readers come back to this article after learning about vectors in high school math; I believe that will help with understanding linear bases.

Linear Spaces

A linear space is really just a vector space. A linear space is really just the set you get after filling it with infinitely many vectors. Generally speaking, a linear space can be formed from scalar multiples of vectors and sums of vectors, that is, in the following form:

\[ k_1 \vec a_i + k_2 \vec a_j \]

Linear Space
This is a simple two-dimensional vector space, which can be completely filled by the two vectors u, v in the figure.

For example, the vector space of the two-dimensional plane, represented on a coordinate system, is the set of vectors generated by infinitely many kinds of two-dimensional vectors through operations of the form above. We can understand this as the basis form from high school math. We know that in the two-dimensional vector space, the two vectors \( (1, 0), (0, 1) \) can form a linear space through operations of the form above. We call this set of vectors \( (1, 0), (0, 1) \), which can generate the whole two-dimensional linear space, a spanning subset of the two-dimensional linear space.

More formally, if the vectors in a set can form a linear space through the form of vector products and sums, then this set is called a spanning subset of the linear space.

Now let's introduce some material about spanning subsets. Given a spanning subset, if there exists an \(\vec a_i = \sum_{\vec a_j \in S} k_j \vec a_j \), then these vectors are called linearly dependent. In other words, if a spanning subset contains a vector that can be represented by several vectors in the subset other than itself, then these vectors are called linearly dependent. If a spanning subset contains no linearly dependent vectors, then the spanning subset is called linearly independent. A linearly independent spanning subset is also called a basis set (high school math material), or a basis for short. Any basis of a linear space has the same number of vectors, and this number of vectors is also called the dimension.

Representing a Spanning Subset with Matrices

Suppose we have a spanning subset in a two-dimensional linear space: \(S = \{ (1, 0), (0, 1), (2, 3), (3, 4) \}\). We can represent each vector using the row-matrix notation for vectors:

\[ \begin{gathered} \begin{bmatrix} 1 & 0 \end{bmatrix} \\ \begin{bmatrix} 0 & 1 \end{bmatrix} \\ \begin{bmatrix} 2 & 3 \end{bmatrix} \\ \begin{bmatrix} 3 & 4 \end{bmatrix} \end{gathered} \]

We can merge these vectors into an augmented matrix (the last column is all 0):

\[ \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 2 & 3 & 0 \\ 3 & 4 & 0 \end{bmatrix} \]

We find that if we process this augmented matrix with Gaussian elimination, we get this result:

\[ \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{bmatrix} \]

Yes, the pivot positions of the third and fourth rows have all become \(0\).

We find that in these matrices at most two rows have a pivot that is not \(0\), while the other positions are all \(0\). Each possible matrix actually corresponds to one linear basis of this spanning subset, and the elements of the linear basis are the ones whose pivot positions in the matrix are not \(0\). So if we want the linear basis and the dimension of a set of vectors, Gaussian elimination is all we need: the number of nonzero pivots is the dimension, and in fact the dimension is also called the rank of a matrix.

Template Example Problem: BZOJ4004 [JLOI2015] Equipment Purchase

Lian Ge has recently been playing an amazing game. In this game there are \(n\) pieces of equipment, each with \(m\) attributes, represented by the vector \(z_i(a_j, \dots ,a_m)\) \((1 <= i <= n; 1 <= j <= m)\). Each piece of equipment costs \(c_i\). Now Lian Ge wants to buy some equipment, but Lian Ge is very poor, so he is always figuring out how to spend as little money as possible to buy as much equipment as possible. For Lian Ge, if a piece of equipment's attributes can be combined from the other equipment he has bought (that is, Lian Ge can use the equipment in hand to combine the effect of this piece), then there is no need to buy it. The strict definition is: if Lian Ge has bought \(z_{i_1}, \dots, z_{i_p} \), these \(p\) pieces of equipment, then for any \(z_h\) yet to be decided, if there do not exist \(b_1, \dots, b_p\) such that \( b_1 z_{i_1} + \dots + b_p z_{i_p} = z_h\) (\(b\) are real numbers), Lian Ge will buy \(z_h\); otherwise \(z_h\) is useless to Lian Ge and naturally need not be bought. For example, \(z_1 =(1, 2, 3), z_2 =(3, 4, 5), z_h = (2, 3, 4)\), \(b_1 = \frac{1}{2},b_2 = \frac{1}{2}\), then \( b_1 z_1 + b_2 z_2 = z_h\), so if Lian Ge has bought \(z_1\) and \(z_2\), he will not buy \(z_h\) anymore. Lian Ge wants to spend the least money while buying the maximum number of pieces of equipment. Can you help him work it out?

Actually, this is just a textbook problem for linear bases, and we can write it as a template problem. We first record all the vector information into an augmented matrix, and then in Gaussian elimination, when choosing a pivot, we just try to pick the vector with the smaller cost. Here's the code:

// BZ4004.cpp
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 510;
const double eps = 1e-8;

long double mat[MAX_N][MAX_N], cost[MAX_N];
int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%Lf", &mat[i][j]);
    for (int i = 1; i <= n; i++)
        scanf("%Lf", &cost[i]);
    int dim = 0, ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int now = 0;
        for (int j = dim + 1; j <= n; j++)
            if (fabs(mat[j][i]) > eps && (now == 0 || cost[j] < cost[now]))
                now = j;
        if (now == 0)
            continue;
        dim++, ans += cost[now];
        for (int j = 1; j <= m; j++)
            swap(mat[dim][j], mat[now][j]);
        swap(cost[dim], cost[now]);
        for (int j = 1; j <= n; j++)
            if (j != dim && fabs(mat[j][i]) > eps)
            {
                long double rate = mat[j][i] / mat[dim][i];
                for (int k = i; k <= m; k++)
                    mat[j][k] -= rate * mat[dim][k];
            }
    }
    printf("%d %d", dim, ans);
    return 0;
}

The Role of Linear Bases in Bitwise Operations

In informatics competitions, linear bases are usually used more to solve bitwise-operation problems, such as finding the subset of a set with the maximum XOR sum. Let's explore the solution to the maximum subset XOR sum problem:

First let \(S = \{a_1, a_2, a_3, \dots, a_n\}\). We want to find an \(A \subset S\) such that \(xor_{a_i \in A} a_i\) is maximized. Consider transforming it with the linear basis model: for the whole of \(S\) we find its linear basis \(B\), that is, we find a spanning subset whose elements can be XORed together to form all the elements of \(S\). When transforming the model, we can take each binary bit of each number as the value of the corresponding dimension of the vector. For example, \(9 = (1001)_2\); if the numbers only go up to four bits, then this vector can be represented as:

\[ \begin{bmatrix} 1 & 0 & 0 & 1 \end{bmatrix} \]

Then it takes part in the whole Gaussian elimination process. After the Gaussian elimination is done, we get a linear basis \(B\). Because each vector in the linear basis has only one position that is not \(0\), when considering the maximum XOR sum we just traverse starting from the high bits, and during the traversal check whether the linear basis contains some vector whose value in that dimension is not \(0\): if there is one, we directly add its contribution to the answer; if not, we keep traversing. This guarantees optimality, because the contribution of a high bit is larger than all the low-bit contributions combined, so considering it first guarantees that no larger answer can appear, so it is optimal.

Here's the code (without Gaussian elimination, considering a new way to get the linear basis done; readers, please think about the method below):

// P3812.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_N = 55;
ll arr[MAX_N], n, bit[70], ans;

int main()
{
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &arr[i]);
        for (int pt = 62; pt >= 0; pt--)
        {
            if (((arr[i] >> (ll)pt) & 1) == 0)
                continue;
            if (bit[pt] == 0)
            {
                bit[pt] = arr[i];
                break;
            }
            arr[i] ^= bit[pt];
        }
    }
    for (int i = 62; i >= 0; i--)
        if ((ans ^ bit[i]) > ans)
            ans ^= bit[i];
    printf("%lld", ans);
    return 0;
}

Some Properties of the Bitwise Linear Basis

  1. The number of numbers in the linear basis is always the smallest
  2. The numbers in the linear basis cannot represent 0.