Example #1
0
        public void Should_Add_correct_number_to_Vertex_when_make_BFS(string vertex, int value)
        {
            //Stub
            createGraph1();

            //Test
            _graph.BreadthFirstSearch();

            _graph._bfs.ContainsKey(vertex).Should().BeTrue();
            _graph._bfs[vertex].Should().Be(value);
        }
Example #2
0
        static void Main(string[] args)
        {
            int[,] graphArray = //несвзянный граф из лекции, добавлен путь A-С
            {
                { 0, 1, 1, 0, 0, 0, 0 },
                { 1, 0, 1, 1, 0, 0, 0 },
                { 1, 1, 0, 1, 0, 0, 0 },
                { 0, 1, 1, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 1 },
                { 0, 0, 0, 0, 0, 1, 0 }

                //{0, 1, 0, 1, 0, 0, 0},
                //{1, 0, 1, 0, 0, 0, 0},
                //{0, 1, 0, 1, 0, 0, 0},
                //{1, 0, 1, 0, 0, 0, 0},
                //{0, 0, 0, 0, 0, 0, 0},
                //{0, 0, 0, 0, 0, 0, 1},
                //{0, 0, 0, 0, 0, 1, 0}
            };

            Graph graph = new Graph(graphArray);

            Action <int> action = Console.WriteLine;

            Console.WriteLine("Обход в ширину");
            graph.BreadthFirstSearch(3, action);

            Console.WriteLine("Обход в глубину");
            graph.DepthFirstSearch(2, action);

            Console.WriteLine("Обход в глубину с Рекурсией");
            graph.DepthFirstSearchRecursion(2, action);
        }
        static void BFSMain()
        {
            Graph aGraph = new Graph();

            aGraph.AddVertex("A");
            aGraph.AddVertex("B");
            aGraph.AddVertex("C");
            aGraph.AddVertex("D");
            aGraph.AddVertex("E");
            aGraph.AddVertex("F");
            aGraph.AddVertex("G");
            aGraph.AddVertex("H");
            aGraph.AddVertex("I");
            aGraph.AddVertex("J");
            aGraph.AddVertex("K");
            aGraph.AddVertex("L");
            aGraph.AddVertex("M");
            aGraph.AddEdge(0, 1);
            aGraph.AddEdge(1, 2);
            aGraph.AddEdge(2, 3);
            aGraph.AddEdge(0, 4);
            aGraph.AddEdge(4, 5);
            aGraph.AddEdge(5, 6);
            aGraph.AddEdge(0, 7);
            aGraph.AddEdge(7, 8);
            aGraph.AddEdge(8, 9);
            aGraph.AddEdge(0, 10);
            aGraph.AddEdge(10, 11);
            aGraph.AddEdge(11, 12);
            //aGraph.DepthFirstSearch();
            //Console.WriteLine();

            aGraph.BreadthFirstSearch();
            Console.WriteLine();
        }
