http://blog.huang-wei.com/2010/07/15/double-array-trie%ef%bc%88%e5%8f%8c%e6%95%b0%e7%bb%84%e5%ad%97%e5%85%b8%e6%a0%91%ef%bc%89/
Trie在ACM中已经十分普及,也是一种非常有效的索引结构,好处就不多说了。
它的本质就是一个确定的有限状态自动机(DFA),关于它的实现也是有好几种,ACM中用的最多也是最容易实现的就是多路查找树。
但是Trie最大的缺点就是占用空间过大,很容易爆内存,当然在ACM里对Trie树也有相应的优化,如限定高度,对分支较少的节点使用非随机访问的结构(减少宽度),但这些都是牺牲部分查找效率换取的。
这里介绍一种实现,Double-Array Trie(双数组字典树),其实它就是双数组,跟树结构没啥关系。他能在一定程度上减少内存的浪费。
两个数组,一个是base[],另一个是check[]。设数组下标为i ,如果base[i], check[i]均为0,表示该位置为空。如果base[i]为负值,表示该状态为终止态(即词语)。check[i]表示该状态的前一状态。
定义 1. 对于输入字符 c, 从状态 s 转移到状态 t, 双数组字典树满足如下条件:

从定义1中,我们能得到查找算法,对于给定的状态 s 和输入字符 c :
插入的操作,假设以某字符开头的 base 值为i,第二个字符的字符序列码依次为c1, c2, c3…cn,则肯定要满足base[i+c1], check[i+c1], base[i+c2], check[i+c2], base[i+c3], check[i+c3]…base[i+cn], check[i+cn]均为0。
我们知道双数组的实现方法是当状态有新转移时才分配空间给新状态,或可以表述为只分配需要转移的状态的空间。当遇到无法满足上述条件时再进行调整,使得其 base 值满足上述条件,这种调整只影响当前节点下一层节点的重分配,因为所有节点的地址分配是靠 base 数组指定的起始下标所决定的。
我们先得到重分配算法:
Procedure Relocate(s : state; b : base_index) |
foreach input character c for the state s |
base[b + c] := base[base[s] + c]; |
foreach input character d for the node base[s] + c |
check[base[base[s] + c] + d] := b + c |
check[base[s] + c] := none |
如:有两个单词ac和da,先插入单词ac,这时的 base 数组

插入单词da的d时,发现该地址已被c占用,我们进行重分配

从上图可见a和d的位置重新分配了, base 值从0变成了1。
假设,Tire里有n个节点,字符集大小为m,则DATrie的空间大小是n+cm,c是依赖于Trie稀疏程度的一个系数。而多路查找树的空间大小是nm。
注意,这里的复杂度都是按离线算法(offline algorithm)计算的,即处理时已经得到整个词库。在线算法(online algorithm)的空间复杂度还和单词出现的顺序有关,越有序的单词顺序空间占用越小。
查找算法的复杂度和被查找的字符串长度相关的,这个复杂度和多路查找树是一样的。
插入算法中,如果出现重分配的情况,我们要附加上扫描子节点的时间复杂度,还有新base值确定的算法复杂度。假如这儿我们都是用暴力算法(for循环扫描),那插入算法时间复杂度是O(nm + cm2)。。
实际编码过程中,DATrie代码难度大过多路查找树,主要是状态的表示不如树结构那样的清晰,下标很容易搞混掉。
有个地方需要注意的是,base值正数表示起始偏移量,负数表示该状态为终止态,所以在查找新base值时,要保证查到的值是正数。
如:空Trie状态下,插入d时,因为第一个空地址是1,所以得到base=1-4=-3,这样base正负的含义就被破坏了。
关于优化:
- 查找空地址优化
base[i], check[i]均为0,表示该位置为空。我们可以把这部分给利用起来,全为0的标记所包含的信息实在太少了。我们利用base和check数组组成一个双向链表。
定义 2. 设 r1, r2, … ,rcm 为空闲地址有序序列,则我们的双向链表可定义为 :
check[r[i]] = -r[i+1] ; 1 <= i <= cm- 1 |
base[r[i+ 1 ]] = -r[i] ; 1 <= i <= cm- 1 |
由于我们把base[0]作为根节点,所以初始化时就可以把base[0]排除在链表之外,而check[0]可以被作为链表的头节点。
设节点的状态转移集为P = {c1, c2, …, cp},依靠链表我们能得到新的空地址查找算法:
while s <> 0 and s <= c[ 1 ] do |
if s = 0 then return FAIL; |
while i <= p and check[s + c[i] - c[ 1 ]] < 0 do |
if i = p + 1 then return s - c[ 1 ]; |
优化后的空地址查找算法时间复杂度为O(cm2),而重分配算法的时间复杂度为O(m2),则总的时间复杂度为O(cm2 + m2) = O(cm2)。
重分配或删除节点后,原先的地址被作废,可以重新加入链表,这样如果遇到原状态转移集的子集时,就可以派上用场了。
其实这部分的优化就是使用了闲置信息域做成了链表,所以这部分的插入和删除优化原理就很容易理解了,时间复杂度为O(cm)。
while check[t] <> 0 and t < s do |
- 数组长度的压缩
当有节点删除时,我们不仅可以把它加回到链表中,还可以重新为最大非空节点的状态重新确定base值,因为删除可能导致它的前面有空间容纳下它的状态转移集。这样我们可能又得以删除一些空值状态,使得数组长度有希望被压缩。
- 字符后缀的压缩
这个思想借鉴于后缀树,我们可以将没有分支的后缀单独存放,但这个结构肯定独立于DATrie,所以在这就不详述了。详情见[Aoe1989]。
整体而言,在ACM中,DATrie略高的编码复杂度和过低的插入效率,应用面不会太广。但现实问题中,词库大小一般比较稳定,在离线算法也有很大的优化余地,此时DATrie的空间优势就会比较明显。毕竟Trie高效的检索效率这一优点是值得研究探讨的。
这篇日志写的够长了,等有空再把DATrie测试报告给整理下吧。唉,发现自己语言组织能力越来越差了,写的连自己有时都看不下去,要多坚持记下技术日志了~~
以下是只加入空地址查找优化后的DATrie代码,对于字符集表的映射结构也是个需要持续讨论的问题,在这个代码里只支持英文字母。
1
#define ALPHASIZE 30
2
#define MAX 10000
3
#define ALPHAID(x) (1+x-'a')
4
#define IDALPHA(x) (x-1+'a')
5
#define EMPTY(x) (basei[x] < 0 && check[x] < 0)
6
#define DELETE_FREE_NODE(x) check[-basei[x]] = check[x]; \
7
basei[-check[x]] = basei[x]; \
8
maxsize = max(maxsize, x)
9
#define ADD_FREE_NODE(x) basei[x] = MAX; \
10
check[x] = MAX; \
11
abnodes ++
12
class DATire
13

