完整引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git
DFA最小化的算法原理
“DFA状态最小化算法的工作原理是将一个DFA的状态集合分划成多个组,每个组中的各个状态之间相互不可区分。然后,将每个组中的状态合并成状态最少DFA的一个状态。算法在执行过程中维护了状态集合的一个分划,分划中的每个组内的各个状态不能区分,但是来自不同组的任意两个状态是可区分的。当任意一个组不能再被分解为更小的组时,这个分划就不能再进一步精化,此时我们就得到了状态最少的DFA。”
——《编译原理》例3.38
起始时,该分划包含两个组:接受状态组和非接受状态组。
实例
如图(编译原理
图3-36)
首先我们将ABCDE分划到两个组中{ABCD}和{E},{E}是接受状态组,且不可被再分割。
{ABCD}是可分割的,所以我们考虑所有可能的转换。
先看转换字符a:
A->a->B
B->a->B
C->a->B
D->a->B
所以ABCD经过a到达的集合是{B},而{B}属于{ABCD}这一个集合,所以我们说{ABCD}在输入字符为a时是不可分划的。
在来看转换字符b:
A->b->C
B->b->D
C->b->C
D->b->E
到达的集合是{CDE},其中CD属于{ABCD}分划,E属于{E}分划。
所以我们说{ABCD}在输入字符为b时是不可分划的。按照输入b转换到的组,我们将{ABCD}分划为{ABC}和{D}两个组。同时将{ABCD}从组集合中删除。因为{ABCD}已经分划为{ABC}和{D}。
接下来看{ABC},先看在字符a上的转换:
A->a->B
B->a->B
C->a->B
因为全部都是到达的B,所以不可分划。
再看在字符b上的转换:
A->b->C
B->b->D
C->b->C
其中C属于{ABC}组,D属于{D}组。所以{ABC}可以分划为{AC}和{B}。
最后看{AC}组,A和C在字符a上都转换到B,在字符b上都转换到C,所以{AC}是不可分划的组。
最后得到的分组情况为:
{AC},{B},{D},{E}。
同一个组中只需要保留一个节点即可(因为同一个组的节点在转换上都是相同的),所以我们直接将C节点去除,保留A节点(因为A节点是开始状态节点)。最终得到的状态最小DFA的转换表为:
代码实现(关键代码)
- BOOL CDFA::FindRelationNode(list<DFANodeRelation> &lstNodeRelation,
- int nIdxFrom, unsigned char ch, int &nMapToIdx)
- {
- list<DFANodeRelation>::iterator it = lstNodeRelation.begin();
- for ( ; it != lstNodeRelation.end(); it++)
- {
- if (it->m_nIdxFrom == nIdxFrom && it->m_ch == ch)
- {
- nMapToIdx = it->m_nIdxTo;
- return TRUE;
- }
- }
- return FALSE;
- }
- int CDFA::FindIdxInListSet(int nMapToIdx, list<set<int>> &lstSet)
- {
- int i = 0;
- for (list<set<int>>::iterator it = lstSet.begin(); it != lstSet.end(); it++, i++)
- {
- set<int> & setIdx = *it;
- for (set<int>::iterator itInt = setIdx.begin(); itInt != setIdx.end(); itInt++)
- {
- if (nMapToIdx == *itInt)
- {
- return i;
- }
- }
- }
- return -1;
- }
- BOOL CDFA::PartitionOneGroup(list<set<int>> &lstSet, set<int> &setOneGroup,
- list<DFANodeRelation> &lstNodeRelation,
- map<int, set<int>> &mapPartitionInfo)
- {
- BOOL bRet = FALSE;
- list<DFANodeRelation>::iterator itRelation;
- set<unsigned char> setChar;
- set<int> setMapToIdx;
- try
- {
- // collect each node's translation char in the set
- for (set<int>::iterator it = setOneGroup.begin(); it != setOneGroup.end(); it++)
- {
- for (itRelation = lstNodeRelation.begin(); itRelation != lstNodeRelation.end(); itRelation++)
- {
- if (itRelation->m_nIdxFrom == *it)
- {
- setChar.insert(itRelation->m_ch);
- }
- }
- }
- // end collect
- for (set<unsigned char>::iterator it = setChar.begin(); it != setChar.end(); it++)
- {
- mapPartitionInfo.clear();
- int nMapToIdx = -1; // indicate map to a dead state, there no translation for this pair of node/char
- for (set<int>::iterator itNodeId = setOneGroup.begin(); itNodeId != setOneGroup.end(); itNodeId++)
- {
- if (FindRelationNode(lstNodeRelation, *itNodeId, *it, nMapToIdx))
- {
- int nIdx = FindIdxInListSet(nMapToIdx, lstSet);
- if (nIdx == -1)
- assert(FALSE);
- mapPartitionInfo[nIdx].insert(*itNodeId);
- }
- else
- mapPartitionInfo[-1].insert(*itNodeId);
- }
- if (mapPartitionInfo.size() > 1)// had distinguish
- {
- break;
- }
- }
- }
- catch (...)
- {
- goto Exit0;
- }
- bRet = TRUE;
- Exit0:
- return bRet;
- }
- BOOL CDFA::PartitionGroups(list<set<int>> &lstSet, list<DFANodeRelation> &lstNodeRelation)
- {
- BOOL bRet = FALSE;
- list<set<int>>::iterator it = lstSet.begin();
- map<int, set<int>> mapPartitionInfo;
- // used map to record the node can translate to which group,
- // the int(map key) is group id.
- // the set<int> contain the node ID that can translate to the group.
- for ( ; it != lstSet.end(); )
- {
- mapPartitionInfo.clear();
- set<int> &setOneGroup = *it;
- CHECK_BOOL ( PartitionOneGroup(lstSet, setOneGroup, lstNodeRelation, mapPartitionInfo) );
- if (mapPartitionInfo.size() > 1)// means that current group can partition
- {
- map<int, set<int>>::iterator itM = mapPartitionInfo.begin();
- for ( ; itM != mapPartitionInfo.end(); itM++)
- {
- try
- {
- lstSet.push_back(itM->second);
- }
- catch (...)
- {
- goto Exit0;
- }
- }
- it = lstSet.erase(it);// if a group had partition, the group need delete in the list
- }
- else
- it++;
- }
- bRet = TRUE;
- Exit0:
- return bRet;
- }
- /**
- @brief Minimize DFA
- @param nSetSize node count
- @param lstNodeRelation node relation table
- @param setAcceptingIdx set for Accepting status node's index
- @param lstSet for save the result
- @return TRUE, success; otherwise means fail.
- */
- BOOL CDFA::MinimizeDFA(int nNodeCount,
- list<DFANodeRelation> &lstNodeRelation,
- set<int> &setAcceptingIdx,
- list<set<int>> &lstSet
- )
- {
- BOOL bRet = FALSE;
- set<int> setUnAccepting;
- assert(nNodeCount >= 1);
- assert(setAcceptingIdx.size() != 0);
- assert(lstNodeRelation.size() != 0);
- lstSet.clear();
- try
- {
- lstSet.push_back(setAcceptingIdx);
- // get unAccepting set
- for (int i = 0; i < nNodeCount; i++)
- {
- if (setAcceptingIdx.find(i) == setAcceptingIdx.end())
- {
- setUnAccepting.insert(i);
- }
- }
- if (setUnAccepting.size() > 0)
- {
- lstSet.push_back(setUnAccepting);
- }
- }
- catch (...)
- {
- goto Exit0;
- }
- CHECK_BOOL ( PartitionGroups(lstSet, lstNodeRelation) );
- bRet = TRUE;
- Exit0:
- return bRet;
- }
完整引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git