static void Main(string[] args) { Console.Title = "遍历城市网"; string[] cities = new string[] { "北京", "上海", "天津", "重庆", "武汉","深圳","成都","西安"}; AdjacencyList graph = new AdjacencyList(cities); graph.AddEdge(cities[0], cities[1]); graph.AddEdge(cities[0], cities[2]); graph.AddEdge(cities[1], cities[3]); graph.AddEdge(cities[1], cities[4]); graph.AddEdge(cities[2], cities[5]); graph.AddEdge(cities[2], cities[6]); graph.AddEdge(cities[3], cities[7]); graph.AddEdge(cities[4], cities[7]); graph.AddEdge(cities[5], cities[6]); graph.DepthFirstSearch(); Console.ReadLine(); }
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(); }
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()); }
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(); }