rmykkänen

notes

Longest sequences

date

These are a type of dynamic programming problem where we're looking for common sequences of subelements within two or more elements. Most commonly, chars within a string.

Longest common substring

Given two strings a1a_1, a2a_2, return the largest substring present in both.

Solution

We'll first solve the longest common suffix problem. Let a1,a2a_1, a_2 be any strings with a1y,a2ya_1y, a_2y having the longest common suffix yy. Now, what is the longest common substring of a1yb1,a2yb2a_1yb_1, a_2yb_2?

  • If b1=b2b_1 = b_2, then yb1=yb2yb_1 = yb_2 is the longest common suffix.
  • Else, yy is the longest common substring, they have no common suffix. Let's define the elements of our table.
  • two-dimensional table TT, ii is index of first string, jj index of second
T[i,j]={T[i−1,j−1]+1if xi=yj0,else\begin{equation} T[i,j] = \begin{cases} T[i-1, j-1] + 1 & \text{if } x_i = y_j \\ 0, & \text{else} \end{cases} \end{equation}

Pseudocode

def lcsubstring(x, y):
	dp = [0; len(x) + 1][0; len(y) + 1]
	maximum = 0
	maxpos = (0, 0)

	for i in 1..(len(x) + 1):
		for j in 1..(len(y) + 1):
			if x[i-1] = y[j-1]:
				dp[i, j] = dp[i - 1, j - 1] + 1
			else
				dp[i, j] = 0
			if maximum < dp[i, j]:
				maximum = dp[i, j]
				maxpos = i, j

Considering the problem in terms of suffixes is easier than in substrings. The table stores suffix lengths, and the longest suffix of a prefix is the longest substring.

Longest common subsequence

Take the example x=x = "abcbdab", yy = "bdcaba". Subsequences are not contiguous, so for the example, subsequences "bcba" and "bdab" are valid. Let X=x1,..,xmX = x_1,..,x_m, Y=y1,..,ynY = y_1, .., y_n, Z=z1,..,zkZ = z_1, .., z_k where ZZ is the longest common subsequence of XX and YY. Then if xm=ynx_m = y_n, zk=xm=ynz_k = x_m = y_n and z1,..,zk−1z_1, .., z_{k-1} is the longest common subsequence. If xm≠ynx_m \neq y_n, then Zk≠XmZ_k \neq X_m or Zk≠YnZ_k \neq Y_n and ZZ is still the longest common subsequence.

Solution

Let's define our recurrence T[i,j]T[i,j] as the longest common subsequence of x1,..,xix_1,..,x_i and y1,..,yjy_1,..,y_j. If either of ii or jj is zero, we want zero because there is no possible subsequence. If two letters are equal, we want to add that character so we take the LCS of both strings without that character and add one for that character. Otherwise, the LCS is the max of the LCSs with one less character.

T[i,j]={0if i=0 or j=0T[i−1,j−1]+1if xi=yjmax(T[i−1,j],T[i,j−1])if xi≠yj\begin{equation} T[i,j] = \begin{cases} 0 & \text{if } i = 0 \text{ or } j = 0 \\ T[i-1, j-1] + 1 & \text{if } x_i = y_j \\ max(T[i-1, j], T[i, j-1]) & \text{if } x_i \neq y_j \end{cases} \end{equation}

We take the max since if both letters are different, it may increase a previous LCS in one, but not both.

Pseudocode

def lcs(x, y):
	dp = [0; len(x)+1][0; len(y)+1]

	for i in 1..(len(x) + 1):
		for j in 1..(len(y) + 1):
			if x[i-1] = y[j-1]:
				dp[i, j] = dp[i-1, j-1] + 1
			else:
				dp[i, j] = max(dp[i-1, j], dp[i, j-1])

	return dp[len(x), len(y)]

The runtime here is n×mn \times m because of the table, where n=∣x∣n = |x| and m=∣y∣m = |y|.

Longest palindromic subsequence

Suppose you have as input one string x=a1,..,anx = a_1, .., a_n and we want to find the longest palindromic subsequence. This is the same as lcs(a,a.reverse())lcs(a, a.reverse()).

Solution

We can construct the dp array dp[n][n]dp[n][n] with dp[i][j]dp[i][j] = LPS from ai,..,aja_i,..,a_j. We have the base cases where empty strings and single chars are palindromic, so ∀i dp[i][i]=1\forall i \text{ } dp[i][i] = 1. If ai,..,aja_i,..,a_j has the fact that ai=aja_i = a_j, then it could be the ends of a palindrome, depending on ai(ai+1,..,aj−1)aja_i(a_{i+1}, .., a_{j-1})a_j, so we obtain the recurrence:

T[i,j]={2+T[i+1,j−1]if xi=xjmax(T[i+1,j],T[i,j−1])if xi≠xj\begin{equation} T[i,j] = \begin{cases} 2 + T[i + 1, j - 1] & \text{if } x_i = x_j \\ max(T[i+1, j], T[i, j-1]) & \text{if } x_i \neq x_j \end{cases} \end{equation}

Pseudocode

def lps(x):
	dp = [0; |x|][0; |x|]

	for i in 1..|x|:
		dp[i][i] = 1

	for s in range 1..|x|:
		for i in range |x| - s:
			j = i+s
			if x[i] = x[j]:
				dp[i][j] = 2 + dp[i+1][j-1]
			else:
				dp[i][j] = max(dp[i+1][j], dp[i][j-1])

The runtime is O(n2)O(n^2) because it's a two-dimensional n×nn \times n table.