Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // 初始化图
            SimpleGraph example_graph = new SimpleGraph();

            char[] A = { 'B' };
            char[] B = { 'A', 'C', 'D' };
            char[] C = { 'A' };
            char[] D = { 'E', 'A' };
            char[] E = { 'B' };
            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);

            breadthFirstSearch(example_graph, 'A');

            // 防止退出
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        private static void breadthFirstSearch(SimpleGraph graph, char start)
        {
            // 初始化队列
            Queue queue = new Queue();

            queue.Enqueue(start);
            Dictionary <char, bool> visited = new Dictionary <char, bool>();

            visited[start] = true;

            while (queue.Count != 0)
            {
                char current = (char)queue.Dequeue();
                Console.WriteLine("Visiting: " + current);
                foreach (char next in graph.neighbors(current))
                {
                    if (!visited.ContainsKey(next))
                    {
                        queue.Enqueue(next);
                        visited[next] = true;
                    }
                }
            }
        }