/// <summary>
        ///GraphAdjMatrix`1 构造函数 的测试
        ///</summary>
        public void GraphAdjMatrixConstructorTestHelper <T>()
        {
            int n = 5; // TODO: 初始化为适当的值
            GraphAdjMatrix <T> target = new GraphAdjMatrix <T>(n);

            Assert.AreEqual(target.GetNumOfEdge(), 0);
        }
Пример #2
0
        public void AssertCanNotSearch()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 8);
            graph.AddEdge(6, 7);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 5);


            graph.AddEdge(0, 9);
            graph.AddEdge(9, 10);
            graph.AddEdge(10, 11);


            var bfs    = new BreadthFirstSearch(graph);
            var result = bfs.CanFind(0, 12);

            Assert.IsFalse(result);
        }
Пример #3
0
        public void AssertCanSearch()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 8);
            graph.AddEdge(6, 7);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 5);


            graph.AddEdge(0, 9);
            graph.AddEdge(9, 10);
            graph.AddEdge(10, 11);
            graph.AddEdge(11, 12);

            var bfs    = new BreadthFirstSearch(graph);
            var result = bfs.CanFind(0, 12);

            Assert.IsTrue(result);

            var direction = bfs.PathToGoal();

            Assert.AreEqual(12, direction[4]);
            Assert.AreEqual(11, direction[3]);
            Assert.AreEqual(10, direction[2]);
            Assert.AreEqual(9, direction[1]);
            Assert.AreEqual(0, direction[0]);
        }
        public void AssertCanSearchShortestPathWithMultipleParents()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6 });

            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 3, 3);
            graph.AddEdge(1, 2, 1);
            graph.AddEdge(2, 4, 1);
            graph.AddEdge(4, 6, 4);
            graph.AddEdge(3, 5, 3);
            graph.AddEdge(5, 6, 1);
            graph.AddEdge(3, 6, 5);

            var bfs    = new DijkstraShortestPath(graph);
            var result = bfs.CanFind(0, 6);

            Assert.IsTrue(result);

            var direction = bfs.PathToGoal();

            Assert.AreEqual(0, direction[0]);
            Assert.AreEqual(3, direction[1]);
            Assert.AreEqual(5, direction[2]);
            Assert.AreEqual(6, direction[3]);
        }
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion



        /// <summary>
        ///SetNode 的测试
        ///</summary>
        public void SetNodeTestHelper <T>()
        {
            int n = 3;                                             // TODO: 初始化为适当的值
            GraphAdjMatrix <T> target = new GraphAdjMatrix <T>(n); // TODO: 初始化为适当的值
            int      index            = 0;                         // TODO: 初始化为适当的值
            Node <T> node             = null;                      // TODO: 初始化为适当的值

            target.SetNode(index, node);

            Assert.IsNull(target.GetNode(0));
        }
Пример #6
0
    void Start()
    {
        nodes = new Node <string> [5];

        graph = new GraphAdjMatrix <string>(5);

        shortPath = new int[5];

        nodes[0] = new Node <string>("a");
        graph.SetNode(0, nodes[0]);
        nodes[1] = new Node <string>("b");
        graph.SetNode(1, nodes[1]);
        nodes[2] = new Node <string>("c");
        graph.SetNode(2, nodes[2]);
        nodes[3] = new Node <string>("d");
        graph.SetNode(3, nodes[3]);
        nodes[4] = new Node <string>("e");
        graph.SetNode(4, nodes[4]);


        graph.SetEdge(graph.GetNode(0), graph.GetNode(1), 100);
        graph.SetEdge(graph.GetNode(0), graph.GetNode(3), 150);
        graph.SetEdge(graph.GetNode(0), graph.GetNode(4), 200);
        graph.SetEdge(graph.GetNode(1), graph.GetNode(2), 50);
        graph.SetEdge(graph.GetNode(2), graph.GetNode(3), 75);
        graph.SetEdge(graph.GetNode(3), graph.GetNode(4), 100);


        //for (int i = 0; i < graph.GetNumOfVertex(); i++)
        //{
        //    Debug.Log(graph.GetNode(i).Data);
        //}


        //for (int i = 0; i < graph.GetNumOfVertex(); i++)
        //{
        //    for (int j = 0; j < graph.GetNumOfVertex(); j++)
        //    {
        //        Debug.Log(graph.GetEdge(i, j));
        //    }
        //}


        //graph.Dijkstra(ref shortPath, nodes[0]);

        //for (int i = 0; i < shortPath.Length; i++)
        //{
        //    Debug.Log(shortPath[i]);
        //}

        //graph.DFSTraverse();

        //graph.BFSTraverse();
    }
