Example #1
0
        static void Main(string[] args)
        {
            Graph graph = new Graph();

            graph.AddEdge(1, 2);
            graph.AddEdge(1, 3);

            graph.AddEdge(2, 7);

            graph.AddEdge(3, 4);
            graph.AddEdge(3, 5);

            graph.AddEdge(4, 5);
            graph.AddEdge(4, 6);

            graph.AddEdge(6, 8);

            graph.AddEdge(7, 5);

            graph.Print();

            List <Graph.Node> traversal = graph.BFS(graph.Nodes[2]);

            Console.Write("BFS: ");
            traversal.ForEach(item => Console.Write(item.Value + ", "));
            Console.WriteLine();
            Console.Write("DFS: ");
            List <Graph.Node> dfstraversal = graph.DFS(graph.Nodes[4]);

            dfstraversal.ForEach(item => Console.Write(item.Value + ", "));
        }
Example #2
0
        static void Main(string[] args)
        {
            Dictionary <int, List <int> > input = GraphHelper.Parse(@".\input.txt");
            Graph graph = new Graph(input);

            graph.BFS();
            graph.PrintTree();
        }
        static void Main(string[] args)
        {
            int[,] input = GraphHelper.Parse(@".\input.txt");
            Graph graph = new Graph(input);

            graph.BFS();
            graph.PrintTree();
        }
Example #4
0
        static void Main(string[] args)
        {
            Graph graph = new Graph();
            int   N     = Int32.Parse(Console.ReadLine());

            for (int i = 0; i < N - 1; i++)
            {
                string str = Console.ReadLine();
                int    u   = Int32.Parse(str.Split(' ')[0]);
                int    v   = Int32.Parse(str.Split(' ')[1]);
                graph.addEdge(u, v);
            }
            int x = Int32.Parse(Console.ReadLine());

            graph.BFS(x);
        }
Example #5
0
        // Driver code
        static void Main(string[] args)
        {
            Graph g = new Graph(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);

            Console.Write("Following is Breadth First " +
                          "Traversal(starting from " +
                          "vertex 2)\n");
            g.BFS(2);
        }
Example #6
0
        // Driver method to test
        static void Main(string[] args)
        {
            // Create a graph with 4 node or vertices
            Graph g = new Graph(4);

            // Connect a vertex with other vertex with an edge
            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(3, 3);

            // Traverse the graph
            Console.WriteLine("Following is Breadth First Traversal (starting from vertex 2)");
            g.BFS(2);

            // Output
            // Following is Breadth First Traversal (starting from vertex 2)
            // 2 0 3 1

            // Create a graph with 4 node or vertices
            Graph h = new Graph(6);


            // Connect a vertex with other vertex with an edge
            h.AddEdge(0, 1);
            h.AddEdge(0, 2);
            h.AddEdge(1, 0);
            h.AddEdge(1, 3);
            h.AddEdge(1, 4);
            h.AddEdge(2, 4);
            h.AddEdge(3, 4);
            h.AddEdge(3, 4);
            h.AddEdge(4, 5);

            // Traverse the graph
            Console.WriteLine("\nFollowing is Breadth First Traversal (starting from vertex 1)");
            h.BFS(0);

            // Output
            // Following is Breadth First Traversal (starting from vertex 0)
            // 0 1 2 3 4 5

            Console.ReadKey();
        }
Example #7
0
        static void Main(string[] args)
        {
            Graph g = new Graph(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);

            const int vertex = 1;

            Console.WriteLine("Following is Breadth First Traversal " + "(starting from vertex {0})", vertex);
            g.BFS(vertex);
            Console.ReadLine();
        }
Example #8
0
        static void Main(string[] args)
        {
            int queries = int.Parse(Console.ReadLine());

            for (int i = 0; i < queries; i++)
            {
                int[] numbers = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
                int   n       = numbers[0],
                      edges   = numbers[1];
                Graph g       = new Graph(n);

                for (int j = 0; j < edges; j++)
                {
                    numbers = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
                    g.AddEdge(numbers[0] - 1, numbers[1] - 1);
                }

                int    startV = int.Parse(Console.ReadLine()) - 1;
                int?[] previousVertex;
                int[]  distance;

                g.BFS(startV, out previousVertex, out distance);

                List <int> distanceList = distance.Select(number =>
                {
                    if (number == int.MaxValue)
                    {
                        return(-1);
                    }

                    return(number * 6);
                }).ToList();
                distanceList.RemoveAt(startV);

                Console.WriteLine(string.Join(" ", distanceList.Select(number => number.ToString()).ToArray()));
            }
        }