CSE 413 Homework 6 Solutions
1)
As a dynamic programming solution, the first step is to determine how the
problem depends upon a number of small problems. Consider the problem of finding the
path to v using at most i edges. This can be denoted as path(v,i). Assume that the problem
of finding a shortest path to any vertex using at most i-1 edges has already been solved.
How can this information be used to determine path(v,i) ? Consider the vertices
adjacent to v. Any path from s to v must pass through one of these vertices in order to
reach v, using one edge. In other words, the path to v must be the minimum of the costs
of the paths to each adjacent vertex plus the cost of the link between that vertex and v.
Because of the less than or equal to constraint, path(v,i) may simply be path(v,i-1) if no
such path is shorter.
So, path(v,i)
= Min(path(v,i-1), {For all w such that w is adjacent to v, Path(w,i-1)+
cost(w->v)}).
Practically, the pseudocode for this (see next page) will compute the matrix for
dynamic programming in a the reverse fashion. In other words for some i<=k, first
initialize the cost for each vertex as the same as the i-1th column. This makes sure to
consider the <= constraint in the problem. Then consider any vertex that can be reached
using i-1 edges. Compute the cost of a path from each such vertex to its neighbors, using
the path computed in the i-1th iteration. If the new path cost to any neighbor is less than
the current cost to each of those vertices, replace the current path with the new path.
It is important to initialize the cost of the path to each vertex as
∞
, except for the
source vertex, which will be 0. This allows for a check to see if a vertex has been reached
by the algorithm yet (by checking if the cost to that vertex is less than
∞
).
Note that this algorithm could be simplified by eliminating extra work. If the path
to a vertex was not altered in the last iteration, it is not necessary to check that vertex’s
neighbors again because they have already been checked and the cost to travel to any
vertex will never go up with the less than or equal constraint. However, this is not done
now to make the pseudocode more readable. It would be simple to check the count of
edges used to get the last path to do this, as this variable is already required to trace the
path back easily.
Complexity