/// <summary>
        /// KruskalMST 算法的测试
        /// </summary>
        private static void KruskalMSTTest()
        {
            // 准备一个加权无向图
            EdgeWeightedGraph graph = new EdgeWeightedGraph(8);

            graph.addEdge(new Edge(4, 5, 0.35f));
            graph.addEdge(new Edge(4, 7, 0.37f));
            graph.addEdge(new Edge(5, 7, 0.28f));
            graph.addEdge(new Edge(0, 7, 0.16f));
            graph.addEdge(new Edge(1, 5, 0.32f));
            graph.addEdge(new Edge(0, 4, 0.38f));
            graph.addEdge(new Edge(2, 3, 0.17f));
            graph.addEdge(new Edge(1, 7, 0.19f));
            graph.addEdge(new Edge(0, 2, 0.26f));
            graph.addEdge(new Edge(1, 2, 0.36f));
            graph.addEdge(new Edge(1, 3, 0.29f));
            graph.addEdge(new Edge(2, 7, 0.34f));
            graph.addEdge(new Edge(6, 2, 0.40f));
            graph.addEdge(new Edge(3, 6, 0.52f));
            graph.addEdge(new Edge(6, 0, 0.58f));
            graph.addEdge(new Edge(6, 4, 0.93f));
            // 创建KruskalMST对象
            KruskalMST       kruskal  = new KruskalMST(graph);
            QueueList <Edge> allEdges = kruskal.edges();
            IEnumerator      ie       = allEdges.GetEnumerator();

            while (ie.MoveNext())
            {
                Edge e = (Edge)ie.Current;
                int  v = e.either();
                int  w = e.other(v);
                Console.WriteLine(v + "->" + w + " : " + e.weight().ToString("F2") + ",");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 队列 测试
        /// </summary>
        private static void QueueListTest()
        {
            // 创建队列对象
            QueueList <Student> list = new QueueList <Student>();

            // 测试队列的enqueue方法
            for (int i = 0; i < 5; i++)
            {
                list.enqueue(new Student()
                {
                    cardID = i, name = "sun" + i
                });
            }
            Student stu = list.dequeue();

            Console.WriteLine("Result: " + stu.cardID);
            Console.WriteLine();
            IEnumerator ie = list.GetEnumerator();

            while (ie.MoveNext())
            {
                if (ie.Current is Student)
                {
                    Student s = ie.Current as Student;
                    Console.WriteLine("cardID: " + s.cardID);
                }
                else
                {
                    Console.WriteLine("QueueList GetEnumerator is null.");
                }
            }
        }
        /// <summary>
        /// 前序遍历 的测试
        /// </summary>
        private static void BinaryTreePreErgodicTest()
        {
            BinaryTree <string, string> tree = new BinaryTree <string, string>();

            tree.put("E", "5");
            tree.put("B", "2");
            tree.put("G", "7");
            tree.put("A", "1");
            tree.put("D", "4");
            tree.put("F", "6");
            tree.put("H", "8");
            tree.put("C", "3");

            QueueList <string> keys = tree.preErgodic();

            if (keys != null)
            {
                IEnumerator ie = keys.GetEnumerator();
                while (ie.MoveNext())
                {
                    string value = tree.get(ie.Current.ToString());
                    Console.WriteLine(ie.Current.ToString() + "------" + value);
                }
            }
            else
            {
                Console.WriteLine("not keys.");
            }
        }
        /// <summary>
        /// 最短路径
        /// </summary>
        private static void DijksraSPTest()
        {
            // 创建加权有向图
            EdgeDirectedWeightedDirgraph graph = new EdgeDirectedWeightedDirgraph(8);

            graph.addEdge(new EdgeDirected(4, 5, 0.35f));
            graph.addEdge(new EdgeDirected(5, 4, 0.35f));
            graph.addEdge(new EdgeDirected(4, 7, 0.37f));
            graph.addEdge(new EdgeDirected(5, 7, 0.28f));
            graph.addEdge(new EdgeDirected(7, 5, 0.28f));
            graph.addEdge(new EdgeDirected(5, 1, 0.32f));
            graph.addEdge(new EdgeDirected(0, 4, 0.38f));
            graph.addEdge(new EdgeDirected(0, 2, 0.26f));
            graph.addEdge(new EdgeDirected(7, 3, 0.39f));
            graph.addEdge(new EdgeDirected(1, 3, 0.29f));
            graph.addEdge(new EdgeDirected(2, 7, 0.34f));
            graph.addEdge(new EdgeDirected(6, 2, 0.40f));
            graph.addEdge(new EdgeDirected(3, 6, 0.52f));
            graph.addEdge(new EdgeDirected(6, 0, 0.58f));
            graph.addEdge(new EdgeDirected(6, 4, 0.93f));
            // 创建 DijksraSP 对象 查找最短路径
            int       start   = 0;
            DijksraSP dijksra = new DijksraSP(graph, start);
            // 查找最短路径  0->6
            int end = 6;
            QueueList <EdgeDirected> allEdges = dijksra.pathTo(end);

            // 遍历打印
            if (allEdges == null)
            {
                Console.WriteLine("not path to " + end);
            }
            else
            {
                IEnumerator ie = allEdges.GetEnumerator();
                while (ie.MoveNext())
                {
                    EdgeDirected e = (EdgeDirected)ie.Current;
                    Console.WriteLine(e.from() + " -> " + e.to() + " : " + e.weight().ToString("F2"));
                }
            }
        }
        /// <summary>
        ///  PrimMST 算法测试
        /// </summary>
        private static void PrimMSTTest()
        {
            // 准备一个加权无向图
            EdgeWeightedGraph graph = new EdgeWeightedGraph(8);

            graph.addEdge(new Edge(4, 5, 0.35f));
            graph.addEdge(new Edge(4, 7, 0.37f));
            graph.addEdge(new Edge(5, 7, 0.28f));
            graph.addEdge(new Edge(0, 7, 0.16f));
            graph.addEdge(new Edge(1, 5, 0.32f));
            graph.addEdge(new Edge(0, 4, 0.38f));
            graph.addEdge(new Edge(2, 3, 0.17f));
            graph.addEdge(new Edge(1, 7, 0.19f));
            graph.addEdge(new Edge(0, 2, 0.26f));
            graph.addEdge(new Edge(1, 2, 0.36f));
            graph.addEdge(new Edge(1, 3, 0.29f));
            graph.addEdge(new Edge(2, 7, 0.34f));
            graph.addEdge(new Edge(6, 2, 0.40f));
            graph.addEdge(new Edge(3, 6, 0.52f));
            graph.addEdge(new Edge(6, 0, 0.58f));
            graph.addEdge(new Edge(6, 4, 0.93f));
            // 创建一个 primMST对象, 计算加权无向图中的最小树
            PrimMST prim = new PrimMST(graph);
            // 获取最小生成树中的所有边
            QueueList <Edge> queue = prim.edges();
            // 遍历打印所有边
            IEnumerator ie = queue.GetEnumerator();

            while (ie.MoveNext())
            {
                Edge e = (Edge)ie.Current;
                int  v = e.either();
                int  w = e.other(v);
                Console.WriteLine(v + "->" + w + " : " + e.weight().ToString("F2") + ",");
            }
        }
 /// <summary>
 /// Return an enumerator from IEnumerable
 /// </summary>
 /// <returns>Enumerator from IEnumerable</returns>
 IEnumerator IEnumerable.GetEnumerator()
 {
     return(QueueList.GetEnumerator());
 }
 /// <summary>
 /// Return an enumerator from IEnumerator
 /// </summary>
 /// <returns>Enumerator from IEnumerator</returns>
 public IEnumerator <T> GetEnumerator()
 {
     return(QueueList.GetEnumerator());
 }