コード例 #1
0
        public AlgorithmResult Run(IGraph graph, IAlgorithmHost host)
        {
            if (graph.Vertices.Count() == 0)
            {
                return(new AlgorithmResult(false, false));
            }

            var selectedVertex = host.GetSelectedVertex();

            var dfs = new Helpers.DepthFirstSearch(graph)
            {
                ProcessEdge = (v1, v2) =>
                {
                    if (v2.Color != VertexColor.Gray)
                    {
                        graph.ChangeColor(v2, VertexColor.Gray);
                        host.ShowCommentForLastAction($"Edge connected vertices {v1.Index} and {v2.Index} exists. Add vertex {v2.Index} to stack.");
                        host.AddToTableForLastAction(v2.Index.ToString());
                    }
                },
                ProcessVertexLate = (v) =>
                {
                    graph.ChangeColor(v, VertexColor.Black);
                    host.ShowCommentForLastAction($"Vertex {v.Index} has not unvisited adjacent vertices.");
                    host.ShowComment("Returns to last vertex.");
                    host.RemoveRowFromTableForLastAction(v.Index.ToString());
                }
            };

            graph.ChangeColor(selectedVertex, VertexColor.Gray);
            host.ShowCommentForLastAction($"Start DFS from vertex {selectedVertex.Index}.");
            host.AddToTableForLastAction(selectedVertex.Index.ToString());

            dfs.Run(selectedVertex);
            host.ShowComment("DFS has been finished.");

            return(new AlgorithmResult());
        }
コード例 #2
0
        public AlgorithmResult Run(IGraph graph, IAlgorithmHost host)
        {
            if (graph.Vertices.Count() == 0)
            {
                host.Output.WriteLine("Graph is Empty.");
                return(new AlgorithmResult(false, false));
            }

            var selectedVertex = host.GetSelectedVertex();

            var bfs = new Helpers.BreadthFirstSearch(graph)
            {
                ProcessChild = (vParent, vChild) =>
                {
                    graph.ChangeColor(vChild, VertexColor.Gray);
                    host.ShowCommentForLastAction($"Edge connected vertices {vParent.Index} and {vChild.Index} exists. Add vertex {vChild.Index} to queue.");
                    host.AddToTableForLastAction(vChild.Index.ToString());
                },
                ProcessVertexLate = (v) =>
                {
                    graph.ChangeColor(v, VertexColor.Black);
                    host.ShowCommentForLastAction($"Vertex {v.Index} has not unvisited adjacent vertices.");
                    host.ShowComment($"Remove vertex {v.Index} from queue.");
                    host.RemoveRowFromTableForLastAction(v.Index.ToString());
                }
            };

            graph.ChangeColor(selectedVertex, VertexColor.Gray);
            host.ShowCommentForLastAction($"Start BFS from vertex {selectedVertex.Index}.");
            host.AddToTableForLastAction(selectedVertex.Index.ToString());

            bfs.Run(selectedVertex);
            host.ShowComment("BFS has been finished.");

            return(new AlgorithmResult());
        }