You are on page 1of 21

Discrete Mathematical Structures

Shortest Path Problem


 Many problems in the real life are related to
the shortest path problem. Such as
 Pipe lines
 Electric power grid
 Airline system p708
 Communicating network p709

22/12/11 College of Computer Science & Technology, BUPT 2


Problem
 There are two types of such problems
 Determining the shortest path from a vertex to
an assigned vertex.
 Determining the shortest path of any two
vertices in the graph.

3
Dijkstra Algorithm
 The algorithm is to find the shortest way
from v1 to vn,at the same time,it gets the
shortest way from v1 to each other vertices
in the graph.

4
Example1
 What is the length of a shortest path between
a and z in the weighted graph shown in
Figure 3?

5
6
7
Example
 Let li represent the length from v1 to vi
 Let dij represent the length of the edge (vi,vj)
 Find the shortest path from v1 to v11
v2 1 v5 2 v8
9
2 5 3 9 7
6
8 1 6 2 v11
v1 v3 v6 v9
1 7 4 1
2 3 4
v4 9 v7 1 v10
8
v2 1 v5 2 v8
9
2 5 3 9 7
6
6 2
Step 1 v1 8
7
v3
1
4
v6
1
v9
v11
1 3 4
2
v4 9 v7 1 v10
 Find the set of adjacent vertices of v1,which are
{v2, v3, v4}
 Find the length from v1 to the vertices in the set.
 l2=d12=2 l3=d13=8 l4=d14=1
 Find the shortest length of l2,l3 ,l4
 Min{l2,l3 ,l4}= l4=1
 Connect v1 to v4

9
v2 1 v5 2 v8
9
2 5 3 9 7
6
8 1 6 2 v11
Step 2 v1
1 7
v3
4
v6
1
v9
2 3 4
v4 9 v7 1 v10

 Find the set of adjacent vertices of {v1, v4}


which are {v2, v3, v7}
 Find the length from v1 to the vertices in the set.
 l2=2 l3=8 l7= l4 + d47=1+9=10
 Find the shortest length of l2,l3 ,l7
 Min{l2,l3 ,l7}= l2=2
 Connect v1 to v2
10
v2 1 v5 2 v8
9
2 5 3 9 7
6
1 6 2
Step 3 v1 8
7
v3
4
v6
1
v9
v11
1 3 4
2
v4 9 v7 1 v10

 Find the set of adjacent vertices of {v1, v2, v4} which


are {v3, v5, v7}
 Find the length from v1 to the vertices in the set.
 l3= min{8,l2+d23,l4 +d43}={8,8,8}=8
 l5= l2 + d25=3
 l7= l4 + d47=10
 min{l3,l5 ,l7}= l5=3
 Connect v2 to v5 11
v2 1 v5 2 v8
9
2 5 3 9 7
6
1 6 2
Step 4 v1 8
7
v3
4
v6
1
v9
v11
1 3 4
2
v4 9 v7 1 v10

 Find the set of adjacent vertices of {v1, v2, v4 ,


v5} which are {v3, v6, v7 , v8, v9}
 Find the length from v1 to the vertices in the set.
 l3= 8 l6= l5 + d56=6 l7=10
 l8= l5 + d58=5 l9= l5 + d59=12
 Min{l3,l6,l7 ,l8,l9}= l8=5
 Connect v5 to v8
12
v2 1 v5 2 v8
9
2 5 3 9 7
6
1 6 2
Step 5 v1 8
7
v3
4
v6
1
v9
v11
1 3 4
2
v4 9 v7 1 v10

 Find the set of adjacent vertices of {v1, v2, v4 ,


v5, v8} which are {v3 , v6, v7, v9 , v11}
 Find the length from v1 to the vertices in the set.
 l3=8 l6= l5 +d56=6 l7=10
 l9= l8 + d89=12 l11= l8 + d8,11= 14
 Min{l3 ,l6,l7 ,l9 ,l11}= l6=6
 Connect v5 to v6
13
v2 1 v5 2 v8
9
2 5 3 9 7
6
1 6 2
Step 6 v1 8
7
v3
4
v6
1
v9
v11
1 3 4
2
v4 9 v7 1 v10

 Find the set of adjacent vertices of {v1, v2, v4 , v5, v6,


v8} which are {v3 , v7, v9 , v11}
 Find the length from v1 to the vertices in the set.
 l3= min{8, l6 +d36}={8,7}=7
 l7= min{l4 + d47, l6 +d67} =min{10,10}=10
 l9= min{l8 + d89, l6 +d69 , l5 +d59} =min{10,10}=10
 l11=14
 Min{l3,l7 ,l9 ,l11}= l3=7
 Connect v6 to v3 14
Step 7
 Continue to
 connect v3 to v7
 connect v7 to v10
 connect v10 to v9
 connect v9 to v11
 Then the path from v1 to v11 is the answer.
 At the same time ,we also get the shortest
path from v1 to other vertices in the graph.

15
Finding the shortest path between
any two vertices
 Distance Matrix: Let G is a graph with n vertices.
The distance matrix of G is D=(dij)nn
 dij represent the the weights of the edge (vi,vj)
 If there’s no edge between vi and vj then dij=
 Let D2=D*D= (dij2)nn
 dij2=min{di1+d1j, di2+d2j,…, din+dnj,}
 dij2 is the shortest length of the path from vi to vj with two edges.
 As the same Dk=Dk-1*D= (dijk)nn
 dijk is the shortest length of the path from vi to vj with k edges.

16
Define 
 Let A= (aij)nn , B= (bij)nn
 C=A  B = (cij)nn
 cij=min(aij, bij)
 P=D  D2  D3    Dn
 pij = (pij)nn
 pij represent the shortest length from vi to vj

17
1

Example
3 3
2
 1 2 
  3 3 
D 2
   2
 
   
 1 2    1 2     4 4
  3 3     3 3     5 
D2   * 
   2     2     
     
              

18
  4
4   1 2      6
   5    3 3      
D3   * 
        2     
     
             

 1 2 4 
 D4=() 44   3 3 
P 
 P=D  D2  D3 D4    2 
 
     
19
The traveling salesman problem

20
homework
 §10.6
 8, 16, 18, 26

21

You might also like