Example #4
0
        static void Main(string[] args)
        {
            //ifstream inputs;
            string[] lines = System.IO.File.ReadAllLines(@"data\input1.txt");
            Graph    gptr  = null;

            int            w;
            Queue <string> visitedq = new Queue <string>();
            Queue <string> adjq     = new Queue <string>();


            foreach (string line in lines)
            {
                List <string> inputs = new List <string>(line.Split(" "));

                for (int i = 0; i < inputs.Count; i++)
                {
                    if (inputs[i] == "")
                    {
                        inputs.RemoveAt(i);
                        i--;
                    }
                }
                switch (inputs[0])
                {
                case "#":     //comment
                    try
                    {
                        Console.WriteLine(line);
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception");
                    }
                    break;

                case "c":     //construct empty graph object

                    try
                    {
                        Console.WriteLine("Constructor()");
                        gptr = new Graph();
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception");
                    }
                    break;

                case "d":     //Add directed edge to graph object
                    try
                    {
                        Console.WriteLine("AddEdge(" + inputs[1] + ", " + inputs[2] + ", " + inputs[3] + ")");
                        gptr.AddEdge(inputs[1], inputs[2], Convert.ToInt32(inputs[3]));
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in AddEdge");
                    }
                    break;

                case "u":     // Add undirected edge to graph object

                    try
                    {
                        Console.WriteLine("AddEdge(" + inputs[1] + ", " + inputs[2] + ", " + inputs[3] + ")");
                        gptr.AddEdge(inputs[1], inputs[2], Convert.ToInt32(inputs[3]));
                        Console.WriteLine("AddEdge(" + inputs[1] + ", " + inputs[2] + ", " + inputs[3] + ")");
                        gptr.AddEdge(inputs[2], inputs[1], Convert.ToInt32(inputs[3]));
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in AddEdge");
                    }
                    break;

                case "?":
                    try
                    {
                        if (gptr.VertexExists(inputs[1]))
                        {
                            Console.WriteLine("VertexExist (" + inputs[1] + ") -- true");
                        }
                        else
                        {
                            Console.WriteLine("VertexExist (" + inputs[1] + ") -- false");
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in VertexExist");
                    }
                    break;

                case "!":     //edge exist
                    try
                    {
                        if (gptr.EdgeExists(inputs[1], inputs[2]))
                        {
                            Console.WriteLine("EdgeExist (" + inputs[1] + "," + inputs[2] + ") -- true");
                        }
                        else
                        {
                            Console.WriteLine("EdgeExist (" + inputs[1] + "," + inputs[2] + ") -- false");
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in EdgeExist");
                    }
                    break;

                case "w":       // WeightIs()
                    try
                    {
                        Console.WriteLine("WeightIs(" + inputs[1] + ", " + inputs[2] + ") -- ");
                        w = gptr.WeightIs(inputs[1], inputs[2]);
                        Console.WriteLine(w);
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch (GraphEdgeNotFound)
                    {
                        Console.WriteLine("Error: edge not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in WeightIs");
                    }
                    break;

                case "m":       // MarkVertex()
                    try
                    {
                        Console.WriteLine("MarkVertex(" + inputs[1] + ") ");
                        gptr.MarkVertex(inputs[1]);
                        Console.WriteLine();
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in MarkVertex");
                    }
                    break;

                case "e":       // ClearMarks()
                    try
                    {
                        Console.WriteLine("ClearMarks() ");
                        gptr.ClearMarks();
                        Console.WriteLine();
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in ClearMarks");
                    }
                    break;

                case "i":       // IsMarked()
                    try
                    {
                        Console.WriteLine("IsMarked(" + inputs[1] + ") -- ");
                        if (gptr.IsMarked(inputs[1]))
                        {
                            Console.WriteLine("true");
                        }
                        else
                        {
                            Console.WriteLine("false");
                        }
                        Console.WriteLine();
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in IsMarked");
                    }
                    break;

                case "s":       // Perform Depth-First Search
                    try
                    {
                        Console.WriteLine("DFS( " + inputs[1] + ", " + inputs[2] + " ) -- ");
                        gptr.DepthFirstSearch(inputs[1], inputs[2], visitedq);

                        if (visitedq.Count == 0)
                        {
                            Console.WriteLine("No path found");
                        }
                        else
                        {
                            Console.WriteLine(" { ");
                            while (visitedq.Count != 0)
                            {
                                Console.WriteLine(visitedq.Peek() + " ");
                                visitedq.Dequeue();
                            }
                            Console.WriteLine("}");
                        }
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in DFS");
                    }
                    visitedq = new Queue <string>();
                    break;

                case "g":       // GetToVertices
                    try
                    {
                        Console.WriteLine("GetToVertices( " + inputs[1] + " ) -- ");
                        gptr.GetToVertices(inputs[1], adjq);

                        if (adjq.Count == 0)
                        {
                            Console.WriteLine("No adjacent vertices found");
                        }
                        else
                        {
                            Console.WriteLine(" { ");
                            while (adjq.Count != 0)
                            {
                                Console.WriteLine(adjq.Peek() + " ");
                                adjq.Dequeue();
                            }
                            Console.WriteLine("}");
                        }
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in GetToVertices");
                    }
                    while (adjq.Count != 0)
                    {
                        adjq.Dequeue();
                    }
                    break;

                case "b":       // Perform Breadth-First Search
                    try
                    {
                        Console.WriteLine("BFS( " + inputs[1] + ", " + inputs[2] + " ) -- ");
                        gptr.BreadthFirstSearch(inputs[1], inputs[2], visitedq);

                        if (adjq.Count == 0)
                        {
                            Console.WriteLine("No path found");
                        }
                        else
                        {
                            Console.WriteLine(" { ");
                            while (adjq.Count != 0)
                            {
                                Console.WriteLine(visitedq.Peek() + " ");
                                visitedq.Dequeue();
                            }
                            Console.WriteLine("}");
                        }
                    }
                    catch (GraphVertexNotFound)
                    {
                        Console.WriteLine("Error: vertex not found");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception in BFS");
                    }

                    visitedq = new Queue <string>();
                    break;

                case "p":       // Print Graph
                    gptr.Print();
                    break;

                case "~":       // Destructor
                    try
                    {
                        gptr = null;
                        Console.WriteLine("Destructor()");
                    }
                    catch
                    {
                        Console.WriteLine("Error: unanticipated exception");
                    }
                    break;
                }
            }
        }