Пример #1
0
        public AcyclicSP(DirectedWeightedEdgeGraph G, int s)
        {
            //从加权有向图中获取它的有向图,以供拓扑排序用
            tmpG = new DirectGraph(G.GetV());
            for (int v = 0; v < G.GetV(); v++)
            {
                foreach (var e in G.GetAdj(v))
                {
                    int to = e.To();
                    tmpG.AddAdj(v, to);
                }
            }

            int V = G.GetV();

            edgeTo = new DirectedWeightedEdge[V];
            distTo = new double[V];
            for (int i = 0; i < V; i++)
            {
                distTo[i] = double.MaxValue;
            }

            distTo[s] = 0.0;
            Topological top = new Topological(tmpG);

            foreach (var v in top.Order)
            {
                relax(G, v);
            }
        }
Пример #2
0
        public Topological(DirectGraph G)
        {
            DirectCycle cycle = new DirectCycle(G);

            if (!cycle.hasCycle())
            {
                DFSOrder dfsOrder = new DFSOrder(G);
                order = dfsOrder.ReversePost;
            }
        }
Пример #3
0
 public DFSOrder(DirectGraph G)
     : base(G)
 {
     reversePost = new Stack <int>();
     for (int v = 0; v < G.GetV(); v++)
     {
         if (!marked[v])
         {
             Search(v);
         }
     }
 }
Пример #4
0
 public DirectCycle(DirectGraph _G)
     : base(_G)
 {
     onStack = new bool[G.GetV()];
     for (int t = 0; t < G.GetV(); t++)
     {
         if (!marked[t])
         {
             Search(t);
         }
     }
 }
Пример #5
0
        private int M;//状态数量,即正则表达式长度

        /// <summary>
        /// 根据给定的正则表达式构造NFA
        /// </summary>
        public NFA(string regexp)
        {
            Stack<int> ops = new Stack<int>();//用来保存左括号、或符号
            re = regexp.ToCharArray();
            M = re.Length;
            G = new DirectGraph(M + 1);//有向图,保存每个点出发可以到达的点

            for (int i = 0; i < M; i++)
            {
                int lp = i;
                if (re[i] == '(' || re[i] == '|')//|的索引也要压入栈中,这样因为(的索引也压入了,可以遇到右括号时,(和|的索引都能知道,就能实现或跳转了
                {
                    ops.Push(i);
                }
                else if (re[i] == ')')
                {
                    List<int> orList = new List<int>();
                    while (ops.Count > 0)
                    {
                        int or = ops.Pop();
                        if (re[or] == '(')//找到|符号的上一个左括号的位置
                        {
                            lp = or;
                            break;
                        }
                        if (re[or] == '|') orList.Add(or);
                    }
                    foreach (var or in orList)
                    {
                        G.AddAdj(lp, or + 1);//把(和|后面的第一个字符相连
                        G.AddAdj(or, i);//把|和)相连
                    }
                }
                if (i < M - 1 && re[i + 1] == '*')//当前字符下一字符为闭包
                {
                    G.AddAdj(lp, i + 1);//即B->*
                    G.AddAdj(i + 1, lp);//即*->B
                }
                if (re[i] == '(' || re[i] == '*' || re[i] == ')')//这些符号也都可以进入下一状态,但|不能直接进入下一状态。普通字符之间不需要进行连接,因为不是epsilon转换
                {
                    G.AddAdj(i, i + 1);
                }
            }
        }
Пример #6
0
 public DFSOrder(DirectGraph G)
     : base(G)
 {
     reversePost = new Stack<int>();
     for (int v = 0; v < G.GetV(); v++)
     {
         if (!marked[v])
         {
             Search(v);
         }
     }
 }
Пример #7
0
 public Topological(DirectGraph G)
 {
     DirectCycle cycle = new DirectCycle(G);
     if (!cycle.hasCycle())
     {
         DFSOrder dfsOrder = new DFSOrder(G);
         order = dfsOrder.ReversePost;
     }
 }
Пример #8
0
 public DirectCycle(DirectGraph _G)
     : base(_G)
 {
     onStack = new bool[G.GetV()];
     for (int t = 0; t < G.GetV(); t++)
     {
         if (!marked[t])
         {
             Search(t);
         }
     }
 }