/
Lecture 19 Tries Linked List Lecture 19 Tries Linked List

Lecture 19 Tries Linked List - PowerPoint Presentation

cleverfan
cleverfan . @cleverfan
Follow
344 views
Uploaded On 2020-06-24

Lecture 19 Tries Linked List - PPT Presentation

Stack Queue Heap Priority Queue Hash Table BST AVL Multiway Red Black Algorithm Analysis Tools Is finding a pattern same as finding a word in the dictionary How fast can you search for a word in a set of words ID: 785862

nodes trie compressed node trie nodes node compressed suffix ize bxa minimize insert nimize word inimize number strings root

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Lecture 19 Tries Linked List" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Lecture 19

Tries

Slide2

Linked List

Stack

Queue

Heap

Priority Queue

Hash Table

BST

AVL

Multiway

Red-

Black

Algorithm Analysis Tools

Slide3

Is finding a pattern same as finding a word in the dictionary?

Slide4

How fast can you search for a word in a set of words?

Slide5

How to make ordered tree for strings?

Slide6

Standard Tries

Except root, each node is labeled with a characterThe children of a node are alphabetically orderedThe path from root to external nodes yields the string

Slide7

S = { bear, bell, bid, bull, buy, sell, stock, stop }

Slide8

Insert

Assumption: no word is prefix of other word!What if this is not the case?

Slide9

Time complexity

n total size of the strings in Sm size of given string d size of the alphabet

s number of words

Slide10

Search?

O(m)

Slide11

Insert

& delete?O(md)

Slide12

Word Matching with a

Trie

s

e

e

b

e

a

r

?

s

e

l

l

s

t

o

c

k

!

s

e

e

b

u

l

l

?

b

u

y

s

t

o

c

k

!

b

i

d

s

t

o

c

k

!

a

a

h

e

t

h

e

b

e

l

l

?

s

t

o

p

!

b

i

d

s

t

o

c

k

!

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

a

r

87

88

a

e

b

l

s

u

l

e

t

e

0, 24

o

c

i

l

r

6

l

78

d

47, 58

l

30

y

36

l

12

k

17, 40,

51, 62

p

84

h

e

r

69

a

Slide13

How many nodes do we need?

Can we reduce the number of nodes?

Slide14

Compressed Tries

Slide15

Compressed Tries

A compressed trie has internal nodes of degree at least twoIt is obtained from standard trie by compressing chains of “redundant” nodes

A redundant node is the one that has only one child

Slide16

How many nodes do I have in a compressed

Trie?

Slide17

Compressed

Trie PropertiesEvery internal node of T has at least two children and at most d childrenT has s leaf nodesThe number of internal nodes of T is O(s)

Slide18

1,0,0

1,1,1

4

,1,1

0

,1,1

3

,1,2

0

,0,0

7,0,3

6,1,2

1,2,3

8,2,3

4,2,3

5,2,2

0,2,2

2,2,3

3,3,4

9,3,3

hear

e

ll

e

Slide19

Compact Representation of Tries

Stores at the nodes ranges of indices instead of substringsUses O(s) space, where s is the number of strings in the arrayServes as an auxiliary index structure

Slide20

Insertion/Deletion in Compressed

Trie

f

g

reat

one

u

or

n

r

insert green

f

g

reat

one

u

or

n

r

insert

gre

en

search ends

here

f

g

re

one

u

or

n

r

at

en

Slide21

Suffix

Trie

7

3

1

5

0

4

2

6

Slide22

Suffix

TrieNot all strings are guaranteed to have corresponding suffix trie.For example: xabxa

a

bxa

bxa

xa

bxa

a

xa

bxa

bxa

bxa

$

$

Slide23

Pattern Matching Using Suffix

Trie

Compressed Trie

Auxiliary Trie

Slide24

How much space do we need to make a suffix

trie of n character string?

Slide25

To

Trie or Not to Trie?

What would you use to store phonebook?

Slide26

Constructing a Suffix

TrieS[1..n] is the stringStart with a single edge for SEnter

the edges for the suffix S[i..n] where i goes from 2 to nStarting at the root node find the longest part from the root whose label matches a prefix of S[i..n]. At some point, no further matches are possibleIf the point is at a node, then denote this node by wIf it is in the middle of an edge, then insert a new node called w, at this pointCreate

a new edge running from w to a new leaf labeled S[i..n]

Slide27

Example

minimize

minimize

m

inimize

minimize

inimize

m

i

nimize

m

in

imize

minimize

inimize

nimize

minimize

nimize

m

ize

nim

ize

i

Slide28

Example

m

in

imize

minimize

nimize

m

ize

nim

ize

i

m

ini

mize

nimize

m

ize

nim

ize

i

inimize

ize

m

Slide29

Example

7

3

1

5

0

4

2

6