コード例 #1
0
        private GraphDigraph reverse()
        {
            // 创建图
            GraphDigraph g = new GraphDigraph(this.V);

            // 添加边, 遍历原图的每个顶点
            for (int v = 0; v < this.V; v++)
            {
                //  获取由该顶点v指出的所有的边
                IEnumerator ie = this.adj[v].GetEnumerator();
                while (ie.MoveNext())
                {
                    // 原图中表示的是由顶点v->w的边
                    int w = (int)ie.Current;
                    g.addEdge(w, v);
                }
            }
            return(g);
        }
コード例 #2
0
        /// <summary>
        /// 拓扑排序 测试
        /// </summary>
        private static void TopoLogicalOrderTest()
        {
            // 准备有向图
            GraphDigraph graph = new GraphDigraph(6);

            graph.addEdge(0, 2);
            graph.addEdge(0, 3);
            graph.addEdge(1, 3);
            graph.addEdge(2, 4);
            graph.addEdge(3, 4);
            graph.addEdge(4, 5);
            // 通过TopoLogicalOrder对象排序
            TopoLogicalOrder order = new TopoLogicalOrder(graph);

            if (!order.isCycle())
            {
                StackList <int> stack = order.order();
                // 获取顶点线性排序的打印
                if (stack != null)
                {
                    IEnumerator ie = stack.GetEnumerator();
                    while (ie.MoveNext())
                    {
                        int o = (int)ie.Current;
                        Console.Write(o + ",");
                    }
                }
                else
                {
                    Console.WriteLine("TopoLogicalOrder stack is null.");
                }
            }
            else
            {
                Console.WriteLine("has cycle.");
            }
        }