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% |
利用Trie树搜索比暴力匹配方式搜索快得多,一次查询的时间比值:最好为:4.78%,最坏为:9.57,平均为:7.90%
4.源代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int X = 30;
const int Y = 30;
const int MAX = 30;
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;
private:
void destory(Node* r);
void find(Node *pRoot, string &str, int limit_num, vector &word_set);
public:
Trie();
~Trie();
void insert(string str);
void search(string &str, int limit_num, vector &word_set);
};
Trie::Trie(){
}
Trie::~Trie(){
destory(pRoot);
}
//销毁Trie树
void Trie::destory(Node* pRoot){
if(pRoot == NULL){
return;
}
destory(pRoot -> left);
destory(pRoot -> right);
delete pRoot;
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 = new Node();
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 = new Node();
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 = new Node();
p -> length = distance;
p -> word = str;
p -> left = NULL;
p -> right = pCur;
pPre -> right = p;
break;
}
}
}
} else {
//如果Trie树还不存在,以该单词创建根节点
pRoot = new Node();
pRoot -> length = 0;
pRoot -> word = str;
}
}
//搜索与给定字符串的编辑距离小于给定值的所有字符串(内部调用)
void Trie::find(Node* pRoot, string &str, int limit_num, vector &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 &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 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 word_set_1;
long time_3 = getCurrentTime();
trie.search(rand_str, 3, word_set_1);
long time_4 = getCurrentTime();
//5.利用暴力匹配计算结果
vector word_set_2;
vector 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();
}