ICS 582Lecture 02المحاضرة 02Part 11الجزء 11
Filling the DP table and recovering the alignmentملء جدول DP واستعادة المحاذاة
The full algorithm, a cell by cell computation for intention to execution, backpointers and backtrace to read off the alignment, the time and space complexity, and the lecture in one page.الخوارزمية كاملة، وحساب خلية بخلية لتحويل intention إلى execution، والمؤشرات الخلفية (backpointers) والتتبع العكسي (backtrace) لقراءة المحاذاة، وتعقيد الزمن والذاكرة، وملخص المحاضرة في صفحة واحدة.
- Conceptsالمفاهيم
- 5
- Slidesالشرائح
- 87-94
- Readingمدة القراءة
- 30 min30 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
The edit distance table is the most examinable algorithm in this lecture. SLP3 sets it as a by-hand exercise, and every spelling, transliteration or speech evaluation you will run in your research is this table applied to characters or words: word error rate is literally an edit distance divided by a length.
Part 10 defined the problem and the recurrence. This part makes the algorithm run. You will fill the 10 x 10 table for intention to execution cell by cell, see why the slide's table ends in 8 while the unit-cost answer is 5, store arrows as you go, and then walk those arrows home to read an alignment off the table. The same fill-then-backtrace pattern returns later in the course as Viterbi decoding and CKY parsing, so the effort pays three times.
جدول مسافة التعديل (edit distance) هو أكثر خوارزمية قابلة لأن تأتي في الامتحان من هذه المحاضرة. يضعه كتاب SLP3 تمرينًا يُحل يدويًا، وكل تقييم للتهجئة أو للنقل الحرفي (transliteration) أو للتعرف على الكلام ستجريه في بحثك هو هذا الجدول نفسه مطبقًا على الحروف أو الكلمات: فمعدل خطأ الكلمات (word error rate) هو حرفيًا مسافة تعديل مقسومة على طول.
عرّف الجزء 10 المسألة وعلاقة التكرار (recurrence). أما هذا الجزء فيجعل الخوارزمية تعمل فعلًا. ستملأ الجدول 10 x 10 لتحويل intention إلى execution خلية خلية، وسترى لماذا ينتهي جدول الشريحة بالقيمة 8 بينما الإجابة بالكلف الأحادية (unit cost) هي 5، وستخزن الأسهم أثناء الملء، ثم تتبع تلك الأسهم رجوعًا إلى البداية لتقرأ محاذاة (alignment) من الجدول. ونمط «املأ ثم تتبع رجوعًا» نفسه يعود لاحقًا في المقرر في فك ترميز Viterbi وفي التحليل النحوي CKY، فالجهد الذي تبذله الآن يؤتي ثماره ثلاث مرات.
By the end you canفي نهاية هذا الجزء ستستطيع
- Initialize and fill an edit distance table by hand under a stated cost convention and read the distance from D[n,m].تهيئة جدول مسافة التعديل (edit distance) وملؤه يدويًا وفق اصطلاح كلفة محدد، ثم قراءة المسافة من D[n,m].
- Store argmin arrows while filling and backtrace them into an alignment with operation letters.تخزين أسهم الـ argmin أثناء الملء، ثم تتبعها رجوعًا (backtrace) للحصول على محاذاة (alignment) مع حروف العمليات.
- Explain what a tie in the argmin means for the number of optimal alignments.شرح ما يعنيه التعادل في الـ argmin بالنسبة لعدد المحاذاات المثلى.
- State time and space complexity, and how two rows or Hirschberg's recursion reduce space.ذكر التعقيد الزمني والمكاني، وكيف يقلل استخدام صفين أو عودية Hirschberg من الذاكرة المطلوبة.
- Explain why slide 89 ends in 8 while unit costs give 5, and pass an exam under either convention.شرح لماذا ينتهي جدول الشريحة 89 بالقيمة 8 بينما تعطي الكلف الأحادية 5، والإجابة الصحيحة في الامتحان وفق أي من الاصطلاحين.
Start with a route you can check by eye. Take intention, delete the i to get ntention, replace n by e to get etention, replace t by x to get exention, insert u to get exenution, and replace n by c to get execution. Five edits, each of cost 1, and the string arrives exactly where it should.
ابدأ بمسار يمكنك التحقق منه بعينك. خذ intention، واحذف الحرف i لتحصل على ntention، ثم استبدل n بـ e لتحصل على etention، ثم استبدل t بـ x لتحصل على exention، ثم أدرج u لتحصل على exenution، ثم استبدل n بـ c لتحصل على execution. خمسة تعديلات، كلفة كل منها 1، وتصل السلسلة تمامًا إلى حيث يجب أن تصل.
- intention, delete i, gives ntentionintention، احذف i، فتحصل على ntention
- ntention, substitute n by e, gives etentionntention، استبدل n بـ e، فتحصل على etention
- etention, substitute t by x, gives exentionetention، استبدل t بـ x، فتحصل على exention
- exention, insert u, gives exenutionexention، أدرج u، فتحصل على exenution
- exenution, substitute n by c, gives executionexenution، استبدل n بـ c، فتحصل على execution
SLP3 states that the unit-cost Minimum edit distance between these two words is exactly 5, so this ladder is not merely a route, it is an optimal one. The interesting question is what optimality implies about the middle rungs. Suppose exention really lies on a shortest path. Then the way we reached exention must itself be the shortest way to reach exention: if a cheaper route to exention existed, we could splice it in and get a cheaper route to execution, which contradicts the assumption that our path was shortest. Every prefix of a shortest path is a shortest path. That property is called Optimal substructure, and it is the license for Dynamic programming: instead of remembering whole edit sequences, remember the best cost for every intermediate state once, and build longer answers out of shorter ones.
ينص SLP3 على أن minimum edit distance بالكلف الأحادية بين هاتين الكلمتين تساوي 5 بالضبط، لذا فهذا السلّم ليس مجرد مسار، بل مسار أمثل. والسؤال المثير هو: ماذا تعني الأمثلية بالنسبة للدرجات الوسطى؟ لنفترض أن exention تقع فعلًا على مسار أقصر. عندئذ يجب أن تكون الطريقة التي وصلنا بها إلى exention هي نفسها أقصر طريق للوصول إلى exention: فلو وُجد طريق أرخص إلى exention لأمكننا تركيبه مكان الجزء الأول والحصول على طريق أرخص إلى execution، وهذا يناقض افتراض أن مسارنا هو الأقصر. إذن كل بادئة (prefix) من مسار أقصر هي نفسها مسار أقصر. تسمى هذه الخاصية optimal substructure (البنية الجزئية المثلى)، وهي المسوّغ لاستخدام dynamic programming (البرمجة الديناميكية): فبدلًا من تذكر تسلسلات تعديل كاملة، نتذكر أفضل كلفة لكل حالة وسيطة مرة واحدة، ونبني الإجابات الأطول من الإجابات الأقصر.
The states themselves are what part 10 introduced: a pair of prefixes, the first i letters of the source and the first j letters of the target. The naive search over edit sequences explodes because thousands of different sequences pass through the same intermediate string, and each one is scored again from scratch. A table indexed by prefix pairs collapses all of those into one cell, and the Alignment you read from the table at the end is just the ladder above, laid out as a path through cells (SLP3, section 2.9.1).
أما الحالات نفسها فهي ما قدّمه الجزء 10: زوج من البادئات، أي أول i حرفًا من المصدر وأول j حرفًا من الهدف. البحث الساذج في تسلسلات التعديل ينفجر حجمه لأن آلاف التسلسلات المختلفة تمر بالسلسلة الوسيطة نفسها، وكل واحد منها يُحسب من الصفر مرة أخرى. أما الجدول المفهرس بأزواج البادئات فيدمج كل ذلك في خلية واحدة، والـ alignment (المحاذاة) التي تقرؤها من الجدول في النهاية ما هي إلا السلّم السابق نفسه، مرسومًا كمسار عبر الخلايا (SLP3، القسم 2.9.1).
Recall
Why does the fact that exention lies on an optimal path from intention to execution tell you anything about the path from intention to exention?لماذا يخبرك كون exention واقعة على مسار أمثل من intention إلى execution بشيء عن المسار من intention إلى exention؟
Compute the top-left corner of the intention to execution table by hand, using the slide's convention where a substitution costs 2. The empty string needs nothing to become the empty string, so D[0,0] = 0. Turning the one-letter prefix i into nothing takes one deletion, D[1,0] = 1. Building e from nothing takes one insertion, D[0,1] = 1. The first interior cell compares i with e: D[1,1] = min(1 + 1, 1 + 1, 0 + 2) = 2, and all three candidates tie.
احسب الزاوية العليا اليسرى من جدول intention إلى execution يدويًا، باستخدام اصطلاح الشريحة الذي تكون فيه كلفة الاستبدال 2. السلسلة الفارغة لا تحتاج إلى شيء لتصبح سلسلة فارغة، لذا D[0,0] = 0. وتحويل البادئة المكونة من حرف واحد i إلى لا شيء يتطلب حذفًا واحدًا، D[1,0] = 1. وبناء e من لا شيء يتطلب إدراجًا واحدًا، D[0,1] = 1. أول خلية داخلية تقارن i مع e: D[1,1] = min(1 + 1, 1 + 1, 0 + 2) = 2، والمرشحون الثلاثة متعادلون.
That small calculation is the whole algorithm, repeated. SLP3's pseudocode (its Fig. 2.21, reproduced on slide 88) makes it precise. Let n be the length of the source and m the length of the target, and create a matrix D with n + 1 rows and m + 1 columns, the extra row and column standing for the empty prefix. The first column is D[i,0] = D[i-1,0] + del-cost(source[i]), because the only way to turn a length-i prefix into nothing is to delete each of its letters. The first row is D[0,j] = D[0,j-1] + ins-cost(target[j]), because the only way to build a length-j prefix from nothing is to insert each letter. With unit insertion and deletion those two lines reduce to D[i,0] = i and D[0,j] = j. Then, row by row and column by column, every interior cell is the minimum of three candidates, and the answer is D[n,m].
هذا الحساب الصغير هو الخوارزمية كلها، مكررة. والشيفرة الوصفية (pseudocode) في SLP3 (الشكل 2.21 فيه، والمعروض في الشريحة 88) تجعل ذلك دقيقًا. ليكن n طول المصدر وm طول الهدف، وأنشئ مصفوفة D فيها n + 1 صفًا وm + 1 عمودًا، حيث يمثل الصف والعمود الإضافيان البادئة الفارغة. العمود الأول هو D[i,0] = D[i-1,0] + del-cost(source[i])، لأن الطريقة الوحيدة لتحويل بادئة طولها i إلى لا شيء هي حذف كل حرف من حروفها. والصف الأول هو D[0,j] = D[0,j-1] + ins-cost(target[j])، لأن الطريقة الوحيدة لبناء بادئة طولها j من لا شيء هي إدراج كل حرف. ومع كلفة إدراج وحذف أحادية يتبسط هذان السطران إلى D[i,0] = i وD[0,j] = j. ثم، صفًا بعد صف وعمودًا بعد عمود، تكون كل خلية داخلية القيمة الصغرى من ثلاثة مرشحين، والإجابة هي D[n,m].
The Edit distance recurrence looks only at three neighbors: the cell above (delete the source letter source[i], written x_i in the formula), the cell to the left (insert the target letter target[j], written y_j), and the diagonal cell (substitute one for the other, or match for free when they are equal). Nothing else in the two strings is consulted. That locality is what makes the fill order work: going row by row from the top-left, all three neighbors are already computed when you reach a cell. Any order with that property is fine, including column by column or along anti-diagonals, which is how the hover figure below animates it.
تنظر edit distance recurrence (علاقة التكرار لمسافة التعديل) إلى ثلاثة جيران فقط: الخلية العليا (حذف حرف المصدر source[i]، المكتوب x_i في الصيغة)، والخلية اليسرى (إدراج حرف الهدف target[j]، المكتوب y_j)، والخلية القطرية (استبدال أحدهما بالآخر، أو التطابق مجانًا عندما يتساويان). ولا يُرجع إلى أي شيء آخر في السلسلتين. هذه المحلية هي ما يجعل ترتيب الملء صحيحًا: عند السير صفًا بصف من الزاوية العليا اليسرى، تكون الجيران الثلاثة محسوبة مسبقًا عندما تصل إلى أي خلية. وأي ترتيب يحقق هذه الخاصية مقبول، بما في ذلك عمودًا بعمود أو على طول الأقطار المعاكسة (anti-diagonals)، وهي الطريقة التي يحرّك بها الشكل التفاعلي أدناه الملء.
Two conventions, two right answersاصطلاحان، وإجابتان صحيحتان
Slide 86 defined a unit Substitution cost, but the table on slide 89 is SLP3's Fig. 2.20, which charges 2 for a substitution. SLP3 explains the choice: a substitution can always be simulated by one deletion and one insertion, so charging 2 makes the metric coincide with the version of Levenshtein's distance that allows only insertions and deletions. Under that convention intention to execution costs 8; under unit costs it is 5, the length of the ladder from the previous concept. Both numbers are correct in their own world, and an exam answer is correct only if it names the world it lives in.
عرّفت الشريحة 86 substitution cost (كلفة الاستبدال) بأنها أحادية، لكن الجدول في الشريحة 89 هو الشكل 2.20 من SLP3، الذي يحتسب 2 للاستبدال. ويشرح SLP3 هذا الاختيار: يمكن دائمًا محاكاة الاستبدال بحذف واحد وإدراج واحد، لذا فإن احتساب 2 يجعل المقياس مطابقًا لنسخة Levenshtein distance التي لا تسمح إلا بالإدراج والحذف. وفق هذا الاصطلاح يكلف تحويل intention إلى execution القيمة 8؛ أما بالكلف الأحادية فهو 5، أي طول السلّم من المفهوم السابق. كلا الرقمين صحيح في عالمه، وإجابة الامتحان لا تكون صحيحة إلا إذا سمّت العالم الذي تنتمي إليه.
| Conventionالاصطلاح | Insertالإدراج | Deleteالحذف | Substituteالاستبدال | Matchالتطابق | Distanceالمسافة | Optimal alignmentsالمحاذاات المثلى |
|---|---|---|---|---|---|---|
| Unit (Levenshtein)أحادية (Levenshtein) | 11 | 11 | 11 | 00 | 55 | 77 |
| SLP3 table (slide 89)جدول SLP3 (الشريحة 89) | 11 | 11 | 22 | 00 | 88 | 134134 |
Here is the full table from the slide with the source intention down the rows and the target execution across the columns, followed by the same table under unit costs. Compare any substitution cell: D[1,1] is 2 in the first and 1 in the second, and that one-point gap propagates until the corners read 8 and 5.
إليك الجدول الكامل من الشريحة، والمصدر intention على امتداد الصفوف والهدف execution على امتداد الأعمدة، ويليه الجدول نفسه بالكلف الأحادية. قارن أي خلية استبدال: D[1,1] تساوي 2 في الأول و1 في الثاني، وهذا الفرق بنقطة واحدة ينتشر حتى تصبح قيمتا الزاويتين 8 و5.
| # | e | x | e | c | u | t | i | o | n | |
|---|---|---|---|---|---|---|---|---|---|---|
| # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 6 | 7 | 8 |
| n | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 7 | 8 | 7 |
| t | 3 | 4 | 5 | 6 | 7 | 8 | 7 | 8 | 9 | 8 |
| e | 4 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 9 |
| n | 5 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 10 |
| t | 6 | 5 | 6 | 7 | 8 | 9 | 8 | 9 | 10 | 11 |
| i | 7 | 6 | 7 | 8 | 9 | 10 | 9 | 8 | 9 | 10 |
| o | 8 | 7 | 8 | 9 | 10 | 11 | 10 | 9 | 8 | 9 |
| n | 9 | 8 | 9 | 10 | 11 | 12 | 11 | 10 | 9 | 8 |
| # | e | x | e | c | u | t | i | o | n | |
|---|---|---|---|---|---|---|---|---|---|---|
| # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| i | 1 | 1 | 2 | 3 | 4 | 5 | 6 | 6 | 7 | 8 |
| n | 2 | 2 | 2 | 3 | 4 | 5 | 6 | 7 | 7 | 7 |
| t | 3 | 3 | 3 | 3 | 4 | 5 | 5 | 6 | 7 | 8 |
| e | 4 | 3 | 4 | 3 | 4 | 5 | 6 | 6 | 7 | 8 |
| n | 5 | 4 | 4 | 4 | 4 | 5 | 6 | 7 | 7 | 7 |
| t | 6 | 5 | 5 | 5 | 5 | 5 | 5 | 6 | 7 | 8 |
| i | 7 | 6 | 6 | 6 | 6 | 6 | 6 | 5 | 6 | 7 |
| o | 8 | 7 | 7 | 7 | 7 | 7 | 7 | 6 | 5 | 6 |
| n | 9 | 8 | 8 | 8 | 8 | 8 | 8 | 7 | 6 | 5 |
Worked exampleمثال محلول
Three cells of the slide's table, substitution cost 2ثلاث خلايا من جدول الشريحة، بكلفة استبدال 2
D[1,1], i versus eD[1,1]، الحرف i مقابل e
Up gives D[0,1] + 1 = 2, left gives D[1,0] + 1 = 2, diagonal gives D[0,0] + 2 = 2. All three tie, so D[1,1] = 2 and the cell stores three arrows.الخلية العليا تعطي D[0,1] + 1 = 2، واليسرى تعطي D[1,0] + 1 = 2، والقطرية تعطي D[0,0] + 2 = 2. الثلاثة متعادلة، لذا D[1,1] = 2 وتخزن الخلية ثلاثة أسهم.D[4,2], e versus xD[4,2]، الحرف e مقابل x
Up gives D[3,2] + 1 = 6, left gives D[4,1] + 1 = 4, diagonal gives D[3,1] + 2 = 6. Left wins alone, so D[4,2] = 4 with a single left arrow.الخلية العليا تعطي D[3,2] + 1 = 6، واليسرى تعطي D[4,1] + 1 = 4، والقطرية تعطي D[3,1] + 2 = 6. اليسرى تفوز وحدها، لذا D[4,2] = 4 مع سهم واحد إلى اليسار.D[7,7], i versus iD[7,7]، الحرف i مقابل i
The letters match, so the diagonal is free: D[6,6] + 0 = 8. Up and left would give 9 + 1 and 9 + 1, so D[7,7] = 8 with one diagonal arrow.الحرفان متطابقان، لذا القطر مجاني: D[6,6] + 0 = 8. والعليا واليسرى كانتا ستعطيان 9 + 1 و9 + 1، لذا D[7,7] = 8 مع سهم قطري واحد.Resultالنتيجة
Continuing to the corner, D[9,9] = 8. Under unit costs the same three cells read 1, 4 and 5, and the corner reads 5.بالاستمرار حتى الزاوية، D[9,9] = 8. وبالكلف الأحادية تكون قيم الخلايا الثلاث نفسها 1 و4 و5، وتكون قيمة الزاوية 5.
Worked exampleمثال محلول
A table you can finish in a minute: cat to cut, unit costsجدول تنهيه في دقيقة: من cat إلى cut بالكلف الأحادية
Bordersالحواف
Row # is 0 1 2 3 and column # is 0 1 2 3.الصف # هو 0 1 2 3 والعمود # هو 0 1 2 3.Row cالصف c
c matches c, so D[1,1] = 0; then D[1,2] = 1 and D[1,3] = 2 by inserting u and t.c يطابق c، لذا D[1,1] = 0؛ ثم D[1,2] = 1 وD[1,3] = 2 بإدراج u وt.Row aالصف a
D[2,1] = 1 (delete a), D[2,2] = min(1 + 1, 0 + 1, 1 + 1) = 1 (substitute a by u), D[2,3] = 2.D[2,1] = 1 (حذف a)، وD[2,2] = min(1 + 1, 0 + 1, 1 + 1) = 1 (استبدال a بـ u)، وD[2,3] = 2.Row tالصف t
D[3,1] = 2, D[3,2] = 2, and t matches t so D[3,3] = D[2,2] + 0 = 1.D[3,1] = 2، وD[3,2] = 2، وt يطابق t لذا D[3,3] = D[2,2] + 0 = 1.Resultالنتيجة
Distance 1, alignment c a t over c u t with one substitution.المسافة 1، والمحاذاة c a t فوق c u t مع استبدال واحد.
| # | c | u | t | |
|---|---|---|---|---|
| # | 0 | 1 | 2 | 3 |
| c | 1 | 0 | 1 | 2 |
| a | 2 | 1 | 1 | 2 |
| t | 3 | 2 | 2 | 1 |
Now run the same procedure yourself. The simulator fills any pair of short words one cell at a time, prints the three candidates for each new cell, stores the arrows, and keeps both conventions side by side so you can watch the corner change from 5 to 8 with one toggle. The backtrace button belongs to the next concept; try it after reading on.
الآن نفّذ الإجراء نفسه بنفسك. يملأ المحاكي أي زوج من الكلمات القصيرة خلية في كل مرة، ويطبع المرشحين الثلاثة لكل خلية جديدة، ويخزن الأسهم، ويعرض الاصطلاحين جنبًا إلى جنب حتى ترى قيمة الزاوية تتغير من 5 إلى 8 بنقرة واحدة. أما زر التتبع الرجوعي (Backtrace) فيخص المفهوم التالي؛ جرّبه بعد أن تكمل القراءة.
| # | e | x | e | c | u | t | i | o | n | |
|---|---|---|---|---|---|---|---|---|---|---|
| # | ||||||||||
| i | ||||||||||
| n | ||||||||||
| t | ||||||||||
| e | ||||||||||
| n | ||||||||||
| t | ||||||||||
| i | ||||||||||
| o | ||||||||||
| n |
D[0,0] = 0: the empty string is already the empty string.D[0,0] = 0: السلسلة الفارغة هي أصلًا السلسلة الفارغة.
| Conventionالاصطلاح | Substitution costكلفة الاستبدال | Distanceالمسافة | Optimal alignmentsالمحاذاات المثلى |
|---|---|---|---|
| Unit (Levenshtein)موحدة (Levenshtein) | 11 | 55 | 77 |
| SLP3 table (slide 89) (shown)جدول SLP3 (الشريحة 89) (المعروض) | 22 | 88 | 134134 |
Quick checkاختبار سريع
In the recurrence, which candidate corresponds to deleting the source letter source[i]?في علاقة التكرار، أي مرشح يقابل حذف الحرف المصدر source[i]؟
Quick checkاختبار سريع
Slide 89 ends at 8 for intention to execution, but unit costs give 5. What explains the difference?ينتهي جدول الشريحة 89 بالقيمة 8 لتحويل intention إلى execution، بينما تعطي الكلف الأحادية 5. ما الذي يفسر الفرق؟
Recall
Why is the first column initialized to i and the first row to j?لماذا يُهيّأ العمود الأول بالقيمة i والصف الأول بالقيمة j؟
Recall
Which three cells does D[i,j] depend on, and what operation does each represent?على أي ثلاث خلايا تعتمد D[i,j]، وما العملية التي تمثلها كل منها؟
Put your finger on the 8 in the bottom-right corner of slide 91. That cell holds a single diagonal arrow, because n matches n and the free diagonal was the only candidate that reached 8. Step diagonally to (8,8), still 8, o matches o. Diagonal again to (7,7) for i, and to (6,6) for t. At (5,5) three arrows are drawn; the bold path takes the diagonal, substituting n by u for 2, which is why the value drops from 8 to 6 at (4,4). That cell has only a left arrow: insert c. Then (4,3) matches e for free, (3,2) substitutes t by x, (2,1) substitutes n by e, and (1,0) points up, deleting i, into (0,0).
ضع إصبعك على القيمة 8 في الزاوية السفلى اليمنى من الشريحة 91. تحمل هذه الخلية سهمًا قطريًا واحدًا، لأن n يطابق n والقطر المجاني هو المرشح الوحيد الذي وصل إلى 8. انتقل قطريًا إلى (8,8)، وما زالت القيمة 8، وo يطابق o. ثم قطريًا مرة أخرى إلى (7,7) للحرف i، وإلى (6,6) للحرف t. عند (5,5) رُسمت ثلاثة أسهم؛ والمسار العريض يأخذ القطر، مستبدلًا n بـ u بكلفة 2، ولهذا تنخفض القيمة من 8 إلى 6 عند (4,4). لهذه الخلية سهم واحد فقط إلى اليسار: إدراج c. ثم (4,3) تطابق e مجانًا، و(3,2) تستبدل t بـ x، و(2,1) تستبدل n بـ e، و(1,0) تشير إلى الأعلى، حاذفةً i، وصولًا إلى (0,0).
| Cellالخلية | Valueالقيمة | Arrow takenالسهم المتبع | Meaningالمعنى |
|---|---|---|---|
| (9,9)(9,9) | 88 | diagonalقطري | n matches n, cost 0الحرف n يطابق n، الكلفة 0 |
| (8,8)(8,8) | 88 | diagonalقطري | o matches o, cost 0الحرف o يطابق o، الكلفة 0 |
| (7,7)(7,7) | 88 | diagonalقطري | i matches i, cost 0الحرف i يطابق i، الكلفة 0 |
| (6,6)(6,6) | 88 | diagonalقطري | t matches t, cost 0الحرف t يطابق t، الكلفة 0 |
| (5,5)(5,5) | 88 | diagonalقطري | n to u, cost 2 (one of three arrows)استبدال n بـ u، الكلفة 2 (أحد ثلاثة أسهم) |
| (4,4)(4,4) | 66 | leftيسار | insert c, cost 1إدراج c، الكلفة 1 |
| (4,3)(4,3) | 55 | diagonalقطري | e matches e, cost 0 (one of two arrows)الحرف e يطابق e، الكلفة 0 (أحد سهمين) |
| (3,2)(3,2) | 55 | diagonalقطري | t to x, cost 2 (one of three arrows)استبدال t بـ x، الكلفة 2 (أحد ثلاثة أسهم) |
| (2,1)(2,1) | 33 | diagonalقطري | n to e, cost 2 (one of three arrows)استبدال n بـ e، الكلفة 2 (أحد ثلاثة أسهم) |
| (1,0)(1,0) | 11 | upأعلى | delete i, cost 1حذف i، الكلفة 1 |
| (0,0)(0,0) | 00 | stopتوقف | origin reachedالوصول إلى نقطة البداية |
Reverse the list and you have an edit sequence: delete i, n to e, t to x, match e, insert c, n to u, then four matches. Its cost is 1 + 2 + 2 + 0 + 1 + 2 + 0 + 0 + 0 + 0 = 8, the corner value, and written as rows it is SLP3's Fig. 2.17: INTE*NTION over *EXECUTION with the operation letters d s s i s under the changed columns. This is a different five-edit shape from the ladder in the first concept, yet it costs the same under both conventions, 8 here and 5 at unit costs: the ladder substitutes n by c and then inserts u, the bold path inserts c and then substitutes n by u, and both keep the four free matches at the end. The two diverge exactly at the three-arrow tie in (5,5): the left arrow leads to the ladder, the diagonal to the bold path. That is the tie rule of the next subsection in action.
اعكس القائمة فتحصل على تسلسل تعديل: حذف i، وn إلى e، وt إلى x، ومطابقة e، وإدراج c، وn إلى u، ثم أربع مطابقات. كلفته 1 + 2 + 2 + 0 + 1 + 2 + 0 + 0 + 0 + 0 = 8، أي قيمة الزاوية، وإذا كُتب على شكل صفوف فهو الشكل 2.17 من SLP3: INTE*NTION فوق *EXECUTION مع حروف العمليات d s s i s تحت الأعمدة التي تغيرت. هذا شكل مختلف من خمسة تعديلات عن السلّم في المفهوم الأول، ومع ذلك فكلفته هي نفسها وفق الاصطلاحين، 8 هنا و5 بالكلف الأحادية: السلّم يستبدل n بـ c ثم يدرج u، والمسار العريض يدرج c ثم يستبدل n بـ u، وكلاهما يحتفظ بالمطابقات المجانية الأربع في النهاية. ويفترق الاثنان تمامًا عند التعادل ذي الأسهم الثلاثة في (5,5): السهم الأيسر يقود إلى السلّم، والقطري إلى المسار العريض. هذه هي قاعدة التعادل في القسم الفرعي التالي وهي تعمل.
The general mechanism is what slide 90 states. While filling, each cell records which of its three candidates achieved the minimum, the argmin, as a Backpointer. Up means the source letter was deleted, left means the target letter was inserted, diagonal means substitution or match. After the fill, start at D[n,m] and follow arrows until D[0,0]. SLP3 puts it in one sentence: each complete path between the final cell and the initial cell is a minimum distance Alignment. Every step decreases i, j or both, so the walk takes at most n + m steps, which is why SLP3's lecture slides list the Backtrace as O(n + m).
الآلية العامة هي ما تنص عليه الشريحة 90. أثناء الملء، تسجل كل خلية أيًا من مرشحيها الثلاثة حقق القيمة الصغرى، أي الـ argmin، على شكل backpointer (مؤشر خلفي). السهم إلى الأعلى يعني أن حرف المصدر حُذف، وإلى اليسار يعني أن حرف الهدف أُدرج، والقطري يعني استبدالًا أو مطابقة. بعد الملء، ابدأ من D[n,m] واتبع الأسهم حتى D[0,0]. ويقولها SLP3 في جملة واحدة: كل مسار كامل بين الخلية الأخيرة والخلية الأولى هو alignment بأقل مسافة. وكل خطوة تُنقص i أو j أو كليهما، لذا يستغرق السير n + m خطوة على الأكثر، ولهذا تذكر شرائح محاضرة SLP3 أن backtrace (التتبع الرجوعي) يكلف O(n + m).
How to read an arrowكيف تقرأ السهم
- Upأعلى
- delete source[i], move to (i-1, j); two bold cells in one columnاحذف source[i] وانتقل إلى (i-1, j)؛ خليتان عريضتان في عمود واحد
- Leftيسار
- insert target[j], move to (i, j-1); two bold cells in one rowأدرج target[j] وانتقل إلى (i, j-1)؛ خليتان عريضتان في صف واحد
- Diagonalقطري
- substitute or match, move to (i-1, j-1); cost 0 when the letters are equalاستبدال أو تطابق، وانتقل إلى (i-1, j-1)؛ الكلفة 0 عندما يتساوى الحرفان
Ties are alternative alignmentsالتعادلات محاذاات بديلة
A cell like (1,1) on slide 91 carries three arrows because three candidates reached the same minimum. SLP3 says this directly: some cells have multiple backpointers because the minimum extension could have come from multiple previous cells. Each choice at a tie leads to a different complete path, and every complete path has the same total cost, so ties are exactly the reason multiple optimal alignments exist. For intention to execution there are 134 such paths under the cost-2 convention and 7 under unit costs (both counted for this part by walking every pointer set). Erickson shows a smaller case in his textbook, ALGORITHM to ALTRUISTIC, whose table has exactly three optimal paths. Go back to the simulator, press Backtrace, then Next alignment, and watch the highlighted path change at the teal-dotted cells while the distance stays fixed.
خلية مثل (1,1) في الشريحة 91 تحمل ثلاثة أسهم لأن ثلاثة مرشحين وصلوا إلى القيمة الصغرى نفسها. ويقول SLP3 ذلك صراحة: بعض الخلايا لها عدة مؤشرات خلفية لأن الامتداد الأدنى كان يمكن أن يأتي من عدة خلايا سابقة. كل اختيار عند التعادل يقود إلى مسار كامل مختلف، وكل مسار كامل له الكلفة الإجمالية نفسها، لذا فالتعادلات هي بالضبط سبب وجود عدة محاذاات مثلى. ولتحويل intention إلى execution يوجد 134 مسارًا من هذا النوع وفق اصطلاح الكلفة 2، و7 بالكلف الأحادية (وكلاهما حُسب لهذا الجزء بالمرور على كل مجموعة مؤشرات). ويعرض Erickson حالة أصغر في كتابه، من ALGORITHM إلى ALTRUISTIC، وجدولها فيه ثلاثة مسارات مثلى بالضبط. ارجع إلى المحاكي، واضغط Backtrace ثم Next alignment، وراقب المسار المظلل يتغير عند الخلايا المنقطة باللون الأزرق المخضر بينما تبقى المسافة ثابتة.
Quick checkاختبار سريع
A backpointer cell carries three arrows. What does that tell you about the alignments?خلية من خلايا المؤشرات الخلفية (backpointers) تحمل ثلاثة أسهم. ماذا يخبرك ذلك عن المحاذاات؟
Recall
A cell has two arrows. What does this mean for the alignments?خلية فيها سهمان. ماذا يعني ذلك بالنسبة للمحاذاات؟
Recall
Fill the unit-cost table for cat to cut and give the distance and one alignment.املأ جدول الكلف الأحادية من cat إلى cut، واذكر المسافة ومحاذاة واحدة.
The intention to execution table has 10 x 10 = 100 cells, and each cost a glance at three neighbors. Two documents of 100,000 characters each would need 10^10 cells, which is the scale where the accounting starts to matter and where the Stanford CS 262 notes on alignment begin their discussion of space.
جدول intention إلى execution فيه 10 x 10 = 100 خلية، وكل منها كلّف نظرة سريعة إلى ثلاثة جيران. أما وثيقتان طول كل منهما 100,000 حرف فستحتاجان إلى 10^10 خلية، وهذا هو الحجم الذي يبدأ عنده الحساب الدقيق للكلفة بالأهمية، وهو المكان الذي تبدأ فيه ملاحظات Stanford CS 262 عن المحاذاة نقاشها حول الذاكرة.
Time is the easy half. There are (n + 1)(m + 1) cells, each computed in constant time from three already-known neighbors, so the fill runs in O(mn), the bound Wagner and Fischer gave in 1974 and the one Erickson states for his edit distance chapter. Space is where the choices begin. Storing the whole table, as the simulator does, also takes O(mn). But look at the recurrence again: row i reads only row i - 1 and the cells to its own left. If all you want is the distance, keep two rows, the previous one and the one being filled, and throw the rest away. Put the shorter string across the columns and the memory is O(min(m,n)), exactly what slide 92 says.
الزمن هو النصف السهل. هناك (n + 1)(m + 1) خلية، تُحسب كل منها في زمن ثابت من ثلاثة جيران معروفين مسبقًا، لذا يعمل الملء في O(mn)، وهو الحد الذي قدّمه Wagner و Fischer عام 1974 والذي يذكره Erickson في فصله عن مسافة التعديل. أما الذاكرة فهي حيث تبدأ الخيارات. تخزين الجدول كاملًا، كما يفعل المحاكي، يأخذ أيضًا O(mn). لكن انظر إلى علاقة التكرار مرة أخرى: الصف i لا يقرأ إلا الصف i - 1 والخلايا الواقعة على يساره في الصف نفسه. فإذا كان كل ما تريده هو المسافة، فاحتفظ بصفين، السابق والذي يُملأ حاليًا، وتخلّص من الباقي. ضع السلسلة الأقصر على امتداد الأعمدة فتصبح الذاكرة O(min(m,n))، وهذا بالضبط ما تقوله الشريحة 92.
The price of that saving is the third bullet of the slide. The backpointers lived in the rows you discarded, so the two-row version cannot Backtrace. Erickson puts it plainly: by throwing away most of the table, we apparently lose the ability to walk backward. If you need the alignment as well, either keep the full table with its arrows, or recompute cleverly. Hirschberg's 1975 algorithm is the clever recomputation: run the two-row fill forward from the start and backward from the end, meet in the middle column to find which cell of that column the optimal path passes through, then recurse on the two halves. It recovers the full alignment in O(m + n) space while staying O(mn) in time; the CS 262 proof shows the constant roughly doubles, because every level of the recursion refills about half the area of the level above.
ثمن هذا التوفير هو النقطة الثالثة في الشريحة. فالـ backpointers كانت تعيش في الصفوف التي تخلصت منها، لذا لا تستطيع النسخة ذات الصفين إجراء backtrace. ويقولها Erickson بوضوح: بالتخلص من معظم الجدول، نفقد على ما يبدو القدرة على السير رجوعًا. إذا احتجت إلى المحاذاة أيضًا، فإما أن تحتفظ بالجدول كاملًا مع أسهمه، أو تعيد الحساب بذكاء. وHirschberg's 1975 algorithm (خوارزمية Hirschberg عام 1975) هي إعادة الحساب الذكية: نفّذ الملء بصفين إلى الأمام من البداية وإلى الخلف من النهاية، والتقِ في العمود الأوسط لتعرف أي خلية من ذلك العمود يمر بها المسار الأمثل، ثم طبّق العودية على النصفين. وهي تستعيد المحاذاة كاملة في ذاكرة O(m + n) مع بقاء الزمن O(mn)؛ ويبين برهان CS 262 أن الثابت يتضاعف تقريبًا، لأن كل مستوى من العودية يعيد ملء نحو نصف مساحة المستوى الذي فوقه.
| Goalالهدف | Timeالزمن | Spaceالذاكرة | What to storeما الذي يُخزَّن |
|---|---|---|---|
| Distance onlyالمسافة فقط | O(mn)O(mn) | O(min(m,n))O(min(m,n)) | Two rows: the previous one and the one being filledصفان: الصف السابق والصف الذي يُملأ حاليًا |
| Distance and alignmentالمسافة والمحاذاة | O(mn)O(mn) | O(mn)O(mn) | The whole table with an arrow set in every cellالجدول كاملًا مع مجموعة أسهم في كل خلية |
| Alignment in linear spaceالمحاذاة في ذاكرة خطية | O(mn), about twice the constantO(mn)، والثابت تقريبًا ضعفان | O(m+n)O(m+n) | Hirschberg's divide-and-conquer recursion, no stored tableعودية Hirschberg القائمة على التقسيم والدمج (divide-and-conquer)، دون تخزين الجدول |
Quick checkاختبار سريع
You need only the distance between two strings of lengths m and n. What is the least memory the standard algorithm needs?تحتاج فقط إلى المسافة بين سلسلتين طولاهما m و n. ما أقل ذاكرة تحتاجها الخوارزمية القياسية؟
Recall
You need only the distance between two long strings. How much memory do you need and why?تحتاج فقط إلى المسافة بين سلسلتين طويلتين. كم من الذاكرة تحتاج، ولماذا؟
Slide 93 closes the lecture with seven lines, and they tell one story. A word is not a universal primitive, so Tokenization is a design decision rather than a fact about text. Whatever units you pick, their frequencies follow a heavy tail (Zipf for rank against frequency, Heaps for vocabulary growth), so a fixed word vocabulary always leaks unseen words, and subwords are the answer.
تختم الشريحة 93 المحاضرة بسبعة أسطر، وهي تروي قصة واحدة. الكلمة ليست وحدة أولية عامة، لذا فإن tokenization (التقطيع) قرار تصميمي وليس حقيقة عن النص. وأيًا كانت الوحدات التي تختارها، فإن تكراراتها تتبع ذيلًا ثقيلًا (Zipf للرتبة مقابل التكرار، وHeaps لنمو المفردات)، لذا فإن أي مفردات كلمات ثابتة ستتسرب منها دائمًا كلمات غير مرئية سابقًا، والوحدات الجزئية (subwords) هي الحل.
Morphology shows that words do have meaningful pieces, morphemes, but that segmenting them cleanly is hard and differs across languages. Unicode and UTF-8 make multilingual text representable at all, and their bytes are the universal fallback when nothing else fits. BPE then learns a compact, reusable subword vocabulary from data, with a regex pre-tokenizer doing the practical work of splitting and normalizing before it. Finally, once text has become strings of units, the Minimum edit distance table you just filled is the foundational dynamic program for saying how similar two of those strings are.
يبين علم الصرف (morphology) أن للكلمات فعلًا أجزاء ذات معنى، هي morphemes (المورفيمات)، لكن تقطيعها بشكل نظيف صعب ويختلف من لغة إلى أخرى. و Unicode وUTF-8 يجعلان النص متعدد اللغات قابلًا للتمثيل أصلًا، وبايتاتهما هي الخيار الاحتياطي العام حين لا يناسب أي شيء آخر. ثم تتعلم BPE مفردات وحدات جزئية مضغوطة وقابلة لإعادة الاستخدام من البيانات، مع مقطّع مسبق (pre-tokenizer) مبني على regex يتولى العمل العملي للتقسيم والتطبيع قبلها. وأخيرًا، بعد أن يصبح النص سلاسل من الوحدات، يكون جدول minimum edit distance الذي ملأته للتو هو البرنامج الديناميكي الأساسي لقياس مدى تشابه سلسلتين من تلك السلاسل.
- Word is not a universal primitive; tokenization is a design decision.الكلمة ليست وحدة أولية عامة؛ والتقطيع (tokenization) قرار تصميمي.
- Vocabulary growth follows a heavy tail (Zipf, Heaps), which motivates subwords.نمو المفردات يتبع ذيلًا ثقيلًا (Zipf، Heaps)، وهذا ما يبرر الوحدات الجزئية (subwords).
- Morphology differs across languages; morphemes are meaningful but not always easy to segment.الصرف يختلف بين اللغات؛ والمورفيمات ذات معنى لكن تقطيعها ليس سهلًا دائمًا.
- Unicode and UTF-8 make multilingual text possible, and bytes are a universal fallback.Unicode وUTF-8 يجعلان النص متعدد اللغات ممكنًا، والبايتات خيار احتياطي عام.
- BPE learns a compact, reusable subword vocabulary.BPE تتعلم مفردات وحدات جزئية مضغوطة وقابلة لإعادة الاستخدام.
- Regex is the practical tool for pre-tokenization and normalization.التعابير النمطية (regex) هي الأداة العملية للتقطيع المسبق والتطبيع.
- Minimum edit distance is the foundational DP algorithm for string similarity.مسافة التعديل الصغرى (minimum edit distance) هي خوارزمية البرمجة الديناميكية الأساسية لقياس تشابه السلاسل النصية.
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- D[i,0] = i deletions, D[0,j] = j insertions, D[0,0] = 0.D[i,0] = i عمليات حذف، و D[0,j] = j عمليات إدراج، و D[0,0] = 0.
- D[i,j] is the minimum of up plus delete, left plus insert, and diagonal plus substitute, where substitute costs 0 on a match.D[i,j] هي القيمة الصغرى من: الخلية العليا زائد كلفة الحذف، والخلية اليسرى زائد كلفة الإدراج، والخلية القطرية زائد كلفة الاستبدال، حيث تكون كلفة الاستبدال 0 عند التطابق.
- intention to execution costs 5 under unit costs and 8 under SLP3's cost-2 substitution; slide 89 uses cost 2.تحويل intention إلى execution يكلف 5 بالكلف الأحادية، و8 وفق اصطلاح SLP3 الذي يجعل كلفة الاستبدال 2؛ والشريحة 89 تستخدم الكلفة 2.
- Store the argmin arrows while filling; backtrace from D[n,m] to D[0,0] in O(n+m) steps; several arrows in a cell mean several optimal alignments (134 at cost 2, 7 at cost 1 for this pair).خزّن أسهم الـ argmin أثناء الملء؛ ثم تتبعها رجوعًا من D[n,m] إلى D[0,0] في O(n+m) خطوة؛ ووجود عدة أسهم في خلية يعني وجود عدة محاذاات مثلى (134 بالكلفة 2، و7 بالكلفة 1 لهذا الزوج من الكلمات).
- Time O(mn). Space O(mn) with the table, O(min(m,n)) with two rows when only the distance is needed. Hirschberg recovers the alignment in O(m+n) space.الزمن O(mn). والذاكرة O(mn) مع الجدول الكامل، و O(min(m,n)) مع صفين فقط عندما نحتاج المسافة وحدها. وخوارزمية Hirschberg تستعيد المحاذاة في ذاكرة O(m+n).
- Lecture takeaways: tokenization is a design decision; heavy tails motivate subwords; morphemes are meaningful but hard to segment; Unicode and UTF-8 with bytes as the fallback; BPE learns a compact vocabulary; regex handles pre-tokenization and normalization; edit distance is the foundational DP for string similarity.خلاصات المحاضرة: التقطيع (tokenization) قرار تصميمي؛ والذيول الثقيلة في توزيع الكلمات تبرر استخدام الوحدات الجزئية (subwords)؛ والمورفيمات ذات معنى لكن تقطيعها صعب؛ و Unicode و UTF-8 مع البايتات كخيار احتياطي؛ و BPE تتعلم مفردات مضغوطة؛ والتعابير النمطية (regex) تتولى التقطيع المسبق والتطبيع؛ ومسافة التعديل هي خوارزمية البرمجة الديناميكية الأساسية لقياس تشابه السلاسل النصية.
Sourcesالمصادر
- Speech and Language Processing, 3rd edition, chapter 2: Words and TokensBookStanford University (Jurafsky and Martin)Section 2.9, Eq. 2.19 and 2.20, Figs. 2.17 and 2.19 to 2.22 in the August 19, 2026 releaseالقسم 2.9، والمعادلتان 2.19 و 2.20، والأشكال 2.17 و 2.19 إلى 2.22 في إصدار 19 أغسطس 2026(opens in a new tab)(يفتح في تبويب جديد)
- Speech and Language Processing, online manuscript indexDocsStanford University (Jurafsky and Martin)Cited on slide 94 as the January 6, 2026 manuscriptمذكور في الشريحة 94 بوصفه مخطوطة 6 يناير 2026(opens in a new tab)(يفتح في تبويب جديد)
- Minimum Edit Distance lecture slidesDocsStanford University (Jurafsky)Time O(nm), space O(nm), backtrace O(n+m); remember where you came fromالزمن O(nm)، والذاكرة O(nm)، والتتبع الرجوعي O(n+m)؛ تذكّر من أين أتيت(opens in a new tab)(يفتح في تبويب جديد)
- The String-to-String Correction ProblemPaperJournal of the ACM 21(1):168-173, 1974 (Wagner and Fischer)The O(mn) dynamic programming algorithmخوارزمية البرمجة الديناميكية ذات التعقيد O(mn)(opens in a new tab)(يفتح في تبويب جديد)
- A linear space algorithm for computing maximal common subsequencesPaperCommunications of the ACM 18(6):341-343, 1975 (Hirschberg)Alignment recovery in O(mn) time and linear spaceاستعادة المحاذاة في زمن O(mn) وذاكرة خطية(opens in a new tab)(يفتح في تبويب جديد)
- Binary codes capable of correcting deletions, insertions, and reversalsPaperDoklady Akademii Nauk SSSR 163(4):845-848, 1965 (Levenshtein)Origin of the Levenshtein distanceأصل مسافة Levenshtein(opens in a new tab)(يفتح في تبويب جديد)
- Algorithms, chapter 3: Dynamic ProgrammingBookJeff Erickson, University of IllinoisEdit distance in O(mn) time and space; ALGORITHM to ALTRUISTIC with three optimal pathsمسافة التعديل في زمن وذاكرة O(mn)؛ وتحويل ALGORITHM إلى ALTRUISTIC بثلاثة مسارات مثلى(opens in a new tab)(يفتح في تبويب جديد)
- Algorithms, appendix D: Advanced Dynamic Programming, D.1 Saving SpaceBookJeff Erickson, University of IllinoisTwo-row trick, loss of the backward walk, and Hirschberg's O(m+n) space boundحيلة الصفين، وفقدان القدرة على السير رجوعًا، وحد الذاكرة O(m+n) لخوارزمية Hirschberg(opens in a new tab)(يفتح في تبويب جديد)
- CS 262 lecture 3 notes: Linear Space AlignmentDocsStanford UniversityTwo-column scoring, Hirschberg's O(N+M) space and O(MN) time with about twice the constantحساب الدرجات بعمودين، وخوارزمية Hirschberg بذاكرة O(N+M) وزمن O(MN) مع ثابت يقارب الضعف(opens in a new tab)(يفتح في تبويب جديد)
- Introduction to Algorithms, 4th editionBookMIT Press, 2022 (Cormen, Leiserson, Rivest and Stein)Dynamic programming chapter: optimal substructure and overlapping subproblemsفصل البرمجة الديناميكية: البنية الجزئية المثلى والمسائل الجزئية المتداخلة(opens in a new tab)(يفتح في تبويب جديد)