You are on page 1of 11

TRIES

• Standard Tries • Compressed Tries • Suffix Tries

b e a r l l i d l l u y e l l

s t o c k p

Tries

1

• Tradeoffs in text searching Preprocess Pattern Brute Force Boyer Moore Suffix Trie O(m+d) O(n) Preprocess Text Space O(1) O(d) O(n) Search Time O(mn) O(n) * O(m) n = text size m = pattern size * on average Tries 2 .. immutable and searched for often (e. we may want to preprocess the text instead of the pattern in order to perform pattern matching queries in time proportional to the pattern length. works by Shakespeare).Text Processing • We have seen that preprocessing the pattern speeds up pattern matching queries • After preprocessing the pattern in time proportional to the pattern length.g. the Boyer-Moore algorithm searches an arbitrary English text in (average) time proportional to the text length • If the text is large.

stock. bull. bell.n = total size of the strings in S. buy.Standard Tries • The standard trie for a set of strings S is an ordered tree such that: .each node but the root is labeled with a character . remove) take time O(dm) each. . stop } b e a r l l i d l l u y e l l s t o c k p • A standard trie uses O(n) space.the paths from the external nodes to the root yield the strings of S • Example: standard trie for the set of strings S = { bear. bid. where: . Tries 3 . Operations (find.m =size of the string parameter of the operation .the children of a node are alphabetically ordered . insert. sell.d =alphabet size.

58 a r 69 o c k 17. 40. 24 i l l 78 e y 36 t l l 12 d 47. where m = |X| .prefix matching: find the first occurrence of the longest prefix of word X in the text • Each operation is performed by tracing a path in the trie starting at the root s 0 e 1 e 2 3 a 4 5 b e 6 7 a 8 r l ! ? ? b l s e l l s s s t t s t o o t o c p o c k ! c k ! k ! ! 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 s b e i e d a r a s t t b u o c l k b u y i l d ? 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 h e h e b e 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 b e a r 6 h u l l 30 s e e 0. 51.Applications of Tries • A standard trie supports the following operations on a preprocessed text in time O(m).word matching: find the first occurence of word X in the text . 62 p 84 Tries 4 .

Compressed Tries • Trie with nodes of degree at least 2 • Obtained from standard trie by compressing chains of redundant nodes • Standard Trie: b e a r l l i d l l u y e l l c k s t o p • Compressed Trie: b e ar Tries s u ll y ell ck to p 5 id ll .

2 0. 3. 1 1. 2. 0 1. 0 3 0. 1 5. 3. 2. 0. 1 7. 2 2.Compact Storage of Compressed Tries • A compressed trie can be stored in space O(s). where s = |S|. 3 8. 2. 2. 3 4. 3 6. 1. 1. 1. 1. 0 3. 1. 3 3. 2. 2 0. 0. 2 4. 2. 3 b e ar Tries s u ll y ell ck to p 6 id ll . by using O(1) space index ranges at the nodes 0 1 2 3 4 0 1 2 3 0 1 2 3 S[0] = S[1] = S[2] = S[3] = s e e a l o r l c k S[4] = S[5] = S[6] = b u l l S[7] = S[8] = S[9] = h e b e s t a l o r l p b e s s e t b u y b i d 1. 4 9.

Insertion and Deletion into/from a Compressed Trie a abab 1 b abbb 3 baab 2 b aaa 4 bab 5 search stops here insert(bbaabb) a abab 1 b abbb 3 baab 2 b aa a 4 bab 5 bb 6 Tries 7 .

7 6. 1 2.Suffix Tries • A suffix trie is a compressed trie for all the suffixes of a text • Example m i 0 1 n 2 i m i 3 4 5 z 6 e 7 e i mi nimize ze mize nimize ze nimize ze • Compact representation: 7. 7 Tries 2. 7 6. 1 0. 7 4. 7 1. 7 8 . 7 2. 7 6.

7 1. 7 6. 7 4. 1 0. 7 2.supports arbitrary pattern matching and prefix matching queries in O(dm) time.can be constructed in O(dn) time m i 0 1 n 2 i m i 3 4 5 z 6 e 7 7. 7 2. 7 Tries 9 . 7 6. 7 6. where m is the length of the pattern .stores all the n(n−1)/2 suffixes of X in O(n) space . 1 2.Properties of Suffix Tries • The suffix trie for a text X of size n from an alphabet of size d .

such as ..g. Java and coffee) correspond to set operations (e.g. identify “add” “adding” “added”) .stopword elimination (e. ignore “the” “a” “is”) .g... take CS 295-3 Tries 10 . intersection) on the occurrence lists • Additional information retrieval techniques are used. called occurrence list • The trie is kept in internal memory • The occurrence lists are kept in external memory and are ranked by relevance • Boolean queries for sets of words (e.Tries and Web Search Engines • The index of a search engine (collection of all searchable words) is stored into a compressed trie • Each leaf of the trie is associated with a word and has a list of pages (URLs) containing that word.link analysis (recognize authoritative pages) • For this and more .stemming (e..g...

a packet with IP prefix 128.148. Each packet carries the IP address of its destination. • The internet whose nodes are routers.g.. and whose edges are communication links. • Routers use tries on the alphabet 0.brown.110 • Use nslookup on Unix to find out IP addresses • An organization uses a subset of IP addresses with the same prefix. should be forwarded to the Brown gateway router. usually written in “dotted-quad-decimal” notation • E. Brown uses 128.g.1 to do prefix matching. www. take CS 196-5 Tries 11 .edu is 128.g.*.cs. • To learn more.*. • A router forwards packets to its neighbors using IP prefix matching rules. Yale uses 130.. E.32..*.132.148. e.* • Data is sent to a host by fragmenting it into packets.148.Tries and Internet Routers • Computers on the internet (hosts) are identified by a unique 32-bit IP (internet protocol) addres.