power_welsh = function (topo, objects) {
const com = topojson.feature(topo, objects).features;
com.forEach((d) => (d.properties.color = null));
const neighbors = topojson.neighbors(objects.geometries);
const neighbors_indices_sorted = function (neighbors) {
const neighbors_indices = neighbors.map((d, i) => ({ fid: i, v: d }));
return d3.sort(neighbors_indices, (d) => d.v.length);
};
function welsh(id_col) {
let n = 0;
for (const d of neighbors_indices_sorted(neighbors)) {
if (com[d.fid].properties.color === null) {
if (d.v.every((j) => com[j].properties.color !== id_col)) {
com[d.fid].properties.color = id_col;
} else n++;
}
}
return n;
}
let id_col = 0;
while (welsh(id_col)) {
++id_col;
}
return com;
}