15-211 fundamental data structures and algorithms (spring ’05)

14
15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Tries Slides prepared by Uri Dekel, udekel@cs Based on recitation notes by Will Haines

Upload: efrat

Post on 05-Jan-2016

28 views

Category:

Documents


1 download

DESCRIPTION

15-211 Fundamental Data Structures and Algorithms (Spring ’05). Recitation Notes: Tries Slides prepared by Uri Dekel, udekel@cs Based on recitation notes by Will Haines. Radix Sort. A bucket-based sorting algorithm Input: elements with digits in longest key Algorithm: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Fundamental Data Structures and Algorithms (Spring ’05)Recitation Notes: Tries

Slides prepared by Uri Dekel, udekel@cs

Based on recitation notes by Will Haines

Page 2: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

2

Radix Sort

A bucket-based sorting algorithm Input: elements with digits in longest key Algorithm:

From least-significant digit to most significant Place into bins by current digit while preserving

original order (“stable sort”) Append bins into a sequence

Sort time complexity:

n

n

Page 3: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

3

Radix sort example

Input: {115, 364,112, 003, 087, 006, 091, 911}

Phase 1: {091, 911, 112, 003, 364, 115, 006, 087}

Phase 2: {003, 006, 911, 112, 115, 364, 087, 091}

Phase 3: {003, 006, 087, 091, 112, 115, 364, 911}

Page 4: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

4

Tries

Based on the same principle as radix sort Create an -level tree Support search in

O

Page 5: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

5

Multiway Tries with values at predefined depth Every internal node has |A| pointers

|A| is the size of the “alphabet” |A|=10 for decimal digits 0..9 |A|=26 for the English alphabet

The -th level nodes point to leafs that contain actual value of key

Limitations: All keys must have exactly digits

Search time is One key cannot be a prefix of another key

Significant time/space tradeoff Lots of wasted space but the tree is very shallow

Page 6: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

6

Multiway Tries with values at unknown depth Allows searching for -digit key in

Even if longest key is digits Add one “terminal” signal cell to each node

Indicates that the key entered so far is a member of the collection

Allows us to go on searching for longer key

k k

Page 7: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

7

Digital Trie example

Input sequence: {0, 2, 10, 011, 01, 20, 21} We use three digit buckets and one for signal

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 8: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

8

Digital Trie example

Search for “2”: Hit!

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 9: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

9

Digital Trie example

Search for “01”: Hit!

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 10: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

10

Digital Trie example

Search for “011”: Hit!

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 11: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

11

Digital Trie example

Search for “1”: Miss (within trie)

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 12: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

12

Digital Trie example

Search for “22”: Miss (runs off of tree)

0 1 2 T

0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

X0 1 2 T

Page 13: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

13

Hashmap Tries

What if we don’t know the digits of our alphabet? e.g., in HW2 we will have individual words as

“digits” of the search tree We will associate them with final words to complete

phrases

We use a hashmap at each node Amortized constant lookup at each node Amortized constant insert at each node

Page 14: 15-211  Fundamental Data Structures and Algorithms (Spring ’05)

15-211 Recitation notes on tries, Uri Dekel and Will Haines

14

Hashmap Trie Example

Store frequency of all 3-word sequence Input sentence: “in at the ox at the in the in at ox

at ox in the at the in at in ox the ox at the ox”AT IN OX THE

IN OX THE AT OX THE AT IN THE AT IN OX

OX:1AT:1IN:1

IN:2OX:2

IN:1OX:1

THE:1

THE:1AT:1IN:1

THE:2OX:1

THE:1 OX:1 THE:1THE:1AT:2

AT:2