Dynamic programs
2
The nodes in this graphare labeled 1, 2, .
.., 6. There are 4 arcs,namely (1
,
2), (2
,
3), (3
,
4), (4
,
1); 5 edges,
namely (1
,
6), (2
,
6), (1
,
3), (4
,
5), (3
,
5). The sequence of nodes 1
−
2
−
3
−
4, 1
−
2
−
3
−
5
−
4, 4
−
1
−
3
−
5
are examples of paths in this graph. On the other hand the sequences 1
−
4
−
5, 1
−
6
−
1
−
3
−
5are
not
paths in this graph. Examples of cycles include 1
−
2
−
3
−
4
−
1and1
−
6
−
3
−
5
−
1.
In many applications, edges and arcs have
lengths
associated with them. Let
c
ik
denote the length of the
arc (
i, k
). If (
i, k
) is an edge (i.e. bidirectional) then
c
ik
=
c
ki
,otherw
ise
c
ki
=
∞
.A
l
s
ol
e
t
c
ii
=
∞
for all
i
∈
N
. The length of a path is the
sum
of the lengths of edges/arcs in the path.
(a) length of path 1234:
c
12
+
c
23
+
c
34
=15+12+5=32
(b) length of path 54132:
c
54
+
c
41
+
c
13
+
c
32
=20+(
−
30) + 20 + 12 = 22
The goal of the
shortest path problems
in a graph
G
is to Fnd the minimum length paths. These problems
come in many varieties
(a)
s

t
shortest path problem: ±ind the shortest path between the speciFed nodes
s
and
t
.
(b) Singlesource shortest path problem: ±ind shortest paths from a speciFed node
s
to all other nodes in
the graph
(c) Allpairs shortest path problem: ±ind the shortest paths between all pairs of nodes.
The shortest path problem is solved by
recursion
, i.e. by reducing it to a number of stages. To do this we
will need some notation.
1. Singlesource shortest path problem
v
(
t
)
[
k
]
=
length of shortest path from
s
to
k
with
t
or fewer arcs/edges
v
[
k
]
=
length of shortest path from
s
to
k
d
[
k
]
=
node preceding
k
in the best known path from
s
to
k
If there are
n
nodes in a graph then any path can have at most
n
−
1 edges/arcs (why? because n edges
or more would mean repeating nodes). Therefore,
v
[
k
]=
v
(
n
−
1)
[
k
]. Also
v
(0)
[
s
]=0and
v
(0)
[