We are given $2$ strings $A=[1 \dots m]$ and $B[1 \dots n]$ and the following $3$ operations are allowed:
- Insert a charachter,with cost $c_i$
- Delete a character,with cost $c_d$
- Replace a character,with cost $c_r$
We are looking for the optimal sequence of operations(sequence of operations with the minimum cost),for the conversion of the string $A$ to the string $B$.
$$T(i,j)=\text{ the minimum cost of the conversion of the string }\\ A[1 \dots i] \text{ to } B[1 \dots j], 1 \leq i \leq m, 1 \leq j \leq n$$
According to my notes: $$T(i,j)=\min \left\{\begin{matrix} T(i-1,j)+c_d & \text{ // delete the i-th charachter of A}\\ T(i,j-1)+c_i & \text{ // insert the j-th charachter of B} \\ T(i-1,j-1) & \text{if } A[i]=B[j]\\ T(i-1,j-1)+c_r & \text{if } A[i] \neq B[j] \end{matrix}\right.$$
Why is the cost given by the above formula?Could you explain it to me?
EDIT:
For example,suppose that we want to convert the string $ \text{ exponential}$ to the string $\text{ polynomial }$ ,knowing the costs:
$$ c_d=c_i=2 , c_r=1$$
I created the following matrix:

Is it right? If so,is the minimum cost of the conversion then $ T[11,10]=11$ ? Or am I wrong?