Docsity

Tries and String Matching, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Tries and String Matching and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Tries and String Matching

Where We've Been

●

Fundamental Data Structures

●

Red/black trees, B-trees, RMQ, etc.

●

Isometries

●

Red/black trees ≡ 2-3-4 trees, binomial

heaps ≡ binary numbers, etc.

●

Amortized Analysis

●

Aggregate, banker's, and potential methods.

String Data Structures

Text Processing

●

String processing shows up everywhere:

● Computational biology: Manipulating DNA
sequences.
● NLP: Storing and organizing huge text databases.

●

Computer security: Building antivirus databases.

●

Many problems have polynomial-time solutions.

●

Goal: Design theoretically and practically

efficient algorithms that outperform brute-force

approaches.

Tries

Ordered Dictionaries

● Suppose we want to store a set of elements supporting the following operations: ● Insertion of new elements. ● (^) Deletion of old elements. ● Membership queries. ● (^) Successor queries. ● (^) Predecessor queries. ● Min/max queries. ● Can use a standard red/black tree or splay tree to get (worst-case or expected) O(log n ) implementations of each.

B D O U T G E A I O A E R D N D T A A I D I K T A T N T E K B C A D

Tries

●

The data structure we have just seen is

called a trie.

●

Comes from the word re trie val.

●

Pronounced “try,” not “tree.”

●

Because... that's totally how “retrieval” is

pronounced... I guess?

Trie Efficiency

●

What is the cost of looking up a string w in a

trie?

●

Follow at most | w | pointers to get to the node

for w , if it exists.

●

Each pointer can be looked up in time O(1).

●

Total time: O(| w |).

●

Lookup time is independent of the number

of strings in the trie!

B D O U T G E A I O A E R D N D T A A I D I K T A T N T E K B C A D

Removing from a Trie

●

Mark the node as no longer containing a

word.

●

If the node has no children:

● Remove that node.

●

Repeat this process at the node one level

higher up in the tree.

Space Concerns

● Although time-efficient, tries can be extremely space-inefficient. ● A trie with N nodes will need space Θ( N · |Σ|) due to the pointers in each node. ● There are many ways of addressing this: ● Change the data structure for holding the pointers (as you'll see in the problem set). ● Eliminate unnecessary trie nodes (we'll see this next time).

String Matching

●

The string matching problem is the following:

Given a text string T and a nonempty string P ,

find all occurrences of P in T.

●

(Why must P be nonempty?)

●

T is typically called the text and P is the

pattern.

●

We're looking for an exact match; P doesn't

contain any wildcards, for example.

●

How efficiently can we solve this problem?

The Naïve Solution

● Consider the following naïve solution: for every possible starting position for P in T , check whether the | P | characters starting at that point exactly match P. ● Work per check: O(| P |) ● Number of starting locations: O(| T |) ● Total runtime: O(| P| · | T |). ● Is this a tight bound?