I'm trying to code a program for a class that simulates a router and so far I have the basics set up ("router" can send and receive packets through an emulated server to other "routers" connected to the server). Each packet contains only the distance vector for that router. When a router receives a packet it is supposed to update it's own distance vector accordingly using the Bellman-Ford algorithm. The problem I'm having is that I am finding myself unable to implement the actual algorithm without cheating and using an adjacency matrix.
For example, say I have 3 routers connected as follows:
A ---1--- B ---2--- C
That is, A and B are connected with a link cost of 1, and B and C are connected with a link cost of 2. So when the routers are all started, they will send a packet to each of their directly connected neighbors containing their distance vector info. So A would send router B (0, 1, INF), B would send A and C (1, 0, 2) and C would send B (INF, 2, 0) where INF means the 2 routers are not directly connected.
So let's look at router A receiving a packet from router B. To calculate the minimum costs to each other router using the Bellman-Ford algorithm is as follows.
Mincost(a,b) = min((cost(a,b) + distance(b,b)),(cost(a,c) + distance(c,b))
Mincost(a,c) = min((cost(a,b) + distance(b,c)),(cost(a,c) + distance(c,c))
So the problem I am running into is that I cannot for the life of me figure out how to implement an algorithm that will calculate the minimum path for a router to every other router. It's easy enough to make one if you know exactly how many routers there are going to be but how would you do it when the number of routers can be arbitrarily big?