dfa最小化,终于完成了。
采取的方法是hopcroft的填表法,详情见如下代码
#include "nfa_to_dfa.h"
int* dfa_diff_matrix; int mini_dfa_number;//这个是最小化的 dfa表的索引
typedef struct _min_dfa_node
{
pdfa_edge begin;
int is_end;//记录是否是接受节点
}min_dfa_node,*pmin_dfa_node;
min_dfa_node mini_dfa_table[];//设定为100,其实也可以malloc一下,因为我们已经知道了有多少个节点了
//为了偷懒,算了
is_diff(int input_a,int input_b)//判断两个点是不是等价的程序,注意我们传参数的时候默认a比b大
{
int destination_a,destination_b;//临时的转换目标地址
pdfa_edge temp_edge;//遍历邻接表
char label_traverse;//用来遍历符号表
int for_i;
for(for_i=;for_i<alpha_size;for_i++)
{
label_traverse=mini_alpha_set[for_i];
temp_edge=current_dfa_table[input_a].begin;
while(temp_edge!=NULL&&temp_edge->label!=label_traverse)
{
temp_edge=temp_edge->next;
}
if(temp_edge==NULL)
{
destination_a=;
}
else
{
destination_a=temp_edge->dest_dfa_index;
}
temp_edge=current_dfa_table[input_b].begin;
while(temp_edge!=NULL&&temp_edge->label!=label_traverse)
{
temp_edge=temp_edge->next;
}
if(temp_edge==NULL)
{
destination_b=;
}
else
{
destination_b=temp_edge->dest_dfa_index;
}
if(destination_a==destination_b)
{
//下一次吧
}
else
{
if(destination_a*destination_b==)
{
return ;
}
else
{
if( destination_a>destination_b)
{
if(dfa_diff_matrix[dfa_node_number*(input_a)+(input_b)]!=)//如果当前无法判别
{
if(is_diff(destination_a,destination_b))//如果可以判别
{
dfa_diff_matrix[dfa_node_number*(input_a)+(input_b)]=;
return ;
}
else
{
//继续判别
}
}
else
{
return ;
}
}
else
{
if(dfa_diff_matrix[dfa_node_number*(input_b)+(input_a)]!=)//如果当前无法判别
{
if(is_diff(destination_b,destination_a))//如果可以判别
{
dfa_diff_matrix[dfa_node_number*(input_b)+(input_a)]=;
return ;
}
else
{
//继续判别
}
}
else
{
return ;
}
}
}
}
}
return ;
}
void diff_matrix(void)
{
int already_diff_number;//这个是用来判断有多少个点已经经过了等价性测试
int for_i,for_j;
dfa_diff_matrix=malloc(sizeof(int)*dfa_node_number*dfa_node_number);//分配这个节点
for(for_i=;for_i<dfa_node_number;for_i++)
{
for(for_j=;for_j<dfa_node_number;for_j++)
{
dfa_diff_matrix[(for_i)*dfa_node_number+for_j]=;
}
}
for(for_i=;for_i<dfa_node_number;for_i++)
{
for(for_j=;for_j<for_i;for_j++)//这里首先根据是否是接受节点来初始化矩阵
{
if(current_dfa_table[for_i+].is_end!=current_dfa_table[for_j+].is_end)
{
dfa_diff_matrix[(for_i)*dfa_node_number+for_j]=;
}
}
}
do{
already_diff_number=;
for(for_i=;for_i<dfa_node_number;for_i++)
{
for(for_j=;for_j<for_i;for_j++)
{
if(dfa_diff_matrix[(for_i)*dfa_node_number+for_j]!=)
{
if(is_diff(for_i,for_j)==)
{
dfa_diff_matrix[(for_i)*dfa_node_number+for_j]=;
}
}
}
}
}while(already_diff_number!=);//如果本趟处理没有找到新的可分节点,就结束
for(for_i=;for_i<dfa_node_number;for_i++)
{
for(for_j=;for_j<dfa_node_number;for_j++)
{
printf("%d ",dfa_diff_matrix[(for_i)*dfa_node_number+for_j]);
}
printf("\n");
}
}
void minimize_dfa_matrix(void)//在已经构建好了dfa_diff_matrix后,开始群聚,并建图
{
//现在开始群聚
int* already_in_group;//用来记录哪些点已经在群里面了
int* temp_group;
int* before_min_access;//这里来标注哪些节点已经通过了最简化dfa的转换
int group_number=;//注意群号由0开始
int* index_of_group;
pdfa_edge temp_edge;//这个是用来重新建立最小化dfa的临时边
pdfa_edge temp_add_edge;//这个是用来往最小dfa里面增加边的临时边
int dest_group_number;//这个是在增加边的时候的目标编号
int **group_set;
int for_i,for_j;
group_set=malloc(sizeof(int)*dfa_node_number);
already_in_group=malloc(sizeof(int)*dfa_node_number);
for(for_i=;for_i<dfa_node_number;for_i++)
{
already_in_group[for_i]=;
*(group_set+for_i)=NULL;
}
for(for_i=;for_i<dfa_node_number-;for_i++)//聚集
{
if(already_in_group[for_i]==)
{
already_in_group[for_i]=;
temp_group=malloc(sizeof(int)*dfa_node_number);
*(group_set+group_number++)=temp_group;
for(for_j=;for_j<dfa_node_number;for_j++)
{
temp_group[for_j]=;
}//注意这里也需要考虑加减1的问题
temp_group[for_i]=;
for(for_j=for_i+;for_j<dfa_node_number;for_j++)
{
if(!dfa_diff_matrix[(for_j)*dfa_node_number+for_i])
{
temp_group[for_j]=;
already_in_group[for_j]=;
}
}
}
}//现在已经完全都聚集为一团了
mini_dfa_number=group_number;
//现在再将节点和群的关系反转
index_of_group=malloc(sizeof(int)*dfa_node_number);
//这里又需要注意加减1的关系,由于这里dfa节点是从1标号的,而我们在index_of_group是从0标号的,要注意
for(for_i=;for_i<dfa_node_number;for_i++)
{
index_of_group[for_i]=;
}
for_i=;
while(*(group_set+for_i)!=NULL)//前面开了一个索引数组,现在来赋值,这样就可以直接得到某个节点所在的群号
{
for(for_j=;for_j<dfa_node_number;for_j++)
{
if(*(*(group_set+for_i)+for_j)==)
{
index_of_group[for_j]=for_i;
}
}
for_i++;
}//现在关系已经翻转了
//下一步就是利用这个点与群的关系来新建立一个dfa图
//这里的群号就是节点的编号,由于每个群里面的节点都是等价的,所以只需要找一个节点就行了
for(for_i=;for_i<=group_number;for_i++)//这里的for_i是用来表示最小dfa图的标号,所以从1开始
{
//对每一个群进行遍历
mini_dfa_table[for_i].begin=NULL;
mini_dfa_table[for_i].is_end=;
for_j=;
while(*(*(group_set+for_i-)+for_j)!=)//由于group是从0开始标号的,所以要减去1
{
for_j++;
}//找到这个群里面一个节点,注意加减一问题,少犯错误啊,1号节点存储在0号位置上
if(current_dfa_table[for_j+].is_end)
{
mini_dfa_table[for_i].is_end=;//标记为结束节点
}
temp_edge=current_dfa_table[for_j+].begin;
while(temp_edge!=NULL)//重新建设邻接表
{
temp_add_edge=malloc(sizeof(struct _dfa_edge));
temp_add_edge->label=temp_edge->label;
temp_add_edge->next=mini_dfa_table[for_i].begin;
dest_group_number=index_of_group[temp_edge->dest_dfa_index-]+;//特别要注意这里的加一和减一
//由于temp_edge->dest_dfa_node是从1开始标号的,而index_of_group是从0开始标号的,所以我们要减一
//同样,又由于最后的最简化dfa的图是从1开始标号的,所以我们要加1;
temp_add_edge->dest_dfa_index=dest_group_number;
mini_dfa_table[for_i].begin=temp_add_edge;
temp_edge=temp_edge->next;
}
//本群的邻接表构建完成
}//所有群的邻接表构建完成
}
void show_mini_dfa(void)//输出图
{
int for_i;
pdfa_edge temp_dfa_edge;
number_of_end_dfa=;
for(for_i=;for_i<=mini_dfa_number;for_i++)
{
if(mini_dfa_table[for_i].is_end==)
{
printf("this node is an destination node ,index is %d\n",for_i);
}
temp_dfa_edge=mini_dfa_table[for_i].begin;
while(temp_dfa_edge!=NULL)
{
printf("there is an dfa edge from %d to %d with label %c\n",for_i,temp_dfa_edge->dest_dfa_index,temp_dfa_edge->label);
temp_dfa_edge=temp_dfa_edge->next;
}
}
printf("the minimized dfa is completed\n");
}
dfa最小化,终于完成了。的更多相关文章
- DFA 最小化
NDFA.εNDFA 确定化的细节这里就不总结了,这里说一说DFA最小化的算法. 关于DFA最小化,
- dfa最小化,修正了上个版本的一些错误。
上个版本测试的时候,只用了两个非常简单的测试用例,所以好多情况有问题却没有测试出来 bug1:在生成diff_matrix的时候,循环变量少循环了一次,导致最后一个节点在如果无法与其他点合并的情况下, ...
- 编译原理中DFA最小化
关于编译原理最小化的操作,专业术语请移步至:http://www.360doc.com/content/18/0601/21/11962419_758841916.shtml 这里只是记录一下个人的理 ...
- 第九次作业——DFA最小化,语法分析初步
老师:MissDu 提交作业 1.将DFA最小化:教材P65 第9题 答: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 3.自上而下语法分析,回溯产生的原因是 ...
- DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 语言:(01 | 10)*(01 | 10) 自动机图: DFA状态转换矩阵 ...
- 编译原理之DFA最小化,语法分析初步
1.将DFA最小化: 状态转换图: 识别语言:b*ac*(da)*bb* 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 (1)正规式: S -> 0(1S+ ...
- 第九次作业 DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 Ⅰ {1,2,3,4,5} {6,7} {1,2}b={1,2,3,4,5} 3,4}b={5} {6,7} Ⅱ {1,2}{3,4}{5} {6,7} 2.构 ...
- 作业九——DFA最小化
1.将DFA最小化:教材P65 第9题 I {1, 2, 3, 4, 5} {6, 7} {1, 2}b->{1, 2, 3, 4, 5} {3, 4}b->{6, 7} {5}b-> ...
- 编译原理:DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 解析: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 解析: S→ 0A|1B →S → 0(1S|1)|1(0S|0 ...
- 第九次-DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 3.自上而下语法分析,回溯产生的原因是什么? 4.P100 练习4,反复提取 ...
随机推荐
- SQL2005/8数据库提示单个用户无法操作的解决方法
原因分析: 是操作数据库的用户被锁定了,思路是通过查找目标用户,将其解锁即可,可是这样太麻烦了. 解决办法执行如下sql: USE master; GO DECLARE @SQL VARCHAR( ...
- Ubuntu 搭建PHP开发环境
Ubuntu确实很好玩.有喜欢的命令行,简洁的界面,不同于Window要的感觉.偶尔换换环境工作,学习Linux的思维方式,是一种不错的做 法.之前也折腾过Ubuntu,不过,因为网络的问题,一直没有 ...
- 消息系统Kafka介绍 - 董的博客
1. 概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...
- VC++ 中滑动条(slider控件)使用 [转+补充]
滑动控件slider是Windows中最常用的控件之一.一般而言它是由一个滑动条,一个滑块和可选的刻度组成,用户可以通过移动滑块在相应的控件中显示对应的值.通常,在滑动控件附近一定有标签控件或编辑框控 ...
- css margin的相关属性,问题及应用
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=48 引言:margin ...
- SCVMM配置
SCVMM添加工作组主机 1.准备一台物理主机并且安装WindowsServer 2012名为HV2(工作组不加域)接着添加hyper-v角色 2.把SCVMM1服务器上的SCVMM2012代理程序( ...
- 【HTML】Jquery前台传参及接收
在一些网页应用中,有的时候需要前台之间传递参数,通过JS语法来做一些判断处理. 发送端:(a页面) <a href="b.html?Show=true" id="t ...
- C#调用C++ dll时,结构体引用传参的方法
写了一个C++的LogLog Logit 四参数等算法的接口dll,给C#调用,但是发现传参有问题 如 extern "C" _declspec(dllexport) bool ...
- 深入NGINX:我们如何设计它的性能和扩展性
为了更好地理解设计,你需要了解NGINX是如何工作的.NGINX之所以能在性能上如此优越,是由于其背后的设计.许多web服务器和应用服务器使用简单的线程的(threaded).或基于流程的 (proc ...
- 关于phpcmsv9更新缓存出现链接被重置的问题
今天安装phpcmsv9后更新缓存出现链接被重置的错误,..找了半天原因. . .原来是apache配置里面的keepAlive显示的是off,,应该将其改为on...然后重新启动apache....