ArticleslgStudy

computer science

Re-Pair

Re-Pair 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 Re-Pair rather than just read about it. In short: Re-Pair (short for recursive pairing) is a grammar-based compression algorithm that, given an input text, builds a straight-line program, i.e. a context-free grammar generating a single string: the input text. In order to perform the compression in linear time, it consumes the amount of memory that is approximately five times the size of its input.

Re-Pair — main illustration
Re-Pair — illustration

Key takeaways

  • Re-Pair 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 Re-Pair to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Re-Pair from memory before moving on to harder problems.

Reference excerpt

Re-Pair (short for recursive pairing) is a grammar-based compression algorithm that, given an input text, builds a straight-line program, i.e. a context-free grammar generating a single string: the input text. In order to perform the compression in linear time, it consumes the amount of memory that is approximately five times the size of its input. The grammar is built by recursively replacing the most frequent pair of characters occurring in the text. Once there is no pair of characters occurring twice, the resulting string is used as the axiom of the grammar. Therefore, the output grammar is such that all rules but the axiom have two symbols on the right-hand side. Re-Pair was first introduced by N. J. Larsson and A. Moffat in 1999.

How it works

In their paper the algorithm is presented together with a detailed description of the data structures required to implement it with linear time and space complexity. The experiments showed that Re-Pair achieves high compression ratios and offers good performance for decompression. However, the major drawback of the algorithm is its memory consumption, which is approximately 5 times the size of the input. Such memory usage is required in order to perform the compression in linear time but makes the algorithm impractical for compressing large files. The image on the right shows how the algorithm compresses the string w = x a b c a b c y 123123 z a b c {\displaystyle w=xabcabcy123123zabc} . During the first iteration, the pair a b {\displaystyle ab} , which occurs three times in w {\displaystyle w} , is replaced by a new symbol R 1 {\displaystyle R_{1}} . On the second iteration, the most frequent pair in the string w = x R 1 c R 1 c y 123123 z R 1 c {\displaystyle w=xR_{1}cR_{1}cy123123zR_{1}c} , which is R 1 c {\displaystyle R_{1}c} , is replaced by a new symbol R 2 {\displaystyle R_{2}} . Thus, at the end of the second iteration, the remaining string is w = x R 2 R 2 y 123123 z R 2 {\displaystyle w=xR_{2}R_{2}y123123zR_{2}} . In the next two iterations, the pairs 12 {\displaystyle 12} and R 3 3 {\displaystyle R_{3}3} are replaced by symbols R 3 {\displaystyle R_{3}} and R 4 {\displaystyle R_{4}} respectively. Finally, the string w = x R 2 R 2 y R 4 R 4 z R 2 {\displaystyle w=xR_{2}R_{2}yR_{4}R_{4}zR_{2}} contains no repeated pair and therefore it is used as the axiom of the output grammar.

Data structures In order to achieve linear time complexity, Re-Pair requires the following data structures

A sequence representing the input string. Position i {\displaystyle i} of the sequence contains the i-th symbol of the input string plus two references to other positions in the sequence. These references point to the next/previous positions, say k {\displaystyle k} and m {\displaystyle m} , such that the same substring begins at w [ i ] {\displaystyle w[i]} , w [ k ] {\displaystyle w[k]} and w [ m ] {\displaystyle w[m]} and all three occurrences are captured by the same reference (i.e. there is a variable in the grammar generating the string). A priority queue. Each element of the queue is a pair of symbols (terminals or previously defined pairs) that occur consecutively in the sequence. The priority of a pair is given by the number of occurrences of the pair in the remaining sequence. Each time a new pair is created, the priority queue is updated. A hash table to keep track of already defined pairs. This table is updated each time a new pair is created or removed. Since the hash table and the priority queue refer to the same elements (pairs), they can be implemented by a common data structure called PAIR with pointers for the hash table (h_next) and the priority queue (p_next and p_prev). Furthermore, each PAIR points to the beginning of the first (f_pos) and the last (b_pos) occurrences of the string represented by the PAIR in the sequence. The following picture shows an overview of this data structure.

The following two pictures show an example of how these data structures look after the initialization and after applying one step of the pairing process (pointers to NULL are not displayed):

… excerpt ends here. Continue reading the full article.

Illustrations

Re-Pair illustration
Re-Pair illustration
Re-Pair illustration

Worked examples

Example 1 — a first encounter with Re-Pair

Start with the simplest possible case. Write down what Re-Pair 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 Re-Pair 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 Re-Pair 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 Re-Pair

In research
Re-Pair 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 Re-Pair 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
Re-Pair is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compression algorithms, Data compression, Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Re-Pair 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 Re-Pair in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Re-Pair 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 Re-Pair out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Re-Pair in simple terms?

Re-Pair (short for recursive pairing) is a grammar-based compression algorithm that, given an input text, builds a straight-line program, i.e. a context-free grammar generating a single string: the input text. In order to perform the compression in linear time, it consumes the amount of memory that…

Why does Re-Pair 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 Re-Pair?

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 Re-Pair.

Tags

  • Compression algorithms
  • Data compression
  • Lossless compression algorithms

Keep exploring