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

Fundamental data structures such as red/black trees, B-trees, RMQ, etc. and amortized analysis. It also covers string processing, which is used in computational biology, NLP, and computer security. the concept of tries, formally defining it and discussing how to insert and remove from it. It also covers space concerns and the naive solution for string searching. The document then goes on to discuss multi-string searching and the algorithm for it.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

hugger
hugger 🇺🇸

4.8

(12)

916 documents

1 / 65

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tries and String Matching
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41

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?