DSADP2D

Longest Common Subsequence Visualizer

What is LCS

A subsequence keeps order but may skip letters. dp[i][j] is the LCS length of A[:i] and B[:j]. Equal letters take the diagonal plus one; otherwise take the better of left and up.

  • Match: dp[i][j] = dp[i-1][j-1] + 1
  • Mismatch: max(dp[i-1][j], dp[i][j-1])
  • First row/col are 0 — empty prefix has no letters in common.

Brute force approach

If heads match, take both and advance. Else try skipping A or skipping B. The same (i, j) is reached many ways.

Match the current letters and move both pointers, or skip a letter from one string. The pair of indexes repeats. Memoization stores LCS(i, j).

Try a small input

Each string is at most 4 letters. ABC and AC share length 2.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def lcs(i, j):
if i or j is past the end: return 0
match or max(skip A, skip B)
Step 0

Memoized approach

A 2D table: match → diagonal + 1, else max(up, left). Time O(mn).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def lcs(i, j, memo):
if (i, j) in memo: return memo
if past the end: return 0
memo = match or max(skip)
return memo
Step 0

How this visualizer works

A = ABC, B = AEB. The table ends at 2 (AB). Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def lcs(a, b):
2m, n = len(a), len(b)
3dp = [[0]*(n+1) for _ in range(m+1)]
4for i in range(1, m+1):
5for j in range(1, n+1):
6if a[i-1]==b[j-1]: dp[i][j] = dp[i-1][j-1]+1
7else: dp[i][j] = max(dp[i-1][j], dp[i][j-1])
8return dp[m][n]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def lcs(a, b):
2m, n = len(a), len(b)
3dp = [[0]*(n+1) for _ in range(m+1)]
4for i in range(1, m+1):
5for j in range(1, n+1):
6if a[i-1]==b[j-1]: dp[i][j] = dp[i-1][j-1]+1
7else: dp[i][j] = max(dp[i-1][j], dp[i][j-1])
8return dp[m][n]

complexity.md

OperationTime
Each cellO(1)
Fill tableO(mn)
SpaceO(mn)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def lcs_brute(a, b, i=0, j=0):
2if i == len(a) or j == len(b):
3return 0
4if a[i] == b[j]:
5return 1 + lcs_brute(a, b, i + 1, j + 1)
6skip_a = lcs_brute(a, b, i + 1, j)
7skip_b = lcs_brute(a, b, i, j + 1)
8return max(skip_a, skip_b)
1def lcs_dp(a, b):
2m = len(a)
3n = len(b)
4dp = [[0] * (n + 1) for _ in range(m + 1)]
5for i in range(1, m + 1):
6for j in range(1, n + 1):
7if a[i - 1] == b[j - 1]:
8dp[i][j] = dp[i - 1][j - 1] + 1
9else:
10dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
11return dp[m][n]
1int lcs_brute(const char *a, const char *b, int i, int j) {
2int skip_a;
3int skip_b;
4if (a[i] == 0 || b[j] == 0) {
5return 0;
6}
7if (a[i] == b[j]) {
8return 1 + lcs_brute(a, b, i + 1, j + 1);
9}
10skip_a = lcs_brute(a, b, i + 1, j);
11skip_b = lcs_brute(a, b, i, j + 1);
12return skip_a > skip_b ? skip_a : skip_b;
13}
1int lcs_dp(const char *a, const char *b) {
2int m = 0;
3int n = 0;
4int i;
5int j;
6int dp[201][201];
7while (a[m] != 0) {
8m++;
9}
10while (b[n] != 0) {
11n++;
12}
13for (i = 0; i <= m; i++) {
14for (j = 0; j <= n; j++) {
15dp[i][j] = 0;
16}
17}
18for (i = 1; i <= m; i++) {
19for (j = 1; j <= n; j++) {
20if (a[i - 1] == b[j - 1]) {
21dp[i][j] = dp[i - 1][j - 1] + 1;
22} else if (dp[i - 1][j] > dp[i][j - 1]) {
23dp[i][j] = dp[i - 1][j];
24} else {
25dp[i][j] = dp[i][j - 1];
26}
27}
28}
29return dp[m][n];
30}