Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

SR

INSTITUTE OF
M SCIENCE AND
TECHNOLOGY,
CHENNAI.
18CSC201J
DATA STRUCTURES AND ALGORITHMS
Unit- V
SR
INSTITUTE OF SCIENCE
M AND
TECHNOLOGY,
CHENNAI.

Dijkstra's algorithm
Shortest Path Problem
Single-Source Shortest Path Problem - The problem of finding
shortest paths from a source vertex v to all other vertices in the
graph.
Applications
- Maps (Map Quest, Google Maps)
- Routing Systems
Dijkstra's algorithm

Dijkstra's algorithm - is a solution to the single-source


shortest path problem in graph theory.

Works on both directed and undirected graphs. However, all


edges must have nonnegative weights.

Input: Weighted graph G={E,V} and source vertex v∈V, such


that all edge weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths


themselves) from a given source vertex v∈V to all other
vertices
Approach
1. The algorithm computes for each vertex u the distance to u
from the start vertex v, that is, the weight of a shortest path
between v and u.
2. The algorithm keeps track of the set of vertices for which
the distance has been computed, called the cloud C
3. Every vertex has a label D associated with it. For any vertex
u, D[u] stores an approximation of the distance between v
and u. The algorithm will update a D[u] value when it finds
a shorter path from v to u.
4. When a vertex u is added to the cloud, its label D[u] is equal
to the actual (final) distance between the starting vertex v
and vertex u.
Dijkstra pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.
while List is not empty:
v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.
if dist is smaller than n's distance:
n's distance := dist.
n's previous := v.
reconstruct path from v2 back to v1,
following previous pointers.
Example: Initialization
Let us consider A as the source node. Distance of all vertices except source is ∞
Distance(source) = 0

0 ∞
2
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 ∞ 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance.


Update neighbour’s distance
Select the source node A and check the neighbour nodes it reaches. Update the
weights of B and D as 2 and 1.

0 2
2
A B
1
4 3 10

2 2 ∞
∞ C D E

5 8 1 4 6

Distance(B) = 2 1
F G
Distance(D) = 1
∞ ∞
Now, select the node with minimum distance. Weights are 2 and 1 hence 1 is
selected i.e., D is chosen

0 2
2
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 1 4 6

1
F G

∞ ∞
Neighbors of D are selected and their distances are updated.
Distance of C = Distance of D + weight of D to C = 1+ 2=3
Distance of E = Distance of D + weight of D to E = 1+ 2=3
Distance of F = Distance of D + weight of D to F = 1+ 8=9
Distance of G = Distance of D + weight of D to G = 1+ 4=5

0 2
2
A B

4 1 3 10

2
2 3
3 C D E

5 8 1 4 6

1
F G

9 5
Pick vertex in List with minimum distance and update neighbors.
B has the minimum distance. Neighbours of B are D and E.
Distance of D is not updated since D is already visited.
Previous distance of E = 3
New distance of E = 2+10 = 12
Distance of E not updated since it is larger than previously computed.
0 2
2
A B

4 1 3 10

2 2 3
3 C D E

5 8 1 4 6

1
F G

9 5
Pick vertex List with minimum distance and update neighbors. E is selected.
Distance of G not updated since it is larger than previously computed.

0 2
2
A B

4 1 3 10

2 2 3
3 C D E

5 8 1 4 6

1
F G

9 5
Pick vertex List with minimum distance and update neighbors.
C is selected. Neighbour is F.
Distance of F = Distance of C + weight of C to F = 3 + 5 = 8

0 2
2
A B

4 1 3 10

2 2 3
3 C D E

5 8 1 4 6

1
F G

8 5
Pick vertex List with minimum distance and update neighbors
G is selected. F is the neighbour.
Previous distance of F is 8.
Distance of F = Distance of G+weight from G to F = 5+1
Update the distance of F as 6.
0 2
2
A B

4 1 3 10

2 2 3
3 C D E

5 8 1 4 6

1
F G

6 5
F is selected and there are no neighbors to F. This is resultant the shortest path graph

0 2
2
A B

4 1 3 10

2 2 3
3 C D E

5 8 1 4 6

1
F G

6 5
References:
1. A.V.Aho, J.E Hopcroft , J.D.Ullman, Data structures and Algorithms, Pearson Education, 2003
2. Reema Thareja, Data Structures Using C, 1st ed., Oxford Higher Education, 2011
Thank
You

You might also like