Пример #7
0
        public void Create_Graph_With_6_Nodes_Add_Link_From_0To1_Assert_AreLinked_ZeroToOne()
        {
            var graph = new GraphAdjMatrix(new int[] { 0, 1, 2, 3, 4, 5 });

            Assert.AreEqual(6, graph.NumberOfVertices);

            graph.AddDirectedEdge(0, 1);
            var neighbours = graph.GetNeighbours(0);

            Assert.AreEqual(1, neighbours[0]);
        }
Пример #8
0
        public void TestAdjacencyMatrixGraphRepresentation()
        {
            GraphAdjMatrix <string> graphAdjMatrix = new GraphAdjMatrix <string>(4);

            graphAdjMatrix.AddNode(0, 1, "A");
            graphAdjMatrix.AddNode(0, 2, "B");
            graphAdjMatrix.AddNode(1, 2, "C");
            graphAdjMatrix.AddNode(2, 0, "D");
            graphAdjMatrix.AddNode(2, 3, "E");
            graphAdjMatrix.AddNode(3, 3, "F");

            //string dfs = graphAdjMatrix.BFS(2).Trim();
        }
        public void MyTestInitialize()
        {
            //设置这样的图

            /*
             *       v1-----v2
             *       |     /  |
             *       |    v4  |
             *       |   /  \ |
             *       |  /    \|
             *       v3/      v5
             *
             */

            graphAdjMatrix = new GraphAdjMatrix <string>(5);

            //设置节点
            v1 = new Node <string>("v1");
            v2 = new Node <string>("v2");
            v3 = new Node <string>("v3");
            v4 = new Node <string>("v4");
            v5 = new Node <string>("v5");

            graphAdjMatrix.SetNode(0, v1);
            graphAdjMatrix.SetNode(1, v2);
            graphAdjMatrix.SetNode(2, v3);
            graphAdjMatrix.SetNode(3, v4);
            graphAdjMatrix.SetNode(4, v5);

            //设置边
            graphAdjMatrix.AddEdge(v1, v2, 1);
            graphAdjMatrix.AddEdge(v1, v3, 1);

            graphAdjMatrix.AddEdge(v2, v4, 1);
            graphAdjMatrix.AddEdge(v2, v5, 1);


            graphAdjMatrix.AddEdge(v1, v2, 1);
            graphAdjMatrix.AddEdge(v1, v3, 1);


            graphAdjMatrix.AddEdge(v3, v4, 1);
            graphAdjMatrix.AddEdge(v4, v5, 1);
        }
        public void AssertCanSearchSomeWhereInMiddle()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);
            graph.AddEdge(4, 5);
            graph.AddEdge(5, 6);
            graph.AddEdge(5, 8);
            graph.AddEdge(6, 7);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 5);


            graph.AddEdge(0, 9);
            graph.AddEdge(9, 10);
            graph.AddEdge(10, 11);
            graph.AddEdge(11, 12);

            var dfs    = new DepthFirstSearch(graph);
            var result = dfs.CanFind(0, 8);

            Assert.IsTrue(result);

            var direction = dfs.PathToGoal();

            Assert.AreEqual(8, direction[8]);
            Assert.AreEqual(7, direction[7]);
            Assert.AreEqual(6, direction[6]);
            Assert.AreEqual(5, direction[5]);
            Assert.AreEqual(4, direction[4]);
            Assert.AreEqual(3, direction[3]);
            Assert.AreEqual(2, direction[2]);
            Assert.AreEqual(1, direction[1]);
            Assert.AreEqual(0, direction[0]);
        }
