예제 #1
0
        private static void breadthFirstSearch(SimpleGraph graph, string start)
        {
            // 初始化队列
            Queue <string> queue = new Queue <string>();

            queue.Enqueue(start);
            // 记录访问过的节点
            Dictionary <string, bool> visited = new Dictionary <string, bool>();

            visited[start] = true;

            while (queue.Count != 0)
            {
                string current = queue.Dequeue();
                Console.WriteLine("当前访问节点: " + current);
                foreach (var next in graph.neighbors(current))
                {
                    if (!visited.ContainsKey(next))
                    {
                        queue.Enqueue(next);
                        visited[next] = true;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 图的广度遍历
        /// </summary>
        static void Main(string[] args)
        {
            // 初始化图
            SimpleGraph example_graph = new SimpleGraph();

            #region 全连接图
            List <string> A = new List <string> {
                "B", "C"
            };
            List <string> B = new List <string> {
                "A", "E", "D"
            };
            List <string> C = new List <string> {
                "A", "F", "G"
            };
            List <string> D = new List <string> {
                "E", "B"
            };
            List <string> E = new List <string> {
                "B", "H", "D"
            };
            List <string> F = new List <string> {
                "H", "C", "G"
            };
            List <string> G = new List <string> {
                "F", "C"
            };
            List <string> H = new List <string> {
                "E", "F"
            };
            #endregion

            #region  向图 Key->value表示 当前节点 可到达的 相邻节点
            //List<string> A = new List<string>{ "B" };
            //List<string> B = new List<string>{ };
            //List<string> C = new List<string>{ "A", "F", "G" };
            //List<string> D = new List<string>{ };
            //List<string> E = new List<string>{ "B", "D" };
            //List<string> F = new List<string>{ "H" };
            //List<string> G = new List<string>{ };
            //List<string> H = new List<string>{ "E" };
            #endregion

            example_graph.edges.Add("A", A);
            example_graph.edges.Add("B", B);
            example_graph.edges.Add("C", C);
            example_graph.edges.Add("D", D);
            example_graph.edges.Add("E", E);
            example_graph.edges.Add("F", F);
            example_graph.edges.Add("G", G);
            example_graph.edges.Add("H", H);


            string input = Console.ReadLine();
            breadthFirstSearch(example_graph, input);
            Console.ReadKey();
        }
예제 #3
0
        /// <summary>
        /// 图的广度遍历
        /// </summary>
        static void Main(string[] args)
        {
            // 初始化图
            SimpleGraph example_graph = new SimpleGraph();

            #region 全连接图
            //char[] A = { 'B','C' };
            //char[] B = { 'A', 'E', 'D' };
            //char[] C = { 'A', 'F', 'G' };
            //char[] D = { 'E', 'B' };
            //char[] E = { 'B','H','D' };
            //char[] F = { 'H', 'C', 'G' };
            //char[] G = { 'F', 'C' };
            //char[] H = { 'E', 'F' };
            #endregion

            #region  向图 Key->value表示 当前节点 可到达的 相邻节点
            List <string> A = new List <string> {
                "B"
            };
            List <string> B = new List <string> {
            };
            List <string> C = new List <string> {
                "A", "F", "G"
            };
            List <string> D = new List <string> {
            };
            List <string> E = new List <string> {
                "B", "D"
            };
            List <string> F = new List <string> {
                "H"
            };
            List <string> G = new List <string> {
            };
            List <string> H = new List <string> {
                "E"
            };
            #endregion

            example_graph.edges.Add("A", A);
            example_graph.edges.Add("B", B);
            example_graph.edges.Add("C", C);
            example_graph.edges.Add("D", D);
            example_graph.edges.Add("E", E);
            example_graph.edges.Add("F", F);
            example_graph.edges.Add("G", G);
            example_graph.edges.Add("H", H);


            string input = Console.ReadLine();
            breadthFirstSearch(example_graph, input);
            // 防止退出
            Console.ReadKey();
        }
예제 #4
0
 private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         SquareMatrix matrix = new SquareMatrix(txt_matrix.Lines, '\t');
         adjGraph = new SimpleGraph(matrix);
         adjGraph.autoPosition     = rdAuto.Checked;
         adjGraph.circularPosition = rdCircular.Checked;
         txt_info.Text             = adjGraph.GetInfo();
         Redraw();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }