networks - Shadows Government

MA2005 – Graphs and Networks
Networks 1
NETWORKS 1
1. Shortest Path Algorithms
Aim: To introduce some terminology used in networks problems and describe
algorithms for finding the shortest path in a network problem.
A network is a graph consisting of a series of nodes (vertices) connected by a number
of edges (arcs). For example:
2
a12
a 24
a 23
1
a13
Fig 1.
4
a 34
3
In the above network, the circles represent the nodes. The quantities a ij alongside the
edges are called weights, which could represent the distances between the adjacent
nodes or the cost of, say, travelling between the nodes in a particular problem. Note
that the weights can take negative values in some networks. The starting node is
known as the source and the end node is usually referred to as sink or terminal node.
If the flow along an edge is directed in a particular direction then an arrow is placed
on that edge and we have a directed graph known as digraph.
Recall that in a graph:
A walk is a succession of edges from a source node to a terminal node. In a walk,
nodes and edges may be visited or traversed more than once.
A trail is a walk in which all the edges are different but a node may be visited more
than once.
A path is a trail in which all the nodes are different. Another words, no edges and
nodes are traversed or visited more than once.
A cycle is path in which the source and terminal nodes are the same. Another words, a
closed path.
In the network below for example, adeb is a cycle but dghfec is not because node 4 is
visited twice.
2
5
a
c
1
b
Fig 2.
d
3
f
4
e
i
h
g
j
6
1
7
MA2005 – Graphs and Networks
Networks 1
Some examples of Networks models
-
Transportation Networks
Telecommunication Networks
Physical Networks such as electrical circuits
Activity Networks for scheduling industrial projects
Shortest Path Problems
Consider the network shown below where the numbers (weights) on the edges
represent the lengths between adjacent nodes. We wish to find the shortest path and
distance from S to T.
a
7
1
3
S
3
6
T
b
5
3
Fig 3.
c
Problems of such small scale can be solved by inspecting the total distance from S to
T along every path. But for larger problems we need a systematic step-by-step method
(algorithm), which can be applied to networks of any size.
Let us now introduce some algorithms for finding the shortest path, specifically
(i)
(ii)
between a source node and a terminal node (Dijkstra’s Algorithm);
between any pair of nodes (Floyd’s Algorithm).
These algorithms will be applied to the network in figure 3.
1.1 Dijkstra’s Algorithm
This algorithm finds the shortest path between a source node and a terminal node.
Each node is assigned a two-part label, which may be tentative or permanent.
Suppose some node u is assigned a permanent label of the form v ,  u  p . This
indicates that the shortest path from S to u is of length u  and that the node prior to
u on this path is v. The subscript p indicates that it is a permanent label.
For a tentative label, the two parts still mean the same thing, but indicate that such a
path is the best one identified so far.
2
MA2005 – Graphs and Networks
Step 1:
Networks 1
Label the source node S permanently as (- , 0) and all other nodes
tentatively as (- , ). Thus for S, the shortest path to S from S is of
length zero with no intervening nodes. Also, (- , ) shows that no other
paths are known and the symbol  is allocated by convention.
Set K = S and proceed to step 3.
Step 2:
Let s t denote the set of tentatively labelled nodes. Find node K such
that K   minx  , x  st . Declare the label of node K to be
permanent.
If k = T then STOP as the shortest path from S to T has been found.
Otherwise go to step 3.
Step 3:
Consider each node y which is directly connected to K and which bears
a tentative label. If  K   a ky    y  then a new tentative label is
assigned to y. The first part of the label is changed to K and  y  takes
the new value  K   a ky . When  K   a ky    y  , no change is
made. When all such nodes y have been considered, return to step 2.
When the algorithm has terminated, the shortest path from S to T is found by using the
first part of the labels to trace a route back to S. If the shortest path from S to every
other node is required then we continue until all the nodes have a permanent label.
NB: This algorithm depends for its validity on the fact that aij  0 . If this is not the
case, the procedure for obtaining permanent labels may produce incorrect results.
Example Find the shortest path and its corresponding distance from S to T for the
network below.
a
7
1
3
S
3
6
T
b
5
3
c
3
MA2005 – Graphs and Networks
Networks 1
1.2 Floyd’s Algorithm
It is frequently the case that we require to know the shortest distances between all
pairs of nodes in a network. For a network having N nodes we could, of course, apply
the Dijkstra algorithm N times, using each node in turn as the source node. There is
however, a specific algorithm available due to Floyd.
In this algorithm, nodes not joined together by an edge are assigned a distance .
Step 1: Form a N  N matrix of distances D[ a ij ] and call it D 1 . Set k = 0.
Step 2: Increase the value of k by 1. If k = N+1, STOP, otherwise go to step 3.
Step 3: let D ( k 1) be the matrix whose elements are given by
aij( k 1)  min[ a ( k ) ij , a ( k ) ik  a ( k ) kj ] .
Find all the elements of D ( k 1) and return to step 2.
In this way, N matrices are generated and the last of these consists of the shortest
distances between all pairs of nodes.
Note that the nodes need to be labelled 1, 2, …,N for this algorithm and that the
algorithm requires that there are no negative weights.
4
MA2005 – Graphs and Networks
Networks 1
Example Apply Floyd’s algorithm to the network in below.
a
7
1
3
S
3
6
T
b
5
3
c
Relabelling the nodes numerically (S as 1, a as 2, etc) and forming the matrix of
distances:
Set
0
7

k=0, D 1   3

5

7
0
3

1
3 5 
3  1 
0  6 ,

 0 3
6 3 0 
0 7 3 5 8 
7 0 3 12 1 


k=2, D 3   3 3 0 8 4  ,


5 12 8 0 3 
8 1 4 3 0 
0 6 3 5 7
6 0 3 11 1 


k=4, D 5   3 3 0 8 4 ,


 5 11 8 0 3 
7 1 4 3 0 
k=1,
 0 7 3 5 
 7 0 3 12 1 


D 2  3 3 0 8 6 ,


 5 12 8 0 3 
 1 6 3 0 
k=3,
0 6 3 5 7
6 0 3 11 1 


D 4   3 3 0 8 4 ,


5 11 8 0 3 
7 1 4 3 0 
k=5,
0
6

D 6  3

5
7
6 3 5 7
0 3 4 1 
3 0 7 4 .

4 7 0 3
1 4 3 0 
The matrix D6 represents the shortest distances between any two nodes and
the highlighted entries indicate indirect routes.
5
MA2005 – Graphs and Networks
Networks 1
Exercises 1.1
(i)
Apply the Dijkstra’s Algorithm to find the shortest path and distance from
node a to node h and from node a to every other node in the following
networks.
6
b
f
6
5
2
a
5
c
4
e
3
1
5
h
10
4
2
5
d
g
9
Apply Dijkstra’s algorithm to the following directed network.
(ii)
6
b
f
6
5
2
a
c
4
5
e
3
1
h
10
5
2
4
d
5
g
9
Exercise1.2
Apply Floyd’s algorithm to find the shortest distance between all pairs of nodes in the
following network.
b
10
5
e
a
2
18
4
8
11
5
5
d
2
c
6