public void RemoveWay(GraphWay way) { if (ways.Contains(way)) { ways.Remove(way); } }
public Graph Clone() { var nodeDictionary = new Dictionary <GraphNode, GraphNode>(); var copyOfGraph = new Graph(); foreach (GraphNode node in Nodes) { var copyOfNode = new GraphNode(node.Position, node.Name, node.Info, node.Style); nodeDictionary[node] = copyOfNode; // соответствие между старой и новой ссылкой copyOfGraph.AddNode(copyOfNode); } foreach (GraphNode markedNode in MarkedNodes) { copyOfGraph.markedNodes.Add(nodeDictionary[markedNode]); } foreach (GraphWay way in Ways) { var copyOfWay = new GraphWay(nodeDictionary[way.From], nodeDictionary[way.To], way.Weight, way.Oriented); copyOfWay.WeightIsLen = way.WeightIsLen; copyOfWay.Style = way.Style; copyOfGraph.AddWay(copyOfWay); } return(copyOfGraph); }
public void AddWay(GraphWay way) { ways.Add(way); }
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); }