Пример #11
0
        public void CreateGraph()
        {
            var graph = new GraphAdjMatrix<string>(5);
            var vertex = new List<Node<string>>
                {
                    new Node<string>("A"),
                    new Node<string>("B"),
                    new Node<string>("C"),
                    new Node<string>("D"),
                    new Node<string>("E"),
                };
            graph.Nodes.AddRange(vertex);

            graph.SetEdge("A", "B", 60);
            graph.SetEdge("A", "C", 100);
            graph.SetEdge("A", "D", 20);
            graph.SetEdge("B", "C", 80);
            graph.SetEdge("B", "D", 95);
            graph.SetEdge("C", "E", 70);
            graph.SetEdge("D", "E", 10);

            graph.Print();
        }
        public void AssertCanSearch()
        {
            IGraph graph = new GraphAdjMatrix(new[] { 0, 1, 2, 3, 4 });

            graph.AddEdge(0, 1, 2);
            graph.AddEdge(1, 3, 1);
            graph.AddEdge(1, 4, 6);
            graph.AddEdge(2, 3, 2);
            graph.AddEdge(3, 4, 1);

            var dks    = new DijkstraShortestPath(graph);
            var result = dks.CanFind(0, 4);

            Assert.IsTrue(result);



            var direction = dks.PathToGoal();

            Assert.AreEqual(4, direction[3]);
            Assert.AreEqual(3, direction[2]);
            Assert.AreEqual(1, direction[1]);
            Assert.AreEqual(0, direction[0]);
        }
Пример #13
0
        public void CreateGraph()
        {
            var graph  = new GraphAdjMatrix <string>(5);
            var vertex = new List <Node <string> >
            {
                new Node <string>("A"),
                new Node <string>("B"),
                new Node <string>("C"),
                new Node <string>("D"),
                new Node <string>("E"),
            };

            graph.Nodes.AddRange(vertex);

            graph.SetEdge("A", "B", 60);
            graph.SetEdge("A", "C", 100);
            graph.SetEdge("A", "D", 20);
            graph.SetEdge("B", "C", 80);
            graph.SetEdge("B", "D", 95);
            graph.SetEdge("C", "E", 70);
            graph.SetEdge("D", "E", 10);

            graph.Print();
        }
Пример #14
0
        public void Create_Graph_With_7_Nodes_One_At_A_Time()
        {
            var graph = new GraphAdjMatrix(new int[] { 0 });

            graph.AddVertex();
            graph.AddVertex();
            graph.AddDirectedEdge(1, 2);
            graph.AddDirectedEdge(2, 0);
            graph.AddVertex();
            graph.AddDirectedEdge(0, 3);
            graph.AddVertex();
            graph.AddDirectedEdge(3, 4);
            graph.AddVertex();
            graph.AddDirectedEdge(5, 4);
            graph.AddVertex();
            graph.AddDirectedEdge(6, 5);
            graph.AddDirectedEdge(6, 4);

            var zerosNeighbours   = graph.GetNeighbours(0);
            var onesNeighbours    = graph.GetNeighbours(1);
            var twosNeighbours    = graph.GetNeighbours(2);
            var threessNeighbours = graph.GetNeighbours(3);
            var foursNeighbours   = graph.GetNeighbours(4);
            var fivesNeighbours   = graph.GetNeighbours(5);
            var sixsNeighbours    = graph.GetNeighbours(6);

            Assert.AreEqual(3, zerosNeighbours[0]);
            Assert.IsTrue(sixsNeighbours.Contains(5));
            Assert.IsTrue(sixsNeighbours.Contains(4));
            Assert.IsTrue(onesNeighbours.Contains(2));

            Assert.IsTrue(twosNeighbours.Contains(0));
            Assert.IsTrue(threessNeighbours.Contains(4));
            Assert.IsTrue(foursNeighbours.Count == 0);
            Assert.IsTrue(fivesNeighbours.Contains(4));
        }
Пример #15
0
        public void Create_Graph_With_6_Nodes_Assert_NumberOfVertex_6()
        {
            var graph = new GraphAdjMatrix(new int[] { 0, 1, 2, 3, 4, 5 });

            Assert.AreEqual(6, graph.NumberOfVertices);
        }