Dijkstra's algorithm

Dijkstra's algorithm is a greedy algorithm for calculating the single source shortest path for a graph. It can also be used to calculate the shortest path spanning tree.

Algorithm
The algorithm is as follows, starting from source $$S$$:
 * 1) Set distance to $$S$$ to 0.
 * 2) While there is a node to be relaxed
 * 3) Find the node V that is the closest to S that is not relaxed.
 * 4) Relax V - Adjust all adjacent vertices

Time complexity of the following algorithm is O(M log N), where M is amount of edges and N is amount of vertexes.

UM[x] is the total distance between the first vertex and x. S shows which vertexes are selected before. A is our matrix that shows the distances. For example, A[i][j] is the distance between i and j.


 * 1) for(all the vertexes except the first vertex)
 * 2) UM[j] = MAX
 * 3) UM[first vertex] = 0
 * 4) CLEAR(S)
 * 5) while(our target vertex doesn't exist in S)
 * 6) vertex = vertex with minimum distance that doesn't exist in S
 * 7) add vertex to S
 * 8) for(all the vertexes that don't exist in S)
 * 9) if(UM[vertex] + A[vertex][current vertex] < UM[current vertex])
 * 10) UM[current vertex] = UM[vertex] + A[vertex][current vertex];