Example #1
0
        // For recursive DFS, we need the graph, the starting node, and the list of visited nodes
        public void DFS(BFS_DFS graph, char currentNode, bool[] visitedNodes)
        {
            // Visit current node and print
            visitedNodes[(int)(currentNode - 65)] = true;

            Console.Write(currentNode + " ");

            // Search adjacency list for next unvisited node
            foreach (var node in graph._nodeLink[(int)(currentNode - 65)])
            {
                // If node has not been visited, run DFS starting from that node
                if (!visitedNodes[(int)(node - 65)])
                {
                    DFS(graph, node, visitedNodes);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            //Create graph and add all nodes and adjacency lists
            BFS_DFS graph = new BFS_DFS(6);

            graph.LinkNodes('A', 'B');
            graph.LinkNodes('A', 'C');

            graph.LinkNodes('B', 'A');
            graph.LinkNodes('B', 'C');
            graph.LinkNodes('B', 'D');

            graph.LinkNodes('C', 'A');
            graph.LinkNodes('C', 'B');
            graph.LinkNodes('C', 'D');
            graph.LinkNodes('C', 'E');

            graph.LinkNodes('D', 'B');
            graph.LinkNodes('D', 'C');
            graph.LinkNodes('D', 'E');
            graph.LinkNodes('D', 'F');

            graph.LinkNodes('E', 'C');
            graph.LinkNodes('E', 'D');
            graph.LinkNodes('E', 'F');

            graph.LinkNodes('F', 'D');
            graph.LinkNodes('F', 'E');

            Console.WriteLine("BFS Search of graph");
            graph.BFS();

            Console.WriteLine();

            Console.WriteLine("DFS Search of graph");
            graph.DFS(graph, 'A', new bool[graph._nodes]);

            Console.ReadLine();
        }