Browse Tutorials
Suffix Array and Suffix Tree in Data Structures & Applications

Suffix Array and Suffix Tree in Data Structures & Applications

09 Feb 2024
Advanced
1.25K Views
17 min read
Learn via Video Course & by Doing Hands-on Labs

Data Structures & Algorithms For Beginners Free Course

Suffix Array and Suffix Tree in Data Structures: An Overview

Suffix Array and Suffix Tree are two of the most powerful search algorithms available today. When it comes to searching for a specific string or pattern in a large amount of text, these two algorithms have proven to be incredibly fast and accurate. Let's delve into the Suffix Arrays and Trees in this DSA tutorial to comprehend why they hold such prominence.

To further enhance your understanding and application of search concepts, consider enrolling in the best Data Structures and Algorithms Course, to gain comprehensive insights into effective data structure utilization for improved problem-solving and time management.

What is a Suffix Array in Data Structures?

It is an ordered array of all the suffixes of a given string, sorted lexicographically. This structure provides fast and efficient memory usage by storing the suffixes of the original string in a compact form. One of the main features that make the Suffix Array so useful is its ability to quickly find the location of a substring within the original string. By analyzing the properties of the Suffix Array, we can gain valuable insights into the structure and patterns of strings.

Naive method to build Suffix Array

Although there are more advanced algorithms to construct the Suffix Array with better time complexity, here is a simple and naive method to build a Suffix Array:


# Structure to store information of a suffix
class Suffix:
    def __init__(self, index, suff):
        self.index = index
        self.suff = suff

# A comparison function used by sort() to compare two suffixes
def suffix_cmp(a, b):
    return (a.suff > b.suff) - (a.suff < b.suff)

# This is the main function that takes a string 'txt' of size n as an
# argument, builds and returns the suffix array for the given string
def build_suffix_array(txt):
    n = len(txt)
    suffixes = [Suffix(0, '')] * n

    # Store suffixes and their indexes in an array of structures.
    # The structure is needed to sort the suffixes alphabetically
    # and maintain their old indexes while sorting
    for i in range(n):
        suffixes[i] = Suffix(i, txt[i:])

    # Sort the suffixes using the comparison function defined above
    suffixes.sort(key=lambda x: x.suff)

    # Store indexes of all sorted suffixes in the suffix array
    suffix_arr = [0] * n
    for i in range(n):
        suffix_arr[i] = suffixes[i].index

    # Return the suffix array
    return suffix_arr

# A utility function to print an array of given size
def print_arr(arr):
    n = len(arr)
    for i in range(n):
        print(arr[i], end=' ')
    print()

# Driver program to test above functions
txt = "banana"
suffix_arr = build_suffix_array(txt)
print("Following is the suffix array for", txt)
print_arr(suffix_arr)
 

import java.util.Arrays;

// Structure to store information of a suffix
class Suffix {
    int index;
    String suff;

    public Suffix(int index, String suff) {
        this.index = index;
        this.suff = suff;
    }
}

public class SuffixArray {

    // A comparison function used by sort() to compare two suffixes
    static int suffixCmp(Suffix a, Suffix b) {
        return (a.suff.compareTo(b.suff));
    }

    // This is the main function that takes a string 'txt' of size n as an
    // argument, builds and returns the suffix array for the given string
    static int[] buildSuffixArray(String txt) {
        int n = txt.length();
        Suffix[] suffixes = new Suffix[n];

        // Store suffixes and their indexes in an array of structures.
        // The structure is needed to sort the suffixes alphabetically
        // and maintain their old indexes while sorting
        for (int i = 0; i < n; i++) {
            suffixes[i] = new Suffix(i, txt.substring(i));
        }

        // Sort the suffixes using the comparison function defined above
        Arrays.sort(suffixes, SuffixArray::suffixCmp);

        // Store indexes of all sorted suffixes in the suffix array
        int[] suffixArr = new int[n];
        for (int i = 0; i < n; i++) {
            suffixArr[i] = suffixes[i].index;
        }

        // Return the suffix array
        return suffixArr;
    }

    // A utility function to print an array of given size
    static void printArr(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // Driver program to test above functions
    public static void main(String[] args) {
        String txt = "banana";
        int[] suffixArr = buildSuffixArray(txt);

        System.out.println("Following is the suffix array for " + txt);
        printArr(suffixArr);
    }
} 

 // Naive algorithm for building suffix array of a given text
 #include 
 #include 
 #include 
 using namespace std;
 
 // Structure to store information of a suffix
 struct suffix
 {
 int index;
 char *suff;
 };
 
 // A comparison function used by sort() to compare two suffixes
 int cmp(struct suffix a, struct suffix b)
 {
 return strcmp(a.suff, b.suff) < 0? 1 : 0;
 }
 
 // This is the main function that takes a string 'txt' of size n as an
 // argument, builds and return the suffix array for the given string
 int *buildSuffixArray(char *txt, int n)
 {
 // A structure to store suffixes and their indexes
 struct suffix suffixes[n];
 
 // Store suffixes and their indexes in an array of structures.
 // The structure is needed to sort the suffixes alphabetically
 // and maintain their old indexes while sorting
 for (int i = 0; i < n; i++)
 {
  suffixes[i].index = i;
  suffixes[i].suff = (txt+i);
 }
 
 // Sort the suffixes using the comparison function
 // defined above.
 sort(suffixes, suffixes+n, cmp);
 
 // Store indexes of all sorted suffixes in the suffix array
 int *suffixArr = new int[n];
 for (int i = 0; i < n; i++)
  suffixArr[i] = suffixes[i].index;
 
 // Return the suffix array
 return suffixArr;
 }
 
 // A utility function to print an array of given size
 void printArr(int arr[], int n)
 {
 for(int i = 0; i < n; i++)
  cout << arr[i] << " ";
 cout << endl;
 }
 
 // Driver program to test above functions
 int main()
 {
 char txt[] = "banana";
 int n = strlen(txt);
 int *suffixArr = buildSuffixArray(txt, n);
 cout << "Following is suffix array for " << txt << endl;
 printArr(suffixArr, n);
 return 0;
 }
 

Output

 Following is suffix array for banana
5 3 1 0 4 2

The output represents the suffix array of the string banana. Each number in the output array corresponds to the starting index of a suffix in the original string. The suffixes are sorted in lexicographical order.

In this case, the suffix array is [5, 3, 1, 0, 4, 2], which means the sorted suffixes are:

 "a"
"ana"
"anana"
"banana"
"na"
"nana"

Note that the suffix array is 0-based, so the indices start from 0.

Read More - DSA Interview Questions and Answers

Applications of Suffix Arrays

  1. Pattern Searching: Suffix arrays are commonly used in pattern searching algorithms, such as the longest common prefix (LCP) algorithm and the enhanced version of the Knuth-Morris-Pratt (KMP) algorithm. These algorithms use suffix arrays to quickly locate occurrences of a pattern within a text string.
  2. Text Compression: Suffix arrays can be employed in text compression algorithms, such as the Burrows-Wheeler Transform (BWT). The BWT rearranges the characters of a text string to enhance the compression ratio, and suffix arrays play a crucial role in constructing the BWT.
  3. Genome Assembly: In bioinformatics, suffix arrays are extensively used in genome assembly algorithms. These algorithms reconstruct the complete DNA sequence of an organism by aligning and merging smaller DNA fragments. Suffix arrays enable efficient matching and comparison of DNA sequences.
  4. Data Mining and Bioinformatics: Suffix arrays are employed in various data mining applications, including substring matching, similarity searches, and sequence alignment. They are particularly useful in bioinformatics for tasks like DNA sequence analysis, gene identification, and protein motif discovery.
  5. Longest Common Substring: Suffix arrays can be utilized to find the longest common substring among multiple strings. By using a combination of LCP arrays and binary search techniques on the suffix array, the longest common substring can be determined efficiently.
  6. Text Indexing: Suffix arrays are useful for creating text indexes, which enable rapid searches and retrieval of specific information within a large text corpus. They form the backbone of index data structures like the enhanced suffix array (ESA) and FM-index used in full-text search engines.
  7. Data Compression: Suffix arrays can be employed in data compression algorithms like the Lempel-Ziv-Welch (LZW) algorithm. Suffix arrays help in efficiently encoding and decoding sequences of symbols, resulting in reduced storage requirements and improved compression ratios

What is a Suffix Tree in Data Structures?

A suffix tree is a data structure that allows for quick and efficient pattern matching in a large body of text. It's a compressed trie that contains all of the text's suffixes. While this may sound complex, the suffix tree's power lies in its ability to speed up operations like text indexing and searching in a way that wasn't possible before.

In a trie data structure, each alphabet of all the strings in the given string is traversed one after another, and each node represents a single alphabet. The identical sub-string is represented by the same chain of nodes if two or more words begin with the same sub-string. Where the sub-string finishes and the unique suffix begins, the chain breaks. Each alphabet of the remaining suffix is then represented by its node.

Properties of a Suffix Tree in Data Structures

The suffix tree for the string S of length n is defined as a tree with the following properties:

  1. There are exactly n leaves on the tree, numbered 1 through n.
  2. A non-empty S substring identifies each edge.
  3. Every internal node has at least two children, except the root.
  4. String labels can't start with the same character on two edges that emerge from the same node.
  5. The suffix S[i..n], for I from 1 to n, is formed by combining all the string labels encountered on the path from the root to the leaf i.

Applications of Suffix Trees

  1. Pattern searching: Suffix trees are extensively used for efficient pattern matching and searching in strings. They enable fast substring queries and can find all occurrences of a pattern in a given text in linear time, making them highly efficient for applications like text editors, search engines, and DNA sequencing.
  2. Longest common substring: Suffix trees can efficiently find the longest common substring between multiple strings. This is particularly useful in bioinformatics for analyzing DNA and protein sequences to identify common regions or motifs.
  3. Text compression: Suffix trees play a crucial role in data compression algorithms like Burrows-Wheeler Transform (BWT) and the related algorithm used in the popular compression tool, gzip. Suffix trees help identify repeated substrings, which can be encoded more compactly.
  4. Data mining: Suffix trees are employed in data mining tasks, such as finding frequent patterns or motifs in large datasets. By constructing a suffix tree of a collection of strings, it becomes possible to efficiently identify common patterns, which can be useful in various applications like DNA analysis, web log analysis, and natural language processing.
  5. Bioinformatics and genomics: Suffix trees are extensively used in DNA sequencing and analysis, where they help identify repeated sequences, locate genes, analyze genomic rearrangements, and compare DNA sequences. They aid in tasks like genome assembly, sequence alignment, and gene finding.
  6. Computational linguistics: Suffix trees are valuable in natural language processing (NLP) tasks. They can be used for efficient keyword searching, text indexing, and analyzing linguistic structures. Applications include information retrieval, text summarization, spelling correction, and sentiment analysis.
  7. Data compression: Suffix trees are utilized in various lossless compression algorithms, including the Lempel-Ziv-Welch (LZW) compression algorithm. Suffix trees facilitate efficient encoding and decoding of data by constructing a dictionary of repeated substrings.
  8. Network analysis: Suffix trees have been employed for analyzing network data, such as detecting patterns in network traffic, identifying network motifs, and finding repeated substructures in network topologies
Summary

Thus, we have explored Suffix Array and Suffix Tree in data structures, two powerful tools for string text processing. Both methods are based on the idea of a suffix array of strings that contain all suffixes from a given text. We discussed that in the case of a Suffix Array, all substrings present in the target text need to be sorted, compared, and indexed which can be extremely time-consuming. On the other hand, in Suffix Tree much faster searches are possible since each node represents a substring that is already sorted.

FAQs

Q1. What are some of the applications of Suffix Arrays?

Pattern Searching, Longest Common Substring, Data Compression, Data Mining, Bioinformatics, etc.

Q2. How many children every internal node in a Suffix Tree has?

Every internal node has at least two children, except the root.

Q3. What is the most important characteristic of a Suffix Array?

It quickly finds the location of a substring within the original string.
Share Article
About Author
Amit Kumar Ghosh (SDE and Mentor)

A passionate professional with over 6 years of experience in development and training. He is passionate about learning new technologies and sharing his experience with professionals. He is an expert in C/C++, Java, Python and DSA.
Accept cookies & close this