1.前言
有关Trie树求多个字符串的最短编辑距离的问题,请参见:利用Trie树求多个字符串的最短编辑距离
本文为上文的续篇,在空间上对算法进行优化。为了充分利用CPU的高速缓存机制和减少寻址时间,在创建Trie树的新节点时,不是每次都new一个新节点,而是提前预先分配一个大的数组,数组里面有足够的节点空间。这样Trie树的节点空间就连续了。指向树的各个节点的指针在跳转时寻址时间应该比以前要快一些,而且这样还可以增加缓存的命中率,因此势必对整体的性能提升有帮助。
2.测试结果
(1)优化前的结果
序号 | 建树时间 | Trie树搜索时间 | 暴力匹配搜索时间 | 时间比值 |
1 | 14726 | 56 | 812 | 6.90% |
2 | 14999 | 72 | 810 | 8.89% |
3 | 15125 | 79 | 825 | 9.58% |
4 | 14642 | 54 | 809 | 6.67% |
5 | 14709 | 72 | 813 | 8.86% |
6 | 14734 | 70 | 831 | 8.42% |
7 | 14649 | 39 | 816 | 4.78% |
8 | 14694 | 54 | 817 | 6.61% |
9 | 14998 | 75 | 810 | 9.26% |
10 | 14644 | 73 | 810 | 9.01% |
平均值 | 14792 | 64.4 | 815.3 | 7.90% |
(2)优化后的结果
序号 | 建树时间 | Trie树搜索时间 | 暴力匹配搜索时间 | 时间比值 |
1 | 14732 | 77 | 818 | 9.41% |
2 | 14710 | 77 | 804 | 9.58% |
3 | 15103 | 39 | 815 | 4.79% |
4 | 14575 | 69 | 809 | 8.53% |
5 | 14625 | 59 | 811 | 7.27% |
6 | 14660 | 59 | 807 | 7.31% |
7 | 15099 | 38 | 821 | 4.63% |
8 | 14696 | 54 | 807 | 6.69% |
9 | 14626 | 76 | 809 | 9.39% |
10 | 14653 | 68 | 805 | 8.45% |
平均值 | 14747.9 | 61.6 | 810.6 | 7.61% |
(3)结果对比
序号 | 建树时间 | Trie树搜索时间 | 暴力匹配搜索时间 | 时间比值 |
优化前 | 14792 | 64.4 | 815.3 | 7.90% |
优化后 | 14747.9 | 61.6 | 810.6 | 7.61% |
优化幅度 | 0.30% | 4.35% | 0.58% | — |
从对比中可以看出,Trie树的建树时间略快一些但是很不明显,Trie树的搜索速度提升了4.35%。
3.源代码
优化后的源代码如下:
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <sys/time.h>
using namespace std;
const int X = 30;
const int Y = 30;
const int MAX = 30;
const int MAX_NODE = 100000;
int edit_length(string &x, string &y);
//-----------------------Trie树的节点定义--------------------------
class Node{
public:
int length;
string word;
Node* left;
Node* right;
public:
Node() : length(0), word(""){
left == NULL;
right == NULL;
}
};
//-----------------------Trie树的操作定义--------------------------
//Trie树的操作定义
class Trie{
private:
Node* pRoot; //根节点
Node* pArray; //一次性分配的空间指针
Node* pPos; //数组的游标
private:
void destory(Node* r);
void find(Node *pRoot, string &str, int limit_num, vector<string> &word_set);
public:
Trie();
~Trie();
void insert(string str);
void search(string &str, int limit_num, vector<string> &word_set);
};
Trie::Trie(){
pArray = new Node[MAX_NODE];
pPos = pArray;
}
Trie::~Trie(){
destory(pRoot);
delete [] pArray;
pPos == NULL;
pArray == NULL;
}
//销毁Trie树
void Trie::destory(Node* pRoot){
if(pRoot == NULL){
return;
}
destory(pRoot -> left);
destory(pRoot -> right);
pRoot = NULL;
}
//插入单词,建立Trie树
void Trie::insert(string str){
if(pRoot != NULL){
//如果trie树已经存在
Node *pPre = pRoot;
Node *pCur = pRoot -> left;
while(1) {
//计算该单词与当前节点的编辑距离
string word = pPre -> word;
int distance = edit_length(word, str);
//若该单词已存在
if(distance == 0) {
break;
}
//若该单词不存在
if(pCur == NULL) {
//若首节点不存在,则创建首节点
pCur = pPos;
pPos++;
pCur -> length = distance;
pCur -> word = str;
pCur -> left = NULL;
pCur -> right = NULL;
pPre -> left = pCur;
break;
} else if (pCur != NULL && pCur -> length > distance) {
//若首节点存在,并且首节点大于目标编辑距离,重建首节点
Node *p = pPos;
pPos++;
p -> length = distance;
p -> word = str;
p -> left = NULL;
p -> right = pCur;
pPre -> left = p;
break;
} else {
//首节点存在,且首节点小于等于目标编辑距离
while(pCur != NULL && pCur -> length < distance){
pPre = pCur;
pCur = pCur -> right;
}
if(pCur != NULL && pCur -> length == distance){
//找到了目标节点
pPre = pCur;
pCur = pCur -> left;
} else {
//创建目标节点
Node *p = pPos;
pPos++;
p -> length = distance;
p -> word = str;
p -> left = NULL;
p -> right = pCur;
pPre -> right = p;
break;
}
}
}
} else {
//如果Trie树还不存在,以该单词创建根节点
pRoot = pPos;
pPos++;
pRoot -> length = 0;
pRoot -> word = str;
}
}
//搜索与给定字符串的编辑距离小于给定值的所有字符串(内部调用)
void Trie::find(Node* pRoot, string &str, int limit_num, vector<string> &word_set){
if(pRoot == NULL){
cout << "kong" << endl;
return;
}
string word = pRoot -> word;
int distance = edit_length(word, str);
if(distance < limit_num) {
word_set.push_back(word);
}
//如果当前节点有孩子的话
Node *pCur = pRoot -> left;
while(pCur != NULL){
if(pCur -> length < distance + limit_num &&
pCur -> length > distance - limit_num &&
pCur -> length > limit_num - distance){
find(pCur, str, limit_num, word_set);
}
pCur = pCur -> right;
}
}
//包装函数,搜索与给定字符串的编辑距离小于给定值的所有字符串(外部调用)
void Trie::search(string &str, int limit_num, vector<string> &word_set){
find(pRoot, str, limit_num, word_set);
}
//---------------------------工具函数------------------------------
//求两个字符串的最断编辑距离
int edit_length(string &x, string &y){
int xlen = x.length();
int ylen = y.length();
int edit[3][Y+1];
memset(edit, 0, sizeof(edit));
int i = 0;
int j = 0;
for(j = 0; j <= ylen; j++){
edit[0][j] = j;
}
for(i = 1; i <= xlen; i++){
edit[i%3][0] = edit[(i-1)%3][0] + 1;
for(j = 1; j <= ylen; j++){
if (x[i-1] == y[j-1]) {
edit[i%3][j] = min(min(edit[i%3][j-1] + 1, edit[(i-1)%3][j] + 1),
edit[(i-1)%3][j-1]);
} else {
if(i >= 2 && j >= 2 && x[i-2] == y[j-1] && x[i-1] == y[j-2]){
edit[i%3][j] = min(min(edit[i%3][j-1] + 1, edit[(i-1)%3][j] + 1),
min(edit[(i-1)%3][j-1] + 1, edit[(i-2)%3][j-2] + 1));
} else {
edit[i%3][j] = min(min(edit[i%3][j-1] + 1, edit[(i-1)%3][j] + 1),
edit[(i-1)%3][j-1] + 1);
}
}
}
}
return edit[(i-1)%3][j-1];
}
//生成随机字符串
string rand_string(int len){
srand(time(NULL));
char a[MAX+1];
for(int i = 0; i < len; i++){
a[i] = rand()%26 + 'a';
}
a[len] = '\0';
string str(a);
return str;
}
//获取当前时间(ms)
long getCurrentTime(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
//-----------------------------测试函数------------------------
//测试最短编辑距离函数
void Test_1(){
string a = "abcdef";
string b = "abcdef";
int max_len = edit_length(a, b);
cout << max_len << endl;
}
//验证Trie树是否完整
void Test_2(){
//1.创建对象,打开文件
Trie trie;
string str;
ifstream fin;
fin.open("dict.txt");
if(!fin){
cout << "打开文件失败!" << endl;
}
//2.建立Trie树
while(getline(fin, str, '\n')){
trie.insert(str);
}
fin.close();
//3.验证Trie树的正确性
fin.open("dict.txt");
if(!fin){
cout << "打开文件失败!" << endl;
}
while(getline(fin, str, '\n')){
int count = 0;
vector<string> word_set;
trie.search(str, 1, word_set);
cout << word_set.size() << " " << str << endl;
}
}
//测试对于随机字符串搜索结果的正确性
void Test_3(){
//1.创建对象,打开文件
Trie trie;
string str;
ifstream fin;
fin.open("dict.txt");
if(!fin){
cout << "打开文件失败!" << endl;
}
//2.建立Trie树
long time_1 = getCurrentTime();
while(getline(fin, str, '\n')){
trie.insert(str);
}
long time_2 = getCurrentTime();
fin.close();
//3.产生随机字符串
string rand_str = rand_string(6);
//rand_str = "wdeuojyucsalslpd";
cout << "随机字符串为:" << rand_str << endl;
//4.利用Trie树计算结果
vector<string> word_set_1;
long time_3 = getCurrentTime();
trie.search(rand_str, 3, word_set_1);
long time_4 = getCurrentTime();
//5.利用暴力匹配计算结果
vector<string> word_set_2;
vector<string> word_dict;
fin.open("dict.txt");
if(!fin){
cout << "打开文件失败!" << endl;
}
while(getline(fin, str, '\n')){
word_dict.push_back(str);
}
int size = word_dict.size();
long time_5 = getCurrentTime();
for(int j = 0; j < size; j++){
if(edit_length(word_dict[j], rand_str) < 3){
word_set_2.push_back(word_dict[j]);
}
}
long time_6 = getCurrentTime();
fin.close();
//6.结果比较
sort(word_set_1.begin(), word_set_1.end());
sort(word_set_2.begin(), word_set_2.end());
cout << "word_set_1的大小:" << word_set_1.size() << endl;
cout << "结果为:";
for(int i = 0; i < word_set_1.size(); i++){
cout << " " << word_set_1[i];
}
cout << endl;
cout << "word_set_2的大小:" << word_set_2.size() << endl;
cout << "结果为:";
for(int i = 0; i < word_set_2.size(); i++){
cout << " " << word_set_2[i];
}
cout << endl;
if(word_set_1 == word_set_2){
cout << "验证正确" << endl;
} else {
cout << "验证错误" << endl;
}
//7.时间比较
cout << "建立Trie树用时(ms):" << time_2 - time_1 << endl;
cout << "Trie树搜索用时(ms):" << time_4 - time_3 << endl;
cout << "暴力搜索用时(ms):" << time_6 - time_5 << endl;
cout << "百分比:" << double(time_4 -time_3)/(time_6 - time_5) << endl;
}
int main(){
//Test_1();
//Test_2();
Test_3();
}