State Design Optimization

Overview

When optimizing dp, one can not only start with the transition process and accelerate transitions. Sometimes one can also start with the state definition and optimize complexity by changing how the state is designed.

What is rather troublesome is that most of these optimizations are not general-purpose; that is, they cannot be applied to multiple problems in a routine way. Therefore, the following sections start from concrete example problems and strive to provide inspiration at the level of ideas, hoping to be of some help to readers.

Example 1

Problem Statement

Given two strings of lengths n,mn,m respectively, consisting only of lowercase letters, A,BA,B, find the longest common subsequence of A,BA,B. (n≤106,m≤103)(n\le 10^6,m\le 10^3)

Naive Solution

You solved it instantly at a glance—isn't this a standard problem?

Define the state fi,jf_{i,j} as the longest common subsequence of AA's first ii characters and BB's first jj characters. Then:

fi,j={max⁡(fi−1,j,fi,j−1),Ai≠Bjfi−1,j−1+1,Ai=Bjf_{i,j}= \begin{cases} \max(f_{i-1,j},f_{i,j-1}) & ,A_i \neq B_j \\ f_{i-1,j-1}+1 & ,A_i = B_j \end{cases}

The time complexity of the above method is O(nm)O(nm), so it cannot pass this problem.

Better Solution

Thinking carefully, we find a property: the final answer will not exceed mm.

Thinking carefully again, we find that LCS has a greedy property.

Redefine fi,jf_{i,j} as the shortest prefix length of AA whose longest common subsequence with the first ii characters of BB has length jj (that is, exchange the answer and the first-dimensional state of the naive method).

By preprocessing, for every position of AA, the positions of the next occurrences of a,b,⋯ ,za,b,\cdots,z, a forward transition can be performed in O(1)O(1).

The complexity is O(m2+26n)O(m^2+26n), so this solution can pass the problem.

Example 2

Problem Statement

Given an unweighted directed graph with nn vertices, determine whether it contains a Hamiltonian cycle. (2≤n≤20)(2\le n\le 20)

Naive Solution

Looking at the data range, we consider state compression.

Let fs,if_{s,i} indicate whether vertex ii can be reached from vertex 11 by visiting only vertices in the set ss. Let gg be the adjacency matrix of the original graph. Then:

fs,i=⋁j∈s,j≠ifs∖{i},j∧gj,i(i∈s)f_{s, i} = \bigvee_{j\in s, j\neq i}f_{s \setminus \{i\}, j}\wedge g_{j, i} \left(i\in s\right)

The time complexity is O(n2×2n)O(n^2 \times 2^n). It may pass if written attractively, but it is not elegant.

Better Solution

In the state design above, each dpdp value represents only one bool value, which seems wasteful.

For each state ss, we can consider compressing fs,1,fs,2,…,fs,nf_{s,1},f_{s,2},\dots,f_{s,n} into one int. We find that after compressing the adjacency matrix in the same way, transitions can be performed in O(1)O(1).

The time complexity is O(n2/w×2n)O(n^2/w\times 2^n), which can pass this problem, where ww is the number of bits in an int.

Example 3

Problem Statement

The standard knapsack problem. nn is the number of items, mm is the knapsack capacity, and vi,wiv_i, w_i are the volume and value of item ii, respectively. 1≤n≤1031 \le n \le 10^3, 1≤m,vi≤10181 \le m, v_i \le \color{red}{10^{18}}, 1≤∑wi≤1031 \le \sum w_i \le 10^3.

Naive Solution

This is a standard knapsack problem.

Define fi,jf_{i, j} as the maximum total value when the first ii items have been selected and the knapsack currently contains capacity jj.

It is easy to obtain fi,j=max⁡(fi−1,j,fi−1,j−vi+wi)f_{i, j} = \max(f_{i - 1, j}, f_{i - 1, j - v_i} + w_i).

vi≤1018v_i \le 10^{18}, so this problem cannot be passed.

Better Solution

Swap the answer and the second dimension of the state. Let fi,jf_{i, j} be the minimum total volume when the first ii items have been selected and the value of the items in the knapsack is jj.

Again, it is easy to obtain fi,j=min⁡(fi−1,j,fi−1,j−wi+vi)f_{i, j} = \min(f_{i - 1, j}, f_{i - 1, j - w_i} + v_i).

Note that after changing the second dimension of the state, the transition must be changed accordingly as well.

The time complexity is O(n∑wi)O(n \sum w_i), so this solution can pass the problem.

This content comes from OI Wiki: State Design Optimization. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.