public void Start(Node origin, Node end) { Node currentNode = origin; NodeConnection cnc = new NodeConnection(); cnc.CameFrom = null; cnc.CostSoFar = 0; cnc.Heuristic = new Connection(origin, end.Location).Weight; cnc.Node = origin; cnc.TotalEstimatedCost = cnc.CostSoFar + cnc.Heuristic; while (cnc.Heuristic != 0) { foreach (var item in cnc.Node.Connections) { NodeConnection nc = new NodeConnection(); nc.CameFrom = cnc; nc.CostSoFar = cnc.CostSoFar + item.Weight; nc.Node = item.Node; nc.Heuristic = new Connection(nc.Node, end.Location).Weight; nc.TotalEstimatedCost = nc.CostSoFar + nc.Heuristic; OpenList.Add(nc); } ClosedList.Add(cnc); NodeConnection previous = cnc; OpenList.Remove(cnc); NodeConnection smallest = null; foreach (var item in OpenList) { if(smallest == null) { smallest = item; } else if(smallest.TotalEstimatedCost > item.TotalEstimatedCost) { smallest = item; } } cnc = new NodeConnection(); cnc = smallest; } StringBuilder sb = new StringBuilder(); while(cnc != null) { sb.Append(cnc.Node.Name + " "); cnc = cnc.CameFrom; } string answer = sb.ToString(); sb = new StringBuilder(); for (int i = answer.Length - 2; i >= 0; i--) { sb.Append(answer[i]); } Console.WriteLine("\n\n\n\tShortest Path is " + sb.ToString()); }
public Connection(Node node, Location originNodeLocation) { this.Node = node; this.Weight = this.GetDistance(originNodeLocation); }