public void Run() { var nodeA = new DjikstraNode { Key = "A", }; var nodeB = new DjikstraNode { Key = "B" }; var nodeC = new DjikstraNode { Key = "C" }; var nodeD = new DjikstraNode { Key = "D" }; var nodeE = new DjikstraNode { Key = "E" }; var nodeF = new DjikstraNode { Key = "F" }; var nodeG = new DjikstraNode { Key = "G" }; var nodeH = new DjikstraNode { Key = "H" }; var tree = new List <DjikstraNode> { nodeA.AddConnection(nodeB, 8).AddConnection(nodeD, 5).AddConnection(nodeC, 2), nodeB.AddConnection(nodeD, 2).AddConnection(nodeF, 13), nodeC.AddConnection(nodeD, 2).AddConnection(nodeE, 5), nodeD.AddConnection(nodeE, 1).AddConnection(nodeF, 6).AddConnection(nodeG, 3), nodeE.AddConnection(nodeG, 1), nodeF.AddConnection(nodeG, 2).AddConnection(nodeH, 3), nodeG.AddConnection(nodeH, 6), nodeH }; FindShortestPath(tree, start: nodeA, end: nodeH); }
public DjikstraNode AddConnection(DjikstraNode node, int cost, bool twoWay = true) { Edges.Add(new Edge { From = this, To = node, Cost = cost }); if (twoWay) { node.AddConnection(this, cost, false); } return(this); }
public void FindShortestPath(List <DjikstraNode> tree, DjikstraNode start, DjikstraNode end) { start.Distance.Value = 0; DjikstraNode?currentNode = null; while (currentNode != end) { var minDistance = double.PositiveInfinity; for (int i = 0; i < tree.Count; i++) { if (!tree[i].Visited && minDistance > tree[i].Distance.Value) { minDistance = tree[i].Distance.Value; currentNode = tree[i]; } } if (currentNode != null) { currentNode.Visited = true; foreach (var edge in currentNode.Edges) { var newDistance = currentNode.Distance.Value + edge.Cost; if (!edge.To.Visited && edge.To.Distance.Value > newDistance) { edge.To.Distance = new Distance { Value = newDistance, ComingFrom = edge.From }; } } } } // print result var node = end; while (node.Distance.ComingFrom != null) { Console.WriteLine($"{node.Key}-> Distance {node.Distance.Value}->Comming from {node.Distance.ComingFrom.Key}"); node = node.Distance.ComingFrom; } }