Exemplo n.º 1
0
        private void BFS(ALGraph G, int k)
        {
            int         i;
            Queue <int> Q;
            EdgeNode    p;

            Q = new Queue <int>();
            sb.Append(G.AdjList[k].vertex.ToString() + "--->");
            visited[k] = true;
            Q.Enqueue(k);
            while (Q.Count != 0)
            {
                i = (int)Q.Dequeue();
                p = G.AdjList[i].firstedge;
                while (p != null)
                {
                    if (!visited[p.adjvex])
                    {
                        sb.Append(G.AdjList[p.adjvex].vertex.ToString() + "--->");
                        visited[p.adjvex] = true;
                        Q.Enqueue(p.adjvex);
                    }
                    p = p.next;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main1(string[] args)
        {
            int e;
            int n;

            Console.Write("请输入图的类型: 0 为无向图, 1 为有向图  ");
            GraphType t = (GraphType)(int.Parse(Console.ReadLine()));

            Console.Write("请输入图的顶点数 : ");
            e = int.Parse(Console.ReadLine());
            Console.Write("请输入图的边数(如果选择无向图,两接点间的边只需输入一次) : ");
            n = int.Parse(Console.ReadLine());
            ALGraph G = new ALGraph(e, n, t);

            //构建一个有向图结构
            (new GraphOperation()).CreateALGraph(G);
            Console.Write("\n ");
            //实现深度遍历
            (new GraphOperation()).DFSTraverse(G);
            Console.Write("\n ");
            //实现广度遍历
            (new GraphOperation()).BFSTraverse(G);
            Console.Write("\n ");
            //判断图的类型
            (new GraphOperation()).NonPreFirstTopSort(G);
            Console.ReadLine();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 建全一个有向图对象
        /// (不是建立 ^_^)
        /// </summary>
        /// <param name="G"></param>
        public void CreateALGraph(ALGraph G)
        {
            int      i, j, k;
            EdgeNode s;

            for (int vnc = 0; vnc < G.VertexNodeCount; vnc++)
            {
                VertexNode cuVN = new VertexNode();
                Console.Write("请输入顶点的编号 : ");
                cuVN.vertex    = Console.ReadLine();
                cuVN.firstedge = null;
                G.AdjList[vnc] = cuVN;
            }

            for (k = 0; k < G.BordeCount; k++)
            {
                Console.Write("请输入边开始顶点的序号 : ");
                i = int.Parse(Console.ReadLine()) - 1;
                Console.Write("请输入边结束顶点的序号 : ");
                j        = int.Parse(Console.ReadLine()) - 1;
                s        = new EdgeNode();
                s.adjvex = j;
                s.next   = G.AdjList[i].firstedge;
                //邻接表头插法
                G.AdjList[i].firstedge = s;
                //建立无向图
                if (G.theType == GraphType.UnDiGraph)
                {
                    s        = new EdgeNode();
                    s.adjvex = i;
                    s.next   = G.AdjList[j].firstedge;
                    G.AdjList[j].firstedge = s;
                }
            }
        }
Exemplo n.º 4
0
        private void DFS(ALGraph G, int i)
        {
            EdgeNode p;

            sb.Append(G.AdjList[i].vertex.ToString() + "--->");

            visited[i] = true;
            p          = G.AdjList[i].firstedge;
            while (p != null)
            {
                if (!visited[p.adjvex])
                {
                    DFS(G, p.adjvex);//递归
                }
                p = p.next;
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 深度优先遍历以邻接表表示的图G.
 /// </summary>
 /// <param name="G"></param>
 public void DFSTraverse(ALGraph G)
 {
     visited = new bool[G.VertexNodeCount];
     sb      = new StringBuilder();
     for (int i = 0; i < G.VertexNodeCount; i++)
     {
         visited[i] = false;
     }
     for (int i = 0; i < G.VertexNodeCount; i++)
     {
         if (!visited[i])
         {
             DFS(G, i);
         }
     }
     Console.Write("深度优先遍历以邻接表表示的图G 的结果:\n ");
     Console.Write(sb);
 }
Exemplo n.º 6
0
        /// <summary>
        /// 有向图无前驱的顶点优先之拓扑排序
        /// </summary>
        /// <param name="G"></param>
        public void NonPreFirstTopSort(ALGraph G)
        {
            if (G.theType == GraphType.UnDiGraph)
            {
                Console.Write("对无向图进行拓扑排序是不对的!");
                return;
            }
            StringBuilder SB = new StringBuilder();

            int[]       indegree = new int[G.VertexNodeCount];
            Stack <int> S;
            int         i, j, count = 0;
            EdgeNode    p;

            for (i = 0; i < G.VertexNodeCount; i++)
            {
                indegree[i] = 0;
            }

            for (i = 0; i < G.VertexNodeCount; i++)
            {
                for (p = G.AdjList[i].firstedge; p != null; p = p.next)
                {
                    indegree[p.adjvex]++;
                }
            }

            S = new Stack <int>();

            for (i = 0; i < G.VertexNodeCount; i++)
            {
                if (indegree[i] == 0)
                {
                    S.Push(i);
                }
            }

            while (S.Count != 0)
            {
                i = (int)S.Pop();
                SB.Append(G.AdjList[i].vertex.ToString() + "--->");
                count++;

                for (p = G.AdjList[i].firstedge; p != null; p = p.next)
                {
                    j = p.adjvex;
                    indegree[j]--;
                    if (indegree[j] == 0)
                    {
                        S.Push(j);
                    }
                }
            }
            if (count < G.VertexNodeCount)
            {
                Console.Write("图中有环,排序失败");
            }
            else
            {
                Console.Write("拓扑排序结果如下 : \n" + SB);
            }
        }