public AlgorithmResult Run(IGraph graph, IAlgorithmHost host)
 {
     foreach (var v in graph.Vertices)
     {
         v.X = _random.Next((int)host.MinElementX, (int)(host.MaxElementX - host.MinElementX));
         v.Y = _random.Next((int)host.MinElementY, (int)(host.MaxElementY - host.MinElementY));
     }
     return(new AlgorithmResult(true, false));
 }
        public Task <bool> RunAsync(IAlgorithm algorithm, IGraph graph, IAlgorithmHost host)
        {
            _algorithmTask = Task.Factory.StartNew <bool>(() =>
            {
                IsAlgorithmExecuting = true;
                var res = algorithm?.Run(graph, host);

                if (!res.ExecuteStepByStep || _undoRedoStartPosition == UndoRedoManager.Instance.Position)
                {
                    Stop(res.SaveChanges);
                    return(true);
                }
                else
                {
                    Restart();
                    return(false);
                }
            }, _algorithmTaskCancellationTokenSource.Token);
            return(_algorithmTask);
        }
        public AlgorithmResult Run(IGraph graph, IAlgorithmHost host)
        {
            var width    = host.MaxElementX;
            var height   = host.MaxElementY;
            var vertices = graph.Vertices;

            var vCount = vertices.Count();

            if (vCount == 0)
            {
                return(new AlgorithmResult(false, false));
            }
            else if (vCount == 1)
            {
                var v = vertices.First();
                v.X           = host.MaxElementX / 2;
                v.Y           = host.MaxElementY / 2;
                v.HasPosition = true;
                return(new AlgorithmResult(true, false));
            }

            var minWidth  = host.MinElementX;
            var minHeight = host.MinElementY;

            width  -= minWidth * 2.25;
            height -= minHeight * 2.25;

            var centerX = width / 2;
            var centerY = height / 2;

            int i = 0;

            foreach (var v in vertices)
            {
                v.X           = Math.Ceiling(centerX + minWidth + centerX * Math.Cos(Math.PI * i / vCount * 2));
                v.Y           = Math.Ceiling(centerY + minHeight + centerY * Math.Sin(Math.PI * i / vCount * 2));
                v.HasPosition = true;
                i++;
            }
            return(new AlgorithmResult(true, false));
        }
        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());
        }
        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());
        }