示例#1
0
        static void Main(string[] args)
        {
            var graph = new Graph(9);

            graph.AddEdge(0, 1); graph.AddEdge(0, 2); graph.AddEdge(0, 5); graph.AddEdge(0, 6);
            graph.AddEdge(3, 4); graph.AddEdge(3, 5);
            graph.AddEdge(4, 5); graph.AddEdge(4, 6);
            graph.AddEdge(7, 8);

            var dfs = new DFS(graph, 0);

            Console.WriteLine($"Path 0-3 : {string.Join(",", dfs.PathTo(3))}");
            Console.WriteLine($"Path 0-2 : {string.Join(",", dfs.PathTo(2))}");
            Console.WriteLine($"Path 0-4 : {string.Join(",", dfs.PathTo(4))}");
            Console.WriteLine($"Path 0-7 : {string.Join(",", dfs.PathTo(7))}");

            Console.ReadLine();
        }
示例#2
0
        public void DFS_Test()
        {
            //Graph2 g = new Graph2(4);

            //g.AddEdge(0, 1);
            //g.AddEdge(0, 2);
            //g.AddEdge(1, 2);
            //g.AddEdge(2, 0);
            //g.AddEdge(2, 3);
            //g.AddEdge(3, 3);

            //Debug.WriteLine("Following is Breadth First Traversal " +
            //				   "(starting from vertex 2)");

            //g.BreadthFirstSearch(2);
            var graph = GraphSeeder.GetTestStructure();

            DFS bfs = new DFS();

            bfs.Search("People.aspx", graph);
        }
        static void Main(string[] args)
        {
            Graph <int> graph = new Graph <int>(true, true);

            Node <int> n1 = graph.AddNode(1);
            Node <int> n2 = graph.AddNode(2);
            Node <int> n3 = graph.AddNode(3);
            Node <int> n4 = graph.AddNode(4);
            Node <int> n5 = graph.AddNode(5);
            Node <int> n6 = graph.AddNode(6);
            Node <int> n7 = graph.AddNode(7);
            Node <int> n8 = graph.AddNode(8);

            graph.AddEdge(n1, n2, 9);
            graph.AddEdge(n1, n3, 5);
            graph.AddEdge(n2, n1, 3);
            graph.AddEdge(n2, n4, 18);
            graph.AddEdge(n3, n4, 12);
            graph.AddEdge(n4, n2, 2);
            graph.AddEdge(n4, n8, 8);
            graph.AddEdge(n5, n4, 9);
            graph.AddEdge(n5, n6, 2);
            graph.AddEdge(n5, n7, 5);
            graph.AddEdge(n5, n8, 3);
            graph.AddEdge(n6, n7, 1);
            graph.AddEdge(n7, n5, 4);
            graph.AddEdge(n7, n8, 6);
            graph.AddEdge(n8, n5, 3);

            //BFS<int> bfs = new BFS<int>();
            //List<Node<int>> bfsNodes = bfs.Search(graph);
            //bfsNodes.ForEach(x => Console.WriteLine(x));

            DFS <int>          dfs      = new DFS <int>();
            List <Node <int> > dfsNodes = dfs.Search(graph);

            dfsNodes.ForEach(x => Console.WriteLine(x));
        }