1
00:00:00,000 --> 00:00:04,320
Two indices can describe two prefixes
rather than a location without

2
00:00:04,400 --> 00:00:05,040
meaning.

3
00:00:05,280 --> 00:00:09,680
Our edit distance example transforms the
invented string G A T

4
00:00:10,080 --> 00:00:12,080
into G C T T.

5
00:00:12,560 --> 00:00:17,440
Insertion, deletion, and substitution each
cost one; leaving an equal

6
00:00:17,520 --> 00:00:19,840
character unchanged costs zero.

7
00:00:20,080 --> 00:00:23,880
The table stores the minimum cost for
every pair of prefixes.

8
00:00:24,080 --> 00:00:28,800
We will define the boundaries, compare the
three possible final steps, and then keep

9
00:00:28,880 --> 00:00:31,280
only the rows needed for the next
calculation.

10
00:00:31,760 --> 00:00:36,680
The result is a global unit-cost distance,
not a local sequence alignment or an

11
00:00:36,720 --> 00:00:37,840
affine gap model.

12
00:00:38,167 --> 00:00:43,127
Define the state at row i and column j as
the minimum cost to transform

13
00:00:43,207 --> 00:00:48,047
the first i source characters into the
first j target characters.

14
00:00:48,087 --> 00:00:52,140
Initialize empty prefixes before filling
interior cells.

15
00:00:52,194 --> 00:00:56,727
An empty source needs one insertion per
target character, and an empty target

16
00:00:56,807 --> 00:00:59,367
needs one deletion per source character.

17
00:00:59,447 --> 00:01:04,327
Fill each cell only after its cell above,
its cell to the left, and its upper-left

18
00:01:04,407 --> 00:01:06,047
diagonal are correct.

19
00:01:06,087 --> 00:01:10,567
A row-by-row traversal from left to right
follows those dependencies.

20
00:01:10,800 --> 00:01:13,360
The empty pair has cost zero.

21
00:01:13,520 --> 00:01:18,320
Across the first row, the source remains
empty while target prefixes grow from

22
00:01:18,400 --> 00:01:22,853
G to G C, G C T, and G C T T.

23
00:01:22,907 --> 00:01:27,120
Their costs are one, two, three, and four
insertions.

24
00:01:27,200 --> 00:01:31,840
Down the first column, the target remains
empty while source prefixes grow from

25
00:01:31,920 --> 00:01:34,613
G to G A and G A T.

26
00:01:34,667 --> 00:01:38,080
Their costs are one, two, and three
deletions.

27
00:01:38,160 --> 00:01:42,107
These boundary answers establish the first
correct dependencies.

28
00:01:42,133 --> 00:01:46,160
Omitting the empty-prefix row or column
usually shifts every later

29
00:01:46,240 --> 00:01:47,200
index.

30
00:01:47,433 --> 00:01:51,833
A deletion removes the final source
character after transforming the preceding

31
00:01:51,913 --> 00:01:55,273
source prefix into the current target
prefix.

32
00:01:55,353 --> 00:01:58,233
Its candidate comes from above plus one.

33
00:01:58,273 --> 00:02:02,473
An insertion adds the final target
character after reaching the preceding

34
00:02:02,553 --> 00:02:06,993
target prefix, so its candidate comes from
the left plus one.

35
00:02:07,033 --> 00:02:11,033
A match or substitution aligns both final
characters and uses the

36
00:02:11,153 --> 00:02:12,153
diagonal.

37
00:02:12,233 --> 00:02:15,486
Add zero when they match and one when they
differ.

38
00:02:15,540 --> 00:02:19,593
The minimum of these candidates covers
every possible final alignment step for

39
00:02:19,713 --> 00:02:21,033
this edit model.

40
00:02:21,300 --> 00:02:23,780
The first G matches G.

41
00:02:23,900 --> 00:02:28,180
Its diagonal candidate has cost zero,
which wins over insertion and

42
00:02:28,260 --> 00:02:29,273
deletion.

43
00:02:29,327 --> 00:02:34,020
The A and C differ in the next selected
cell, so the diagonal candidate adds

44
00:02:34,100 --> 00:02:36,380
one to the preceding cost zero.

45
00:02:36,420 --> 00:02:38,687
That prefix distance is one.

46
00:02:38,713 --> 00:02:43,700
The next T matches T, so the diagonal
carries the cost one forward without

47
00:02:43,780 --> 00:02:44,900
another edit.

48
00:02:44,980 --> 00:02:48,980
These selected cells illustrate the
recurrence, but the complete table also

49
00:02:49,060 --> 00:02:53,700
considers alignments that insert or delete
characters rather than pairing the current

50
00:02:53,780 --> 00:02:54,420
two.

51
00:02:54,667 --> 00:02:57,947
The final target prefix contains an
additional T.

52
00:02:58,347 --> 00:03:02,787
At the bottom-right cell, deletion
proposes four, while insertion and the

53
00:03:02,827 --> 00:03:06,587
diagonal substitution-or-match candidate
both propose two.

54
00:03:06,987 --> 00:03:11,867
The insertion candidate can extend the
alignment that already matches G A T to