{
14
public:
15
void init()
{
16
// double circular linked list (except 0)
17
for (int i = 1; i < MAX; i ++)
{
18
check[i] = -(i+1);
19
basei[i] = -(i-1);
20
}
21
basei[1] = 0; // so check[0] can be updated
22
check[MAX-1] = 1;
23
// basei[0] is root-index
24
// check[0] point to first free cell
25
basei[0] = 0;
26
check[0] = -1;
27
// stat
28
diffwords = 0;
29
maxsize = 0;
30
nodes = 1;
31
abnodes = 0;
32
}
33
void print(int s, char* buf, int d)
{
34
if (basei[s] < 0)
35
puts(buf);
36
int si = abs(basei[s]);
37
for (int i = si+1; i <= si + ALPHASIZE; i ++)
{
38
if (check[i] == s)
{
39
buf[d] = IDALPHA(i-si); buf[d+1] = '\0';
40
print(i, buf, d+1);
41
buf[d] = '\0';
42
}
43
}
44
}
45
bool insert(string word)
{
46
int s = 0, t;
47
for (int i = 0; i < word.length(); i ++)
{
48
char ch = word[i];
49
t = abs(basei[s]) + ALPHAID(ch);
50
if (s == check[t])
51
s = t;
52
else if (EMPTY(t))
{
53
DELETE_FREE_NODE(t);
54
basei[t] = t;
55
check[t] = s;
56
s = t;
57
nodes ++;
58
}
59
else
{
60
int newb = findb(s, ALPHAID(ch));
61
if (newb == -1)
62
return false;
63
else
{
64
relocate(s, newb);
65
i --;
66
}
67
}
68
}
69
if (basei[s] > 0)
70
diffwords ++;
71
basei[s] = -abs(basei[s]);
72
return true;
73
}
74
bool find(string word)
{
75
int s = 0, t;
76
int i;
77
for (i = 0; i < word.length(); i ++)
{
78
char ch = word[i];
79
t = abs(basei[s]) + ALPHAID(ch);
80
if (s == check[t])
81
s = t;
82
else
83
break;
84
}
85
return (i == word.length() && basei[s] < 0);
86
}
87
protected:
88
int findb(int s, int newc)
{
89
ns = 0;
90
int i, j;
91
int si = abs(basei[s]);
92
for (i = si+1; i <= si + ALPHASIZE; i ++)
{
93
if (check[i] == s)
94
sonc[ns ++] = i - si;
95
}
96
sonc[ns ++] = newc;
97
int minson = min(sonc[0], newc);
98
// i < si, the new place must be after old place
99
// i < minson, the negative base value has other meaning
100
for (i = -check[0]; i != 0 && (i < si || i < minson); i = -check[i]) ;
101
for (; i != 0; i = -check[i])
{
102
for (j = 0; j < ns; j ++)
{
103
if (! EMPTY(i + sonc[j] - minson))
104
break;
105
}
106
if (j == ns)
{
107
ns --;
108
assert(i - minson >= 0);
109
return i - minson;
110
}
111
}
112
return -1;
113
}
114
void relocate(int s, int b)
{
115
for (int i = ns-1; i >= 0; i --)
{
116
int news = b + sonc[i];
117
int olds = abs(basei[s]) + sonc[i];
118
DELETE_FREE_NODE(news);
119
check[news] = s;
120
basei[news] = basei[olds];
121
int isi = abs(basei[olds]);
122
for (int j = isi+1; j <= isi + ALPHASIZE; j ++)
{
123
if (check[j] == olds)
124
check[j] = news;
125
}
126
ADD_FREE_NODE(olds);
127
}
128
basei[s] = (basei[s] < 0 ? -1 : 1) * b;
129
}
130
protected:
131
int basei[MAX];
132
int check[MAX];
133
// helper
134
int sonc[ALPHASIZE];
135
int ns;
136
public:
137
// stat
138
int maxsize; // used memory size
139
int nodes; // trie nodes
140
int abnodes; // abandoned trie nodes
141
int diffwords; // diff words
142
// free nodes = maxsize-nodes-abnodes
143
};