Single - Source - Shortest - Path
Single - Source - Shortest - Path
1
Shortest Paths Algorithms
2
Single source Shortest path algorithm
3
Dijkstra’s Algorithm
4
Introduction
• Dijkstra’s algorithm is a greedy
algorithm for solving single source
shortest path problem that provide us
with the shortest path from one particular
source node to all other nodes in the
given graph .
5
6
Rules of Dijkstra’s algorithm
• It can be applied on both directed and undirected graph.
• It is applied on weighted graph.
• For the algorithm to be applicable the graph must be connected.
• Dijkstra’s algorithm does not work for graphs with negative
weight edges. For graphs with negative weight edges,
Bellman_ford algorithm can be used.
• In Dijkstra’s algorithm always assign source vertex to zero
distance.
• Assign every other node a tentative distance.
7
Weight Matrix
Suppose G is a weighted and simple directed graph with m nodes. The weight
0 otherwise
Example:
7
4
R U R S T U
5 7 2 1 W= R 7 5 0 0
S 7 0 0 2
S T
3 T 0 3 0 0
U 4 0 1 0
Initailize-Single-Source(G,s) Relax(u, v, w)
1. for each vertex v є V[G] 1. If d[v] > d[u] + w(u,v)
2. do d[v] := 2. then d[v] := d[u] + w(u,v)
∞
3. Π[v] := NIL 3. Π[v] := u
4. d[s] := 0 4. exit
5. exit
9
Example:
u v u v
1 1
∞ ∞
s 10 9 s 10 9
2 3 2 3
4 6 0 4 6
7 7
5 5
∞
x 2 y x ∞ 2 y
(a) Given weighted, directed graph (b) Initialize d[v] and S = Ø
u v u v
1 1
10 8 14
∞
s 10 9 s 10 9
2 3 2 3
0 4 6 0 4 6
7 7
5 5 5 5 7
∞
x 2 y x 2 y
(c) Consider vertex s with d[s]=0 and S={s} (d) Consider vertex x with d[x]=5 and S={s,x}
10
u v u v
1 1
8 13 8 9
s 10 9 s 10 9
2 3 2 3
0 4 6 0 4 6
7 7
5 5 7 5 5 7
x 2 y x 2 y
(e) Consider vertex y with d[y]=7 and S={s,x,y} (f) Consider vertex u with d[u]=8 and S={s,x,y,u}
u v u v
1 1
8 9 8 9
s 10 9 s 10
2 2 9
3 6 3
0 4 0 4 6
7 7
5 5 7 5 5 7
x 2 y x 2 y
(g) Consider vertex v with d[v]=9 and S={s,x,y,u,v} Figure: Result of Dijkstra’s Algorithm
11
12
13
14
15
END!!!
16