|
Example 2: Given the following graph , show the shortest path from v1 to all other vertices. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1) First step we take the starting vertex and read all its links weights to other vertices into the weight array.
The shortest path after visiting v1:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
¥
¥
¥
(2) Then we find the smallest weight such that its vertex is not visited and add that vertex to the visited vertices.
In our example , the smallest weight is 1 for the unvisited vertex v4.
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
¥
¥
¥
2
v4
v1,v4
(3) Then for all vertices that are not visited , we check if the weight of the unvisited vertex is greater than the sum of the weight of the current visited vertex and the its other connected unvisited vertices weights. If so , we update the unvisited vertices with the new weight.
for (all vertices unvisited)
if( weight unvisited > weight for current visited vertex + weight from the visited vertex to the unvisited )
weight unvisited = weight for current visited vertex + weight from the visited vertex to the unvisited
OR
for (all vertices not visited)
if (weight [unvisited] > weight [vertex] + adjacency[vertex][unvisited] )
weight [unvisited] = weight[vertex] + adjacency[vertex][unvisited]In our example , the weight is 1 of the current visited vertex v4 .
We add this value (1) to all vertex 4 connected vertices that are not visited (all vertices except 1and 4) :
vertices
v1
v2
v3
v4
v5
v6
v7
adjacency [v4] [unvisited]
¥
¥
2
¥
2
8
4
vertices
v1
v2
v3
v4
v5
v6
v7
weight unvisited
¥
2
¥
1
¥
¥
¥
weight[v4] + adjacency[v4][unvisited]
1+ ¥ = ¥
1 + 2=3
1+ 2 =3
1+8 =9
1+4 =5
Comparison
2 < ¥
¥ > 3
¥ >3
¥ > 9
¥ > 5
The New weight from v1 to
¥
2
3
1
3
9
5
The shortest path after visiting v4:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
¥
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
(4) We repeat step 2 and 3 until all nodes are visited.
In our example , the smallest weight is 2 for the unvisited vertex v2.
We add this value (2) to all vertex 2 connected vertices that are not visited (all vertices except 1 ,4 and 2) :
vertices
v1
v2
v3
v4
v5
v6
v7
adjacency [v2] [unvisited]
¥
¥
¥
3
10
¥
¥
vertices
v1
v2
v3
v4
v5
v6
v7
weight unvisited
¥
2
3
1
3
9
5
weight[v2] + adjacency[v2][unvisited]
2 + ¥
2 + 10
2 + ¥
2 + ¥
Comparison
¥ >3 3 > 12 ¥ >9 ¥ >5 The New weight from v1 to
¥
2
3
1
3
9
5
The shortest path after visiting v2 (no change ):
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
3
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
3
v2
v1,v4,v2
¥
2
3
1
3
9
5
The next smallest weight is 3 for the unvisited vertex 3 .
We add this value (3) to all vertex 3 connected vertices that are not visited (all vertices except 1,4,2 and 3) :
v1
v2
v3
v4
v5
v6
v7
adjacency [v3] [unvisited]
4
¥
¥
¥
¥
5
¥
vertices
v1
v2
v3
v4
v5
v6
v7
weight unvisited
¥
2
3
1
3
9
5
weight[v3] + adjacency[v3][unvisited]
3 + ¥
3 + 5
3 + ¥
Comparison
¥ > 3 9 > 8 ¥ > 5 The New weight from v1 to
¥
2
3
1
3
8
5
The shortest path after visiting v3:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
3
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
3
v2
v1,v4,v2
¥
2
3
1
3
9
5
4
v3
v1,v4,v2,v3
¥
2
3
1
3
8
5
The next smallest weight is 3 for the unvisited vertex v5.
We add this value (3) to all vertex 5 connected vertices that are not visited (v6 and v7) :
v1
v2
v3
v4
v5
v6
v7
adjacency [v5] [unvisited]
¥
¥
¥
1
¥
¥
6
vertices
v1
v2
v3
v4
v5
v6
v7
weight unvisited
¥
2
3
1
3
8
5
weight[v5] + adjacency[v5][unvisited]
3 + ¥
3 + 6
Comparison
¥ > 8 5 >9
The New weights
¥
2
3
1
3
8
5
The shortest path after visiting v5:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
3
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
3
v2
v1,v4,v2
¥
2
3
1
3
9
5
4
v3
v1,v4,v2,v3
¥
2
3
1
3
8
5
5
v5
v1,v4,v2,v3,v5
¥
2
3
1
3
8
5
The next smallest weight is 5 for the unvisited vertex v7.
We add this value (5) to all vertex 5 connected vertices that are not visited (only v6 ):
v1
v2
v3
v4
v5
v6
v7
adjacency [v7] [unvisited]
¥
¥
¥
¥
¥
1
¥
vertices
v1
v2
v3
v4
v5
v6
v7
weight unvisited
¥
2
3
1
3
8
5
weight[v7] + adjacency[v7][unvisited]
5+1
Comparison
8 > 6
The New weights
¥
2
3
1
3
6
5
The shortest path after visiting v7:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
3
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
3
v2
v1,v4,v2
¥
2
3
1
3
9
5
4
v3
v1,v4,v2,v3
¥
2
3
1
3
8
5
5
v5
v1,v4,v2,v3,v5
¥
2
3
1
3
8
5
6
v7
v1,v4,v2,v3,v5,v7
¥
2
3
1
3
6
5
The algorithm terminates after we visit v6:
count
vertex
visited
weight
v1
v2
v3
v4
v5
v6
v7
1
v1
v1
¥
2
¥
1
3
¥
¥
2
v4
v1,v4
¥
2
3
1
3
9
5
3
v2
v1,v4,v2
¥
2
3
1
3
9
5
4
v3
v1,v4,v2,v3
¥
2
3
1
3
8
5
5
v5
v1,v4,v2,v3,v5
¥
2
3
1
3
8
5
6
v7
v1,v4,v2,v3,v5,v7
¥
2
3
1
3
6
5
7
v6
v1,v4,v2,v3,v5,v7,v6
¥
2
3
1
3
6
5