CSC325 · 2081 · 5 marks
Define order statistics problem. Find the edit distance between 'cat' and 'car' using dynamic programming.
Define order statistics problem. Find the edit distance between 'cat' and 'car' using dynamic programming. [5]
Order Statistics Problem and Edit Distance
Part 1: Order Statistics Problem
Definition:
Given a set (or array) $A$ of $n$ elements drawn from a totally ordered set, the $i$-th order statistic is the $i$-th smallest element of $A$.
- Input: An array $A$ of $n$ elements and an integer $i$ where $1 \le i \le n$.
- Output: The element $e \in A$ that is greater than exactly $i-1$ other elements of $A$.
Special cases:
- $i = 1$ → minimum element
- $i = n$ → maximum element
- $i = \lfloor (n+1)/2 \rfloor$ → median
The selection problem is the general problem of finding the $i$-th order statistic, solvable in expected linear time $O(n)$ (randomized-select) or worst-case $O(n)$ (median-of-medians).
Part 2: Edit Distance between 'cat' and 'car'
Given Data
- $X = \text{"cat"}$, length $m = 3$
- $Y = \text{"car"}$, length $n = 3$
- Operation costs: insert = delete = replace = $1$
Recurrence
$$ dp[i][j] = \begin{cases} j & i = 0 \ i & j = 0 \ dp[i-1][j-1] & X[i] = Y[j] \ 1 + \min\big(dp[i-1][j],, dp[i][j-1],, dp[i-1][j-1]\big) & X[i] \neq Y[j] \end{cases} $$
Filling the Table
Initialization: first row = $0,1,2,3$; first column = $0,1,2,3$.
Row i=1 (X='c'):
- $dp[1][1]$: c=c → $dp[0][0]=0$
- $dp[1][2]$: c≠a → $1+\min(2,0,1)=1$
- $dp[1][3]$: c≠r → $1+\min(3,1,2)=2$
Row i=2 (X='a'):
- $dp[2][1]$: a≠c → $1+\min(0,2,1)=1$
- $dp[2][2]$: a=a → $dp[1][1]=0$
- $dp[2][3]$: a≠r → $1+\min(2,0,1)=1$
Row i=3 (X='t'):
- $dp[3][1]$: t≠c → $1+\min(1,3,2)=2$
- $dp[3][2]$: t≠a → $1+\min(0,2,1)=1$
- $dp[3][3]$: t≠r → $1+\min(1,1,0)=1$
Final DP Table
| c | a | r | ||
|---|---|---|---|---|
| 0 | 1 | 2 | 3 | |
| c | 1 | 0 | 1 | 2 |
| a | 2 | 1 | 0 | 1 |
| t | 3 | 2 | 1 | 1 |
Result
$$\boxed{\text{Edit Distance} = dp[3][3] = 1}$$
Interpretation: Only one replace operation is required - substitute 't' with 'r' to turn "cat" into "car".
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.