I think the logic to handle this should be something like the following pseudocode:
Code:
new array X; //creates a new array with most existing data copied but distances all being set to -1.
bool change_made = false;
while(!change_made)
{
change_made = false;
for each s in systems{
{
if capital() {s.distance = 0; change_made = true;}
else for each h in hyperlanes(s)
{
if(target_system(h).distance == -1) continue; //skip for now until that system gets populated
if((s.distance == -1) || (target_system(h).distance + 1 < s.distance))
{
s.best_path = target_system(h);
s.distance = target_system(h).distance + 1;
change_made = true;
}
}
}
First loop it will go through all systems and mark capitals with 0. Subsequent loops can hit more than once system at once.
But if it encounters a scenario where a system ends up being closer, it will lower its value and switch the path to the more optimal path.
for example. let us say it has those paths
sol -> lolala -> eptrabon -> bessia -> oshimir -> Churassix
sol -> chiminol -> bessia -> oshimir -> Churassix
loop 1: sol marked as distance 0
loop 2: lolala and chiminol marked as distance 1. through coincidence eptrabon was checked after chiminol and marked as distance 2. then bessia was checked and marked as distance 3 through eptrabon.
loop 3: bessia path through chiminol is noted to be distance 2 and bessia is corrected to the new quicker path. etc
basically it will go through it and each time lower possible loops as much as possible until it figures the optimal path