// PRIVATE ACCESS private void recourseWalker(Graph graph, GraphNode current, Queue <GraphNode> queue, HashSet <GraphNode> visited, Dictionary <GraphNode, int> distnaces) { if (visited.Contains(current)) { return; } visited.Add(current); current.Name = visited.Count.ToString(@"\#0"); current.Info = distnaces[current].ToString(@"0"); foreach (var node in GraphHelper.GetIncidentNodes(graph, current, true)) { if (!visited.Contains(node) && !queue.Contains(node)) { distnaces[node] = distnaces[current] + 1; graph.FindWay(current, node, true).Style = WayStyle.Marked; queue.Enqueue(node); } } while (queue.Count != 0) { recourseWalker(graph, queue.Dequeue(), queue, visited, distnaces); } }
// PRIVATE ACCESS void recourseWalker(Graph graph, GraphNode previous, GraphNode current, HashSet <GraphNode> visited) { current.Name = (visited.Count + 1).ToString(@"\##"); visited.Add(current); if (previous != null) { graph.FindWay(previous, current, true).Style = WayStyle.Marked; } //recourseWalker( graph, current, foreach (var node in GraphHelper.GetIncidentNodes(graph, current)) { if (!visited.Contains(node)) { recourseWalker(graph, current, node, visited); } } }
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); }