public void AlotOfElements() { var queue = new DijkstraPriorityQueue(); queue.Insert(new DijkstraNode("a"), 2); queue.Insert(new DijkstraNode("b"), 2); queue.Insert(new DijkstraNode("c"), 1); queue.Insert(new DijkstraNode("d"), 1); queue.Insert(new DijkstraNode("e"), 1); queue.Insert(new DijkstraNode("f"), 1); queue.Insert(new DijkstraNode("g"), 3); queue.Insert(new DijkstraNode("h"), 2); queue.Insert(new DijkstraNode("i"), 3); queue.Insert(new DijkstraNode("j"), 1); queue.Insert(new DijkstraNode("k"), 2); queue.Insert(new DijkstraNode("l"), 3); queue.Insert(new DijkstraNode("m"), 2); queue.Insert(new DijkstraNode("n"), 3); queue.Insert(new DijkstraNode("o"), 1); queue.Insert(new DijkstraNode("p"), 2); queue.Insert(new DijkstraNode("q"), 0); queue.Insert(new DijkstraNode("r"), 1); var nodes = new List <string>(); while (queue.Size != 0) { var(node, priority) = queue.ExtractMin(); nodes.Add(node.Name); } var actual = string.Join(" ", nodes); Assert.AreEqual("q c d e f j o r a b h k m p g i l n", actual); }
/// <summary> /// Dijkstra initialization /// </summary> /// <param name="graph"> source graph </param> /// <param name="source"> source node </param> /// <param name="distances"> empty priority queue </param> private void Initialize (Graph graph, DijkstraNode source, DijkstraPriorityQueue distances) { source.Distance = 0; var vertices = graph.AdjDict.GetVertices(); foreach (var v in vertices) { distances.Insert(v, v.Distance); } }
/// <summary> /// find shortest distance to vertice v /// between known distance (v.Distance) /// and from u across edge (u, v) /// </summary> /// <param name="u"> source node </param> /// <param name="v"> adjacent node </param> /// <param name="weight"> weith of edge between u and v </param> /// <param name="queue"> pririty queue with Dijkstra nodes </param> private void Relax(DijkstraNode u, DijkstraNode v, int weight, DijkstraPriorityQueue queue) { var distanceThroughU = u.Distance + weight; if (distanceThroughU < v.Distance) { v.Distance = distanceThroughU; v.Prev = u; queue.ChangePriority(v, distanceThroughU); } }
/// <summary> /// Call Dijkstra algorithm /// </summary> /// <param name="graph"> source graph </param> /// <param name="source"> the node /// from which we look for the shortest paths </param> /// <returns> graph with filled "distances" and "prevs" of every node </returns> public Graph Execute(Graph graph, DijkstraNode source) { var priorityQueue = new DijkstraPriorityQueue(); Initialize(graph, source, priorityQueue); while (priorityQueue.Size != 0) { var start = priorityQueue.ExtractMin(); var adjNodes = graph.AdjDict[start.node]; foreach (var(node, weight) in adjNodes) { Relax(start.node, node, weight, priorityQueue); } } return(graph); }
public void EqualPriorities() { var queue = new DijkstraPriorityQueue(); queue.Insert(new DijkstraNode("a"), 1); queue.Insert(new DijkstraNode("b"), 1); queue.Insert(new DijkstraNode("c"), 1); queue.Insert(new DijkstraNode("d"), 1); var nodes = new List <string>(); while (queue.Size != 0) { var(node, priority) = queue.ExtractMin(); nodes.Add(node.Name); } var actual = string.Join(" ", nodes); Assert.AreEqual("a b c d", actual); }
// This puts the start node into the queue protected internal virtual void InitQueue() { Queue = new DijkstraPriorityQueueFibonacciImpl <CostType>(outerInstance.CostComparator); Queue.insertValue(StartNode, outerInstance.StartCost); MySeen[StartNode] = outerInstance.StartCost; }