+sub yppedia_archs_fillbynearest() {
+ # Assign islands not labelled above to archipelagoes.
+ #
+ # We do this by, for each connected component (set of islands
+ # linked by purchaseable charts), searching for the nearest other
+ # connected component which has already been assigned an arch.
+ # `Nearest' means shortest distance of unpurchaseable charts, in
+ # leagues.
+ #
+ # we need only consider vertices which weren't `boring intermediate
+ # vertices' (removed during optimisation as being of order 2)
+ my @ccs_useful= map {
+ [ grep { $widists->has_vertex($_) } @$_ ]
+ } $wiarchs->connected_components();
+
+ my @assignments;
+
+ foreach my $sourceccix (0..$#ccs_useful) {
+ next if defined $wiccix2arch{$sourceccix};
+ next unless $ccs_useful[$sourceccix];
+
+ my @sourcecc= $wiarchs->connected_component_by_index($sourceccix);
+ my @islandnodes= grep { $winode2island{$_} } @sourcecc;
+ next unless @islandnodes; # don't care, then
+
+ foreach my $islandnode (@islandnodes) {
+ printf DEBUG "%-5s arch-join-need cc%-2d %s\n",
+ $islandnode, $sourceccix, $winode2island{$islandnode};
+ }
+ my $best_dist= 9999999;
+ my ($best_target, $best_targetccix, $best_source);
+ foreach my $targetccix (0..$#ccs_useful) {
+ next unless defined $wiccix2arch{$targetccix}; # not helpful
+ next unless $ccs_useful[$targetccix];
+ foreach my $target ($wiarchs->
+ connected_component_by_index($targetccix)) {
+ foreach my $source (@sourcecc) {
+ my $target_dist= $wialldists->path_length($target,$source);
+ next unless defined $target_dist;
+ next if $target_dist >= $best_dist;
+ $best_dist= $target_dist;
+ $best_source= $source;
+ $best_target= $target;
+ $best_targetccix= $targetccix;
+ }
+ }
+ }
+ die "no possible target ?!" unless defined $best_target;
+
+ my $arch= $wiccix2arch{$best_targetccix};
+ my $best_island= $winode2island{$best_target};
+ printf DEBUG "%-5s arch-join-to %-5s dist=%2d cc%-2d %-10s %s\n",
+ $best_source, $best_target, $best_dist,
+ $best_targetccix, $arch,
+ defined($best_island) ? $best_island : "-";
+
+ push @assignments, [ $sourceccix, $arch ];
+ }
+ foreach my $assign (@assignments) {
+ $wiccix2arch{$assign->[0]}= $assign->[1];
+ }
+}
+
+sub winode2arch ($) {
+ my ($node) = @_;
+ my $ccix= $wiarchs->connected_component_by_vertex($node);
+ return $wiccix2arch{$ccix};
+}
+sub wiisland2arch ($) {
+ my ($island) = @_;
+ my $node= $wiisland2node{$island};
+ die "$island ?" unless defined $node;
+ return winode2arch($node);
+}
+