Dijkstra algoritm implementation in Swift

Hello every one,

Here is an implementation for the Dijkstra algorithm in Swift that helps you get the shortest path from node_A to node_B of grid of connected nodes.
The algorithm is as below:

Foreach node set distance[node] = HIGH
SettledNodes = empty
UnSettledNodes = empty

Add sourceNode to UnSettledNodes
distance[sourceNode]= 0

while (UnSettledNodes is not empty) {
  evaluationNode = getNodeWithLowestDistance(UnSettledNodes)
  remove evaluationNode from UnSettledNodes 
    add evaluationNode to SettledNodes

  find the node with the lowest distance in UnSettledNodes and return it 

  Foreach destinationNode which can be reached via an edge from evaluationNode AND which is not in SettledNodes {
    edgeDistance = getDistance(edge(evaluationNode, destinationNode))
    newDistance = distance[evaluationNode] + edgeDistance
    if (distance[destinationNode]  > newDistance) {
      distance[destinationNode]  = newDistance 
      add destinationNode to UnSettledNodes
You can get a code example from this link:

ليست هناك تعليقات:

إرسال تعليق

---- أتشرف بتعليقاتكم ----