You are on page 1of 8

Explain in detail the Dijkstras algorithm to solve the shortest pathproblem.

Dijkstras Algorithm:
Let the starting vertex is called the initial vertex. Let the distance of vertexv be the distance
from the initial vertex to v. Dijkstra's algorithm will assign some initial distance values and will
try to improve them step by step.
1. Assign to every vertex a tentative distance value: set it to zero for our initial node and to
infinity for all other nodes.
2. Mark all vertices unvisited. Set the initial vertex as current. Create a set of the unvisited
vertices called the unvisited set consisting of all the vertices except the initial vertex.
3. For the current vertex, consider all of its unvisited neighbors and calculate their tentative
distances. For example, if the current vertexv1 is marked with a distance of 4, and the edge
connecting it with a neighbor v2 has length 1, then the distance to v2 (through v1) will be 4+1=5.
If this distance is less than the previously recorded tentative distance of v2, then overwrite that
distance. Even though a neighbor has been examined, it is not marked as "visited" at this time,
and it remains in the unvisited set.
4. Mark the current vertex as visited and remove it from the unvisited set. A visited vertex will
never be checked again.
5. If the destination vertex has been marked visited (when planning a route between two specific
nodes) or if the smallest tentative distance among the vertices in the unvisited set is infinity
(when planning a complete traversal), then stop. The algorithm has finished.
6. Select the unvisited vertex that is marked with the smallest tentative distance, and set it as the
new "current node" then go back to step 3.
Example:

1
4

2
3

10

2
5

7
6

4
1

1
4

1
3

7
6

1
3

7
6

1
3

2
5

7
4

1
3

7
4

1
3

2
4

1
2

1
1

2
4

6
1

Step 1:
It represents the initial configuration, assuming that the source node S is V1.
Vertex

Known

dv

dw

Step 2

Vertex

Known

dv

dw

Vertex

Known

dv

dw

5
Vertex
6
1
7
2

0
Known
0
0
0
0

dv

0
dw
0
0
0
0

Step 3

Step 5

Step 4

Vertex

Known

dv

dw

Vertex

Known

dv

dw

Vertex

Known

dv

dw

Step 6

Step 7

Step 8
Vertex

Known

dv

dw

You might also like