예제 #1
0
        public override object Execute(Graph graph)
        {
            ValidateGraph(graph);

            var cleaner = new NameCleaner();

            cleaner.Execute(graph);

            graph.UnmarkAllWays();

            var visited = new HashSet <GraphNode>();

            recourseWalker(graph, null, graph.MarkedNodes[0], visited);

            return(graph);
        }
예제 #2
0
        public override object Execute(Graph graph)
        {
            ValidateGraph(graph);

            var cleaner = new NameCleaner();

            cleaner.Execute(graph);

            graph.UnmarkAllWays();

            var initNode  = graph.MarkedNodes[0];
            var queue     = new Queue <GraphNode>();
            var visited   = new HashSet <GraphNode>();
            var distances = new Dictionary <GraphNode, int>();

            distances[initNode] = 0;

            recourseWalker(graph, initNode, queue, visited, distances);

            return(graph);
        }
예제 #3
0
        public override object Execute(Graph graph)
        {
            ValidateGraph(graph);

            GraphNode from = graph.MarkedNodes[0];
            GraphNode to   = graph.MarkedNodes[1];

            #region Заполяем таблицу соответствий
            var dictionary = new Dictionary <GraphNode, NodeInfo>();   // таблица соответсвтий вершина-информация

            foreach (GraphNode node in graph.Nodes)
            {
                dictionary[node] = new NodeInfo();
            }

            dictionary[from].Weight = 0.0;
            #endregion

            #region Основной цикл алгоритма
            KeyValuePair <GraphNode, NodeInfo> active;

            while (true)
            {
                active = findPairWithMinUnvisitedNode(dictionary);

                if (active.Key == null)
                {
                    break;
                }

                active.Value.Visited = true;

                List <GraphNode> incidentUnvisitedNodes = getUnvisitedIncidentNodes(graph, active.Key, dictionary);

                foreach (GraphNode node in incidentUnvisitedNodes)
                {
                    GraphWay way = graph.FindWay(active.Key, node);

                    if (dictionary[node].Weight > active.Value.Weight + way.Weight)
                    {
                        dictionary[node].Weight   = active.Value.Weight + way.Weight;
                        dictionary[node].Previous = active.Key;
                    }
                }
            }
            #endregion

            if (dictionary[to].Weight == double.MaxValue)
            {
                throw new AlgorithmException("Конечная вершина недостижима из начальной");
            }

            #region Изменяем граф в соответствии с результатами

            var cleaner = new NameCleaner();
            cleaner.Execute(graph);

            var answer = createAnswer(to, dictionary);

            graph.UnmarkAllWays();

            for (int i = 0; i < answer.Count; i++)
            {
                answer[i].Name = (i + 1).ToString(@"\##");

                if (i != 0)
                {
                    graph.FindWay(answer[i - 1], answer[i]).Style = WayStyle.Marked;
                }
            }

            foreach (var pair in dictionary)
            {
                if (pair.Value.Weight != double.MaxValue)
                {
                    pair.Key.Info = pair.Value.Weight.ToString("0.0");
                }
                else
                {
                    pair.Key.Info = "N/A";
                }
            }

            #endregion

            return(graph);
        }