Dijkstra's algorithm
From Algorithmist
(Redirected from Dijkstra's Algorithm)
| This is a stub or unfinished. Contribute by editing me. |
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.
[edit] Algorithm
The algorithm is as follows, starting from source S:
- Set distance to S to 0.
- While there is a node to be relaxed
- Find the node V that is the closest to S that is not relaxed.
- 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.
- for(all the vertexes except the first vertex)
- UM[j] = A[first vertex][j]
- UM[first vertex] = 0
- CLEAR(S)
- while(our target vertex doesn't exist in S)
- vertex = vertex with minimum distance that doesn't exist in S
- add vertex to S
- for(all the vertexes that don't exist in S)
- if(UM[vertex] + A[vertex][current vertex] < UM[current vertex])
- UM[current vertex] = UM[vertex] + A[vertex][current vertex];
- if(UM[vertex] + A[vertex][current vertex] < UM[current vertex])

