Symbol Table Applications
By: Omar Elgabry
Tags: Programming, Coding, Algorithms, Data Structures, Java

Sets

They are just collection of distinct keys. There is no values associated; the keys themselves are values at the same time.

The operations are: add a key, ask if the set contains a key, or remove a key.

Implementation

Sets are commonly implemented in the same way as associative arrays, either using:

A self-balancing binary search tree for sorted sets (which has O(LogN) for most operations).

A hash table for unsorted sets (which has O(1) average-case, but O(N) worst-case, for most operations).

Therefore, sets can be implemented using associative arrays, but, we either:

Remove all references to “value” field from any symbol table implementation.

Use a dummy value as the values, which is ignored.

Applications

Spell Checker : Store words in a dictionary, identify misspelled words.

: Store words in a dictionary, identify misspelled words. Browser : Store visited pages, mark the current page if visited not.

: Store visited pages, mark the current page if visited not. Spam Filter: Store spam IP addresses, eliminate IP address if it’s a spam.

All of these applications are “Exception filter” application; meaning, they store either a whitelist or blacklist of keys, then check if a key exists or not.

public class WhiteList {

public static void main(String[] args) {

SET<String> set = new SET<String>();

String[] whitelist = {/*...*/};

String[] input = {/*...*/};



for(String word: whitelist) // store whitelist words

set.add(word);

for(String word: input) // print whitelist words in input

if (set.contains(word))

System.out.println(word);

}

}

Dictionary Clients

It’s an application of symbol tables, where we find the value (if any) that’s mapped to a given key in a list of key-value pairs.

Applications

DNS Lookup : Store a list of URL and it’s associated IP address in symbol table. The key can be the URL, and value is the IP address, or vice-versa.

: Store a list of URL and it’s associated IP address in symbol table. The key can be the URL, and value is the IP address, or vice-versa. Student Lookup: Store a list of students data, where key can be for example their id, and first name is the value.

Indexing Clients

It’s same as Dictionary Clients. But, this time, the value is a list of values, instead of only one value.

Applications

File Indexing: Store words in all files, then tell me which files contain a given query string. The key is the word, and the value is a set of files containing this word.

import java.io.File;

public class FileIndex {

public static void main(String[] args) {

// The value can have type of "Set";

// a collection of distinct items.

ST<String, SET<File>> st = new ST<String, SET<File>>();

String files[] = {/*...*/};



// for each word in file, add to corresponding set

for (String filename : files) {

File file = new File(filename);

String words[] = file.getWords();

for(String word: words) {

if (!st.contains(word))

st.put(word, new SET<File>());

SET<File> set = st.get(word);

set.add(file);

}

} // get list of files for each word

String input[] = {/*...*/};

for(String word: input)

System.out.println(st.get(word));

}

}

Book Index: Store words in an article, then tell me the position(s) where a word appeared in the article. The key is the word, and the value is a set of indexes where the word appeared in the article.

Sparse Vectors

The matrix-vector multiplication; multiplying matrix by a vector using the brute force solution takes O(N²).

And, if the matrix(or the vector) is sparse, meaning, most of the elements are zero, which is the case in many applications, then using associative arrays can get better performance.

We assume if matrix dimension is 10⁴, then non-zeros per row ~ 10 on average.

Vector Representations

Although using a 1d array takes a constant time to access an element, but, it might consume a lot of wasted space if most of the elements are zeros; Sparse Vector. In addition, iterating over the elements would take a linear time in the worst case.

So, using associative arrays instead can save time and space (both are ~= non-zero keys).

Vector Representations — algs4.cs.princeton.edu

Thus, we can make a data type called “SparseVector”, which uses the symbol table to represent a sparse vector (a vector with a lot of zeros), and provides extra methods for mathematical operations, such as finding dot product.

Matrix Representations

A matrix can also have two representations; 2d array, or an array of associative arrays, or SparseVectors.

Using associative arrays save time and space since most of the elements are zeros (both are ~= non-zero keys in each row, and plus N for space).

Matrix Representations — algs4.cs.princeton.edu

Sparse Matrix-vector Multiplication

The number of iterations equals to non-zero keys in each row (constant for each row). Therefore, using associative arrays can get linear running time for sparse matrix.