
Solution to Exercises From 24 Mar 2004
1Using Djikstra’s algorithm, find the shortest path from node 1 to all other nodes [see slide 09.103
for network diagram]
We start with node 1, looking at the cost to reach each node directly reachable from 1. From all such paths
we select the lowest cost path, adding it to set R for the next iteration, and we repeat this until all nodes
are in the set R. The last path appearing in each column, i.e., before the node to which it is a path is added
to R, is the shortest path from our start node to that particular node. The table stops showing costs +path
entries once an entry is found that does not change (but I duplicate the entry for C6to show it as a ‘tie’ in
cost (6) for iteration 4.
Iteration R Path Path Path Path PathC2C3C4C5C6
1{1} 1 1→2? – 71→4? – ? –
2{1,2} 4 1→2→37 1→4? – ? –
3{1,2,3} 7 1→451→2→3→561→2→3→6
4{1,2,3,5} 6 1→2→3→5→461→2→3→6
5{1,2,3,4,5}
6{1,2,3,4,5,6}
2Repeat using the Bellman-Ford algorithm.
[Shay describes this algorithm on pages 459 - 466 (2nd ed.), 496 - 502 (3rd ed.). Tanenbaum (4th edition)
provides a description on pages 357-360].
With Bellman-Ford we explore, iteratively,ever longer length paths until our table of costs doesn’t change.
We start with a hop count, h,of0and ask what nodes we can reach in 0 hops; this iteration is easy.Next
we set hto 1 and ask what nodes we can reach in 1 hop and what their costs are: we select, for each node
we can reach, the lowest cost path we discover.Ateach iteration we ennumerate all the paths having a
length hop-count hfrom our origin, and update our list of costs if we discover a path to any of the nodes
that is less expensive than any path we already have.
Forinstance, all paths with 1 hop are:
Path Cost
1→21
1→47
Each of these paths is less expensive than the costs we now have, so both go in the table.
Paths with 2 hops are:
Path Cost
1→2→34
1→4→310
1→4→58
Of the two paths to node 3, that via node 2 is least expensive, so that one enters the table. Paths involving
more hops but with lower cumulative cost may replace earlier discovered paths.
Continuing as above, we end up with the table shown below.
Hop Count Path Path Path Path PathC2C3C4C5C6
0∞ε∞ ε ∞ ε∞ε∞ ε
111→2∞ε71→4∞ε∞ε
241→2→381→4→5∞ε
351→2→3→561→2→3→6
461→2→3→5→4
CS455 1 Exercises from 24 Mar 2004