I am trying to implement Distance Vector Routing algorithm, using http://www.cs.bu.edu/fac/byers/courses/791/F99/scribe_notes/cs791-notes-990923.html
(in C++).
Here is what I have done so
far:
i) Read no. of nodes.
ii) Implement the points 1 to 4 in the article as:
for(i = 0; i < nodes; i++) //nodes is the no. of nodes
{
for(j = 0; j < nodes; j++)
{
if(distanceVector[i][j] != 0) //distanceVector holds the cost value between every pair of links, 0 if no link exists
{
for(k = 0; k < nodes; k++)
{
if((distanceVector[i][j] + distanceVector[j][k]) < distanceVector[i][k])
{
distanceVector[i][k] = distanceVector[i][j] + distanceVector[j][k];
via[i][j] = i; // intermediate node, in case no link exists
via[j][i] = j;
}
}
}
}
}
I get the same array / matrix as it is. I have also tried juggling i, j, and k, but of no use.
Am I right in my implementation…???
Two things bother me about your code. First, you are using “0” to represent “no link”. This can get you in trouble. The code basically read as follows: “if there is an intermediary point j, that makes that path from i to k shorter, change the path from i to k to pass via k”. Therefore, using “0” to represent “no link” might make your code choose wrong “via”s. Try instead to use infinity (if you are using floating point) or a really big value (e.g., MAX_INT).
Second, these lines look wrong:
Since you found a path from i to k via j that is shorter, it should be: