2. What is a Graph ?
A graph contains nodes and edges and a
directed graph 'G' represents each edge
'e' with a direction.
Shortest-Path Algorithm:
Let G be a directed graph with m nodes n1,
n2, n3 ... nm , Suppose G is weighted, that is
suppose each edge e in G is assigned a
nonnegative number w(e) called the
weight or length of the edge e.
Definitions
3. Shortest Path Directed Graph
5
7
S
R
1
T
U4
3
2
7
This represents an edge
This represents a node
And this represents the weight of the
edge i.e, the distance between the two
nodes
4. How we will procedd ?
• To find the shortest path between any two
nodes we will draw two tables.
Distance Table (D) and Sequence Table (S)
• We can also refer these tables as matrix.
• The distance table will hold the distance
between any two nodes.
• The sequence table will hold the name of
the nodes that will help in finding the
shortest path between two nodes.
5. How we will procedd ?
• We will use iterative approach to solve the
problem. If there are N nodes in a Graph
we will iterate N-1 times.
• For a graph having N nodes, our distance
table(D) and sequence table(S) will have
N rows and N columns.
7. Create Distance and Sequence
Table
7 5 ∞ ∞
7 ∞ ∞ 2
∞ 3 ∞ ∞
4 ∞ 1 ∞
D0 R S T U
R
S
T
U
RR RS _ _
SR _ _ SU
_ TS _ _
UR _ UT _
S0 R S T U
R
S
T
U
Distance Table Sequence Table
This is a Cell CTU
Iteration = k = 0
8. Conditions
• We will fill the cells Cij in distance table Dk
using the following conditions:
• Is dij > dik + dkj { in distance table D k-1 }
• If YES then fill the cell Cij in Dk table with
the value dik+djk of Dk-1 table.
• If NO then fill the cell Cij in Dk table with
the value dij of Dk-1 table.
• That is we will always fill the Cij in Dk with
the smallest value.
9. 7 5 ∞ ∞
7 12 ∞ 2
∞ 3 ∞ ∞
4 9 1 ∞
D1 R S T U
R
S
T
U
S1 R S T U
R
S
T
U
Distance Table Sequence Table
RR RS _ _
SR SR+RS _ SU
_ TS _ _
UR UR+RS UT _
* We have to remove the infinity sign and find the minimum
value.
Iteration = k = 1 , Row = i = 1 , Column = j =1
10. RR RS _ RS+SU
SR SR+RS _ SU
SR+TS TS _ TS+SU
UR UR+RS UT _
Create Distance and Sequence
Table
7 5 ∞ 7
7 12 ∞ 2
10 3 ∞ 5
4 9 1 ∞
D2 R S T U
R
S
T
U
S2 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 2 , Row = i = 2 , Column = j = 2
11. Create Distance and Sequence Table
7 5 ∞ 7
7 12 ∞ 2
10 3 ∞ 5
4 4 1 6
D3 R S T U
R
S
T
U
S3 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 3 , Row = i = 3 , Column = j = 3
RR RS _ RS+SU
SR SR+RS _ SU
SR+TS TS _ TS+SU
UR TS+UT UT UT+TU
12. Create Distance and Sequence Table
7 5 8 7
7 11 3 2
9 3 6 5
4 9 1 6
D4 R S T U
R
S
T
U
S4 R S T U
R
S
T
U
Distance Table Sequence Table
Iteration = k = 4 , Row = i = 4 , Column = j = 4
RR RS UT+RU
RS+S
U
SR US+SU UT+SU SU
TU+UR TS UT+TU TS+SU
UR TS+UT UT
UT+T
U
13. Time to find the distance and the
shortest path
The shortest distance
between R and S = dRS
in D4= 5
The shortest distance
between R and T = dRT
in D4= 8
7 5 8 7
7 11 3 2
9 3 6 5
4 9 1 6
D4 R S T U
R
S
T
U
RS
RT
15. Shortest Path Algorithm A weighted graph G with M
nodes is maintained in mermory by its weight matrix W.
This algorithm finds a matrix Q such that Q[I, J] is the
length of shortest path from node NI to NJ . INFINITY is a
very large number, and MIN is the minimum value function.
1. Repeat for I, J = 1, 2 .... M: [Initialize Q]
W[I, J] = 0, then: Set Q[I, J] := INFINITY;
Else: Set Q[I, J]:= W[I, J].
[End of Loop]
2. Repeat Steps 3 and 4 for K= 1, 2 ... M: [Update Q]
3. Repeat Step 4 for I = 1, 2 ... M:
4. Repeat for J= 1, 2 ... M:
Set Q[I, J] := MIN(Q[I, J], Q[I, K] + Q[K,J])
[End of loop]
[End of Step 3 loop]
[End of Step 2 loop]
5. Exit.