ArticleslgStudy

computer science

Hirschberg's algorithm

Hirschberg's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hirschberg's algorithm rather than just read about it. In short: In computer science, Hirschberg's algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the optimal sequence alignment between two strings. Optimality is measured with the Levenshtein distance, defined to be the sum of the costs of insertions, replacements, deletions, and null actions needed to change one string into the other.

Key takeaways

  • Hirschberg's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hirschberg's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hirschberg's algorithm from memory before moving on to harder problems.

Reference excerpt

In computer science, Hirschberg's algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the optimal sequence alignment between two strings. Optimality is measured with the Levenshtein distance, defined to be the sum of the costs of insertions, replacements, deletions, and null actions needed to change one string into the other. Hirschberg's algorithm is simply described as a more space-efficient version of the Needleman–Wunsch algorithm that uses dynamic programming. Hirschberg's algorithm is commonly used in computational biology to find maximal global alignments of DNA and protein sequences.

Algorithm information Hirschberg's algorithm is a generally applicable algorithm for optimal sequence alignment. BLAST and FASTA are suboptimal heuristics. If X {\displaystyle X} and Y {\displaystyle Y} are strings, where length ⁡ ( X ) = n {\displaystyle \operatorname {length} (X)=n} and length ⁡ ( Y ) = m {\displaystyle \operatorname {length} (Y)=m} , the Needleman–Wunsch algorithm finds an optimal alignment in O ( n m ) {\displaystyle O(nm)} time, using O ( n m ) {\displaystyle O(nm)} space. Hirschberg's algorithm is a clever modification of the Needleman–Wunsch Algorithm, which still takes O ( n m ) {\displaystyle O(nm)} time, but needs only O ( min { n , m } ) {\displaystyle O(\min\{n,m\})} space and is much faster in practice. One application of the algorithm is finding sequence alignments of DNA or protein sequences. It is also a space-efficient way to calculate the longest common subsequence between two sets of data such as with the common diff tool. The Hirschberg algorithm can be derived from the Needleman–Wunsch algorithm by observing that:

one can compute the optimal alignment score by only storing the current and previous row of the Needleman–Wunsch score matrix; if ( Z , W ) = NW ⁡ ( X , Y ) {\displaystyle (Z,W)=\operatorname {NW} (X,Y)} is the optimal alignment of ( X , Y ) {\displaystyle (X,Y)} , and X = X l + X r {\displaystyle X=X^{l}+X^{r}} is an arbitrary partition of X {\displaystyle X} , there exists a partition Y l + Y r {\displaystyle Y^{l}+Y^{r}} of Y {\displaystyle Y} such that NW ⁡ ( X , Y ) = NW ⁡ ( X l , Y l ) + NW ⁡ ( X r , Y r ) {\displaystyle \operatorname {NW} (X,Y)=\operatorname {NW} (X^{l},Y^{l})+\operatorname {NW} (X^{r},Y^{r})} .

Algorithm description

X i {\displaystyle X_{i}} denotes the i-th character of X {\displaystyle X} , where 1 ⩽ i ⩽ length ⁡ ( X ) {\displaystyle 1\leqslant i\leqslant \operatorname {length} (X)} . X i : j {\displaystyle X_{i:j}} denotes a substring of size j − i + 1 {\displaystyle j-i+1} , ranging from the i-th to the j-th character of X {\displaystyle X} . rev ⁡ ( X ) {\displaystyle \operatorname {rev} (X)} is the reversed version of X {\displaystyle X} .

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Hirschberg's algorithm

Start with the simplest possible case. Write down what Hirschberg's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hirschberg's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hirschberg's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hirschberg's algorithm

In research
Hirschberg's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hirschberg's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hirschberg's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Bioinformatics algorithms, Dynamic programming, Sequence alignment algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Hirschberg's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Hirschberg's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hirschberg's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hirschberg's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hirschberg's algorithm in simple terms?

In computer science, Hirschberg's algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the optimal sequence alignment between two strings. Optimality is measured with the Levenshtein distance, defined to be the sum of the costs of insertions, replacemen…

Why does Hirschberg's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hirschberg's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hirschberg's algorithm.

Tags

  • Bioinformatics algorithms
  • Dynamic programming
  • Sequence alignment algorithms

Keep exploring