55
00:03:11,947 --> 00:03:13,987
G C T at cost one.

56
00:03:14,187 --> 00:03:16,587
Adding the extra T gives cost two.

57
00:03:16,987 --> 00:03:20,827
The complete table confirms that no lower-
cost alignment exists.

58
00:03:21,147 --> 00:03:25,787
A substitution of A with C followed by an
insertion of T achieves that

59
00:03:25,867 --> 00:03:29,227
result, though more than one optimal
alignment can exist.

60
00:03:29,533 --> 00:03:33,693
Repeating one row list creates several
references to the same mutable

61
00:03:33,773 --> 00:03:34,733
object.

62
00:03:34,773 --> 00:03:39,346
An assignment to one apparent row then
changes every alias of that row.

63
00:03:39,400 --> 00:03:43,573
This is an object-ownership error, even
when the recurrence is correct.

64
00:03:43,613 --> 00:03:47,853
A comprehension allocates a new row on
each iteration and gives the table

65
00:03:47,933 --> 00:03:49,480
independent rows.

66
00:03:49,506 --> 00:03:53,453
The written example changes one cell to
nine and compares the resulting

67
00:03:53,533 --> 00:03:54,613
matrices.

68
00:03:54,653 --> 00:03:58,893
Use this same check for grid problems and
other tables built from nested Python

69
00:03:58,973 --> 00:03:59,853
lists.

70
00:04:00,100 --> 00:04:04,073
The previous row provides the above and
diagonal values.

71
00:04:04,127 --> 00:04:08,100
The current row provides the already-
computed value to the left.

72
00:04:08,180 --> 00:04:12,820
Once a complete new row is available, no
older row is needed to compute the next

73
00:04:12,980 --> 00:04:13,820
cost row.

74
00:04:13,860 --> 00:04:18,607
Replace the previous row reference with
the completed current row and repeat.

75
00:04:18,633 --> 00:04:23,380
For the answer-only function, symmetric
unit insertion and deletion costs let us

76
00:04:23,460 --> 00:04:25,647
put the shorter string on the columns.

77
00:04:25,673 --> 00:04:30,580
The displayed rolling example therefore
uses G A T as its column string and G

78
00:04:30,660 --> 00:04:32,900
C T T as its row string.

79
00:04:33,133 --> 00:04:37,613
The full table has m plus one times n plus
one cells, including its

80
00:04:37,693 --> 00:04:38,733
boundaries.

81
00:04:38,853 --> 00:04:42,093
Its time and returned storage follow that
product.

82
00:04:42,173 --> 00:04:47,053
Two retained rows reduce auxiliary storage
to O of the shorter nonempty string

83
00:04:47,213 --> 00:04:50,866
length, while time remains O of m times n.

84
00:04:50,920 --> 00:04:54,613
An edit sequence needs more information
than the final distance.

85
00:04:54,653 --> 00:04:59,453
Keeping only the last rows loses earlier
predecessor choices, so reconstructing

86
00:04:59,533 --> 00:05:02,653
edits needs a separate method or the full
table.

87
00:05:02,733 --> 00:05:06,573
When either string is empty, the answer-
only function returns the other length

88
00:05:06,813 --> 00:05:09,293
directly in constant time and space.

89
00:05:09,533 --> 00:05:11,933
Equal strings need no edits.

90
00:05:12,013 --> 00:05:15,773
An empty string needs one edit per code
point of the other string.

91
00:05:15,813 --> 00:05:20,573
A transposition such as A B to B A is not
a single permitted

92
00:05:20,653 --> 00:05:22,773
edit here and has cost two.

93
00:05:22,813 --> 00:05:27,613
Compare the full-table result with the
two-row result and verify symmetry only

94
00:05:27,693 --> 00:05:30,760
under these equal insertion and deletion
costs.

95
00:05:30,866 --> 00:05:35,693
Python strings expose Unicode code points,
which do not always correspond one-to-one

96
00:05:35,773 --> 00:05:37,400
with visible characters.

97
00:05:37,426 --> 00:05:41,773
The input representation and the allowed
operations belong to the problem contract

98
00:05:41,973 --> 00:05:43,853
before the recurrence begins.

99
00:05:44,100 --> 00:05:48,660
Longest common subsequence also uses two
prefixes, but its state

100
00:05:48,820 --> 00:05:52,420
maximizes retained matches and permits
skipped characters.

101
00:05:52,820 --> 00:05:57,780
A contiguous shared block cannot cross a
mismatch, so its recurrence resets the

102
00:05:57,940 --> 00:05:59,620
current matching length instead.

103
00:05:59,860 --> 00:06:04,660
Before reusing a table pattern, state
exactly what each cell means and which

104
00:06:04,740 --> 00:06:06,340
earlier answers determine it.

105
00:06:06,740 --> 00:06:10,660
The mixed practice page includes a shared-
signal problem with a concealed

106
00:06:10,740 --> 00:06:11,660
explanation.

107
00:06:11,860 --> 00:06:16,340
Attempt it without opening the hint, then
compare its state with the edit-distance

108
00:06:16,420 --> 00:06:17,860
state from this lesson.
