コード例 #1
0
        public void AddEdgeTest()
        {
            /*
             *      A-----B\
             *      |\     E
             *      | \   /
             *      C-  D/
             */

            Node <string> a = new Node <string>("A");
            Node <string> b = new Node <string>("B");
            Node <string> c = new Node <string>("C");
            Node <string> d = new Node <string>("D");
            Node <string> e = new Node <string>("E");

            Node <string>[] myNodes = new Node <string>[] {
                a, b, c, d, e
            };
            GraphAdjacentList <string> gh = new GraphAdjacentList <string>(myNodes);

            gh.AddEdge(a, b, 1);
            gh.AddEdge(a, b, 1);

            gh.AddEdge(b, e, 1);
            gh.AddEdge(e, d, 1);
            gh.AddEdge(d, c, 1);
            gh.AddEdge(c, a, 1);
            gh.AddEdge(a, d, 1);

            AdjacentListNode p = gh[0].FirstAdjacentListNode;

            Assert.IsNotNull(p);
            Assert.AreEqual(1, p.AdjacentVertexNodeIndex);
            p = p.Next;
            Assert.IsNotNull(p);
            Assert.AreEqual(2, p.AdjacentVertexNodeIndex);

            p = p.Next;
            Assert.IsNotNull(p);
            Assert.AreEqual(3, p.AdjacentVertexNodeIndex);
        }
コード例 #2
0
        private static void TestGrapBFSTravesal()
        {
            //     0
            //    /  \
            //   1    2
            //        /
            //       3
            UndirectedGraph<int> graph = new UndirectedGraph<int>();

            AdjacentListNode<int> node = new AdjacentListNode<int>(0);

            AdjacentListNode<int> node1 = new AdjacentListNode<int>(1);

            AdjacentListNode<int> node2 = new AdjacentListNode<int>(2);

            AdjacentListNode<int> node3 = new AdjacentListNode<int>(3);

            // Disconnected node
            AdjacentListNode<int> node4 = new AdjacentListNode<int>(4);

            // Add immediated neighbors of Node 1
            node.Neighbors.Add(node1);
            node.Neighbors.Add(node2);

            node1.Neighbors.Add(node);

            // Add immediate neighbors of node 2
            node2.Neighbors.Add(node3);
            node2.Neighbors.Add(node);

            node3.Neighbors.Add(node2);

            List<AdjacentListNode<int>> allNodes = new List<AdjacentListNode<int>>()
            {
            node,
            node1,
            node3,
            node2,
            node4
            };

            // Traverse all nodes and its vertices
            Console.WriteLine("/*********** BFS Traversal *************/");
            graph.BFSAdjacentList(allNodes);
        }