University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI320 Chap.9 Sequence Alignment, Edit Distance and Linear Space

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 9 of 13 · COMPSCI320

Sequence Alignment, Edit Distance and Linear Space

Edit distance measures how similar two strings are by the cheapest alignment under a gap penalty and a mismatch penalty.

This chapter defines alignments, derives the three-case recurrence, fills and traces back a full table, explains how Hirschberg's method recovers an optimal alignment in linear space, and reduces the longest common subsequence to alignment.

The worked table uses short words so every entry can be checked, and the practice cards change the penalties to show how the optimal alignment shifts.

The chapter also explains why linear space is easy for the value but needs divide and conquer for the alignment itself.

In this chapter

What this chapter covers

  • 01

    Alignments without crossing pairs

  • 02

    Gap and mismatch penalties

  • 03

    The three-case recurrence

  • 04

    Filling and tracing back the table

  • 05

    Hirschberg's linear-space method

  • 06

    Longest common subsequence

Worked example · free

Align two words with gap and mismatch penalties

Q [4 marks]. With a gap penalty of 2 and a mismatch penalty of 3, find the cost of an optimal alignment of SNOWY with SUNNY. The marks are an AskSia practice weighting, not a university marking scheme.
An optimal alignment of SNOWY with SUNNY when a gap costs 2 and a mismatch costs 3.An optimal alignment of SNOWY with SUNNY when a gap costs 2 and a mismatch costs 3. Green lines are matches, the dashed line is the W and N mismatch, and the orange letters O and U are left unmatched, for a total cost of 7.SNOWYSUNNY
An optimal alignment of SNOWY with SUNNY when a gap costs 2 and a mismatch costs 3. Green lines are matches, the dashed line is the W and N mismatch, and the orange letters O and U are left unmatched, for a total cost of 7.
  • 1Fill the first row and column with multiples of 2.
  • 1Fill each cell as the minimum of the diagonal, upper and left candidates.
  • 1The bottom-right cell gives an optimal cost of 7.
  • 1Trace back: S–S, N–N, W–N and Y–Y with O and U unmatched, costing 3 + 2 + 2.
This four-mark allocation is an AskSia practice weighting and not an official university scheme. The edit distance is 7, from one mismatch and two unmatched letters.
Sia tip — Write the two penalties above the table before the first entry so they are not swapped halfway through.
Glossary

Key terms

Edit Distance
The minimum total penalty of an alignment that lines up two strings using gaps and mismatches.
Gap Penalty
The cost charged for each character left unmatched in an alignment.
Sequence Alignment
A set of non-crossing pairs of characters from two strings, each character used at most once.
Longest Common Subsequence
The longest string obtainable from both strings by deleting characters without reordering.
Hirschberg's Method
A divide and conquer refinement that recovers an optimal alignment in linear space.
Mismatch Penalty
The cost charged for pairing two different characters in an alignment.
FAQ

Sequence Alignment, Edit Distance and Linear Space FAQ

Why are the first row and column of the table not zero?

They align a prefix of one string with the empty string, so every character of that prefix is left unmatched and pays the gap penalty. The entry for a prefix of length i is therefore i times the gap penalty. Writing zeros there is the most common slip on these tables and shifts every later entry.

How can an alignment be found in linear space?

Hirschberg's method finds a point on an optimal path through the middle column by adding forward distances from the start to backward distances from the end. Both columns can be computed with two rows of memory. Recording that point and recursing on the two halves recovers the whole alignment, still in O(mn) time but O(m + n) space.

How is the longest common subsequence related to edit distance?

Set the gap penalty to 1 and make every mismatch infinitely expensive, so an alignment uses only matches and gaps. The matched characters form a common subsequence and every other character is a gap. The length of the longest common subsequence is then the total length of both strings minus the edit distance, all divided by two.

How do the penalties change the best alignment?

The best alignment balances mismatches against gaps. When a mismatch is cheap compared with two gaps, the alignment pairs characters even if they differ; when a mismatch is expensive, it leaves characters unmatched instead. Recompute the whole table when a penalty changes, because the change can alter decisions well before the final cell.

Study strategy

Exam move

Fill two full tables by hand, one with cheap and one with expensive mismatches, and compare the alignments they produce. Practise the traceback by labelling each step as a pair, a gap in the first string or a gap in the second. Then explain the Hirschberg split in three sentences without notes.

As a last drill, align two short words of your own under two different penalty settings and explain in one sentence why the optimal alignment changed. Close by writing the longest common subsequence recurrence beside the alignment recurrence and marking where they differ.

Working through Sequence Alignment, Edit Distance and Linear Space in COMPSCI320? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI320 Sequence Alignment, Edit Distance and Linear Space question and get a clear, step-by-step explanation grounded in how COMPSCI320 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 34 of your University of Auckland subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMPSCI320 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works