几个有关图的算法2(DFS,TopoLogicalSort) .

深度优先算法和拓扑算法是很多算法的基础,只得深究:
/// <summary>
    /// 广度搜索算法,图
    /// </summary>
    public partial class GraphicSearchAlg
    {
      
        /// <summary>
        /// 深度优先搜索算法,这里只是计算深度.
        /// </summary>
        /// <param name="g">图,基于邻接表示</param>
        /// <param name="s">源点</param>
        public void DFS(Graphic g, Node s)
        {
            g.FinishOrder = 0;
            g.EnterOrder = 0;
            //初始化
            foreach (var theNode in g.Nodes)
            {
                theNode.VisitedSign = 0;//0 white,1 gray,2 black
                theNode.Parent = null;
                theNode.EnterTime = 0;
                theNode.FinishTime = 0;
                theNode.FinishOrder = 0;
                theNode.EnterOrder = 0;
            }
            s.VisitedSign = 1;
            int theVisitTime = 0;
            DFS_Visit(g, s, ref theVisitTime);
            foreach (var theNode in g.Nodes)
            {
                if (theNode.VisitedSign == 0)
                {
                    DFS_Visit(g, theNode, ref theVisitTime);
                }
            }
            
        }
        /// <summary>
        /// DFS访问算法.
        /// </summary>
        /// <param name="g"></param>
        /// <param name="u"></param>
        /// <param name="VisitTime"></param>
        private void DFS_Visit(Graphic g, Node u,ref int VisitTime)
        {
            u.VisitedSign = 1;
            VisitTime++;
            u.EnterTime = VisitTime;

            g.EnterOrder++;
            u.EnterOrder = g.EnterOrder;
            

            foreach (var theAdjNode in u.AdjNodes.Keys)
            {
                //边分类,存放在对应边的value值.0树边,1 方向边, 2正向边,3交叉边. 
                switch (theAdjNode.VisitedSign)
                {
                    case 1:
                        u.AdjNodes[theAdjNode].EdgeType = 1;
                        break;
                    case 2:
                        if (u.EnterTime < theAdjNode.EnterTime)
                        {
                            u.AdjNodes[theAdjNode].EdgeType = 2;
                        }
                        else
                        {
                            u.AdjNodes[theAdjNode].EdgeType = 3;
                        }
                        break;
                    default:
                        u.AdjNodes[theAdjNode].EdgeType = 0;
                        break;
                }
                if (theAdjNode.VisitedSign == 0)
                {
                    theAdjNode.Parent = u;
                    DFS_Visit(g, theAdjNode, ref VisitTime);
                }
            }
            u.VisitedSign = 2;
            VisitTime++;
            u.FinishTime = VisitTime;
            g.FinishOrder++;
            u.FinishOrder = g.FinishOrder;
        }

       /// <summary>
       /// 拓扑排序
       /// </summary>
       /// <param name="g">有限无回路图</param>
       /// <returns></returns>
        public Node[] TopoLogicalSort(Graphic g)
        {
            int theLen = g.Nodes.Count;
            Node[] theLink = new Node[theLen];
            DFS(g, g.Nodes[0]);
            foreach (var theNode in g.Nodes)
            {
                theLink[theLen - theNode.FinishOrder] = theNode;
            }
            return theLink.ToArray();
        }
    }

你可能感兴趣的:(算法,null,Class)