Public
Edited
Jun 25, 2023
Paused
Fork of D3 U.S. map
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// TODO graph partition calculation
// Reference: the closest problem is probably "node-attributed spatial graph partitioning" https://par.nsf.gov/servlets/purl/10203801
// Reference: the official redistricting https://commons.wikimedia.org/wiki/File:2023_Bangkok_General_Election_Results_by_Constituencies.svg from https://th.wikipedia.org/wiki/กรุงเทพมหานครในการเลือกตั้งสมาชิกสภาผู้แทนราษฎรไทยเป็นการทั่วไป_พ.ศ._2566
// NOTE partition_k(d3.range(50), electoral_district_num) crashes so we need to filter the partitions right in partition_k()
Insert cell
// TODO graph coloring
// - https://observablehq.com/@xoolive/graph-and-map-coloring
// - https://observablehq.com/@fil/4-color-clingo
// - https://observablehq.com/@mbostock/map-coloring
Insert cell
// adapted from https://stackoverflow.com/a/39199937
partition_k = (list, k) => {
let n = list.length
let groups = []
function* partition(i) {
if (i >= n) {
yield groups
} else {
if (n - i > k - groups.length) {
for (let group of groups) {
group.push(list[i])
yield* partition(i + 1)
group.pop()
}
}
if (groups.length < k) {
groups.push([list[i]])
yield* partition(i + 1)
groups.pop()
}
}
}
let result = []
for (let p of partition(0)) {
result.push(JSON.parse(JSON.stringify(p))) // deep copy
}
return result
}
Insert cell
// adapted from https://stackoverflow.com/a/30134039
function* partition(list) {
if (list.length === 1) {
yield [list]
} else {
let first = list[0]
for (let x of partition(list.slice(1))) {
for (let i = 0; i < x.length; i++) {
yield [...(x.slice(0, i)), [first, ...x[i]], ...(x.slice(i+1))]
}
yield [[first], ...x]
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more