array sorting
suffix sorting
block sorting
algorithm
computer science

How to sort array suffixes in block sorting

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Block sorting, commonly recognized through the Burrows–Wheeler Transform (BWT), is a critical component in data compression and text processing. One of the key tasks in implementing block sorting techniques involves sorting the suffixes of an array. This article delves into the intricacies of sorting array suffixes in block sorting, suitable for enhancing understanding among software developers and computer scientists.

Suffix Array Overview

A suffix array is a sorted array of all suffixes of a string. It is deeply rooted in string processing algorithms, used for solving complex string-related problems more efficiently. In the context of block sorting, suffix arrays are pivotal as they facilitate the BWT's transformation, yielding better compression ratios by clustering similar characters.

Consider a string s = "banana". The suffixes of s are:

  1. "banana"
  2. "anana"
  3. "nana"
  4. "ana"
  5. "na"
  6. "a"

When sorted lexicographically, they appear as:

  1. "a"
  2. "ana"
  3. "anana"
  4. "banana"
  5. "na"
  6. "nana"

The suffix array contains the starting indices of these sorted suffixes, such as [5, 3, 1, 0, 4, 2].

Techniques to Sort Array Suffixes

Naive Approach

A straightforward approach involves generating all possible suffixes and sorting them using a comparison-based algorithm like QuickSort or MergeSort:

python
1def sort_suffixes_naive(s):
2    suffixes = [(s[i:], i) for i in range(len(s))]
3    suffixes.sort()  # Sorts based on the string components
4    return [suffix[1] for suffix in suffixes]
5
6s = "banana"
7print(sort_suffixes_naive(s))

Though conceptually simple, this approach has an O(n2logn)O(n^2 \log n) time complexity due to the overhead of sorting, making it impractical for large strings.

Efficient Algorithms

  1. Suffix Array with DC3 Algorithm (Kärkkäinen-Sanders-Burkhardt): The DC3 algorithm, also known as skew or the KSB algorithm, constructs the suffix array in O(n)O(n) time. The process involves:
    • Generating triplets of suffix indices modulo 3.
    • Sorting these triplets and recursively solving reduced problems.
    • Merging the results.
  2. Manber-Myers Algorithm: This is an O(nlogn)O(n \log n) algorithm typically preferred for its simplicity over DC3. It iteratively doubles the span of considered substrings and employs a rank-based sorting:
python
1   def sort_suffixes_manber_myers(s):
2       n = len(s)
3       suffix_arr = list(range(n))
4       rank = [ord(c) for c in s] + [-1]
5       
6       k = 1
7       while k < n:
8           key = lambda x: (rank[x], rank[x + k] if x + k < n else -1)
9           suffix_arr.sort(key=key)
10           tmp = [0] * n
11           
12           for i in range(1, n):
13               tmp[suffix_arr[i]] = tmp[suffix_arr[i - 1]]
14               if key(suffix_arr[i]) > key(suffix_arr[i - 1]):
15                   tmp[suffix_arr[i]] += 1
16               
17           rank = tmp[:]
18           k *= 2
19       
20       return suffix_arr
21   
22   print(sort_suffixes_manber_myers("banana"))

Key Considerations and Trade-offs

ApproachTime ComplexitySpace ComplexityRemark
NaiveO(n2logn)O(n^2 \log n)O(n2)O(n^2)Inefficient for long strings but easy to implement.
DC3 AlgorithmO(n)O(n)O(n)O(n)Optimal but complex; suitable for very large inputs.
Manber-Myers AlgorithmO(nlogn)O(n \log n)O(n)O(n)Balanced approach with simpler implementation than DC3.

Applications

Sorted suffix arrays greatly enhance text searching, pattern matching, and data compression techniques like BWT in tools such as bzip2. They help in detecting repeated substrings, approximating matching, and even in bioinformatics for genome alignment tasks.

Conclusion

Sorting suffix arrays is a profound and necessary step in block sorting algorithms. Efficient methods like the DC3 and Manber-Myers present viable solutions for varying constraints and requirements of modern applications. Understanding these techniques provides invaluable insights into the power of text processing and compression, propelling advancements in computational technologies.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.