예제 #1
0
파일: NFA.cs 프로젝트: g4idrijs/Algorithms
        /// <summary>
        /// 模拟NFA在给定文本上的运行轨迹
        /// </summary>
        public bool recognizes(string txt)
        {
            List<int> pc = new List<int>();//表示从reg某一位可达的后续位置

            GraphDFS dfs = new GraphDFS(G, 0);//先计算从0开始可达的所有状态
            for (int v = 0; v < G.GetV(); v++)
                if (dfs.hasPathTo(v))
                    pc.Add(v);

            for (int i = 0; i < txt.Length; i++)
            {
                List<int> match = new List<int>();//match表示txt字符在reg中匹配后,下一位txt字符去匹配的reg位置
                foreach (int v in pc)
                    if (v < M)
                        if (re[v] == txt[i] || re[v] == '.')
                            match.Add(v + 1);//因为是给下一匹配用的,所以要加1

                pc = new List<int>();
                dfs = new GraphDFS(G, match);
                for (int v = 0; v < G.GetV(); v++)//重新计算目前reg所在位置的后续所有可达状态
                    if (dfs.hasPathTo(v))
                        pc.Add(v);
            }

            foreach (int v in pc)
                if (v == M)
                    return true;//到达了正则的最后一位,表示匹配

            return false;//不匹配
        }
예제 #2
0
 public TranstiveClosure(Graph G)
 {
     all = new GraphDFS[G.GetV()];
     for (int i = 0; i < G.GetV(); i++)
     {
         all[i] = new GraphDFS(G, i);
     }
 }
예제 #3
0
 public TranstiveClosure(Graph G)
 {
     all = new GraphDFS[G.GetV()];
     for (int i = 0; i < G.GetV(); i++)
         all[i] = new GraphDFS(G, i);
 }