Exemplo n.º 1
0
        static void Main(string[] args)
        {
            AdjacencyList <int> graph1 = new AdjacencyList <int>();

            //添加顶点
            graph1.AddVertex(1);
            graph1.AddVertex(2);
            graph1.AddVertex(3);
            graph1.AddVertex(4);
            graph1.AddVertex(5);
            graph1.AddVertex(6);
            graph1.AddVertex(10);
            graph1.AddVertex(7);
            graph1.AddVertex(8);
            graph1.AddVertex(9);
            //添加边
            graph1.AddEdge(1, 2);
            graph1.AddEdge(1, 4);
            graph1.AddEdge(1, 3);
            graph1.AddEdge(4, 6);
            graph1.AddEdge(3, 5);
            graph1.AddEdge(10, 8);
            graph1.AddEdge(10, 7);
            graph1.AddEdge(8, 9);
            Console.WriteLine("graph1结构为:");
            Console.WriteLine(graph1.ToString());
            Console.WriteLine("深度优先遍历graph1:");
            graph1.DFSTraverse();
            Console.WriteLine();
            Console.WriteLine("广度优先遍历graph1:");
            graph1.BFSTraverse();
            int[,] LJmini = new int[6, 6];
            LJmini[0, 1]  = 1;
            LJmini[0, 3]  = 3;
            LJmini[3, 4]  = 2;
            LJmini[0, 4]  = 5;
            LJmini[0, 5]  = 5;
            LJmini[1, 2]  = 3;
            LJmini[1, 5]  = 2;
            Console.WriteLine();
            Dijkstra(LJmini, 0);
            Floyd(LJmini);
            Console.ReadKey();
        }
Exemplo n.º 2
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            AdjacencyList a = new AdjacencyList();
            //添加顶点
            //a.AddVertex("A");
            //a.AddVertex("B");
            //a.AddVertex("C");
            //a.AddVertex("D");
            ////添加边
            //a.AddEdge("A", "B");
            //a.AddEdge("A", "C");
            //a.AddEdge("A", "D");
            //a.AddEdge("B", "D");

            a.AddVertex("V1");
            a.AddVertex("V2");
            a.AddVertex("V3");
            a.AddVertex("V4");
            a.AddVertex("V5");
            a.AddVertex("V6");
            a.AddVertex("V7");
            a.AddVertex("V8");
            a.AddEdge("V1", "V2");
            a.AddEdge("V1", "V3");
            a.AddEdge("V2", "V4");
            a.AddEdge("V2", "V5");
            a.AddEdge("V3", "V6");
            a.AddEdge("V3", "V7");
            //a.AddEdge("V4", "V8");
            a.AddEdge("V5", "V8");
            a.AddEdge("V6", "V8");
            //a.AddEdge("V7", "V8");
            a.DFSTraverse();

            rtbList.AppendText(a.ToString());
            rtbList.AppendText(a.dfsPath.ToString());
        }
Exemplo n.º 3
0
        static void Main1(string[] args)
        {
            #region 显示节点的结构
            //AdjacencyList<char> a = new AdjacencyList<char>();
            ////添加顶点
            //a.AddVertex('A');
            //a.AddVertex('B');
            //a.AddVertex('C');
            //a.AddVertex('D');
            ////添加边
            //a.AddEdge('A', 'B');
            //a.AddEdge('A', 'C');
            //a.AddEdge('A', 'D');
            //a.AddEdge('B', 'D');
            //Console.WriteLine(a.ToString());
            #endregion

            #region 深度优先遍历
            //AdjacencyList<string> D = new AdjacencyList<string>();
            //D.AddVertex("V1");
            //D.AddVertex("V2");
            //D.AddVertex("V3");
            //D.AddVertex("V4");
            //D.AddVertex("V5");
            //D.AddVertex("V6");
            //D.AddVertex("V7");
            //D.AddVertex("V8");
            //D.AddEdge("V1", "V2");
            //D.AddEdge("V1", "V3");
            //D.AddEdge("V2", "V4");
            //D.AddEdge("V2", "V5");
            //D.AddEdge("V3", "V6");
            //D.AddEdge("V3", "V7");
            //D.AddEdge("V4", "V8");
            //D.AddEdge("V5", "V8");
            //D.AddEdge("V6", "V8");
            //D.AddEdge("V7", "V8");
            //D.DFSTraverse();
            #endregion

            #region 广度优先遍历
            AdjacencyList <string> B = new AdjacencyList <string>();
            B.AddVertex("V1");
            B.AddVertex("V2");
            B.AddVertex("V3");
            B.AddVertex("V4");
            B.AddVertex("V5");
            B.AddVertex("V6");
            B.AddVertex("V7");
            B.AddVertex("V8");
            B.AddEdge("V1", "V2");
            B.AddEdge("V1", "V3");
            B.AddEdge("V2", "V4");
            B.AddEdge("V2", "V5");
            B.AddEdge("V3", "V6");
            B.AddEdge("V3", "V7");
            B.AddEdge("V4", "V8");
            B.AddEdge("V5", "V8");
            B.AddEdge("V6", "V8");
            B.AddEdge("V7", "V8");
            B.BFSTraverse();
            #endregion

            Console.ReadLine();
        }