gr_nodes = [
{id: "A", x: 0.12, y: 0.72},
{id: "B", x: 0.35, y: 0.90},
{id: "C", x: 0.38, y: 0.58},
{id: "D", x: 0.62, y: 0.80},
{id: "E", x: 0.65, y: 0.40},
{id: "F", x: 0.88, y: 0.62},
{id: "G", x: 0.30, y: 0.20},
{id: "H", x: 0.88, y: 0.18}
]
gr_edges = [["A", "B"], ["A", "C"], ["B", "C"], ["B", "D"], ["C", "D"], ["C", "E"],
["D", "F"], ["E", "F"], ["E", "G"], ["E", "H"], ["F", "H"]]
gr_ids = gr_nodes.map(n => n.id)
// adjacency sets of the toy graph (undirected: each edge is stored both ways)
gr_toy_adj = {
const adj = gr_nodes.map(() => new Set());
gr_edges.forEach(([a, b]) => {
adj[gr_ids.indexOf(a)].add(gr_ids.indexOf(b));
adj[gr_ids.indexOf(b)].add(gr_ids.indexOf(a));
});
return adj;
}
// the toy graph, with the selected node in red and its neighbours in blue
gr_toy_plot = (sel, size = 380) => {
const i = gr_ids.indexOf(sel);
const cells = gr_nodes.map((n, j) => ({
...n,
fill: j === i ? "tomato" : gr_toy_adj[i].has(j) ? "steelblue" : "lightgray",
ink: j === i || gr_toy_adj[i].has(j) ? "white" : "black"
}));
const edges = gr_edges.map(([a, b]) => {
const p = gr_nodes[gr_ids.indexOf(a)], q = gr_nodes[gr_ids.indexOf(b)];
return {x1: p.x, y1: p.y, x2: q.x, y2: q.y, hot: a === sel || b === sel};
});
return Plot.plot({
width: size, height: size, margin: 12,
x: {domain: [0, 1], axis: null},
y: {domain: [0, 1], axis: null},
marks: [
Plot.link(edges, {
x1: "x1", y1: "y1", x2: "x2", y2: "y2",
stroke: d => d.hot ? "tomato" : "#bbb",
strokeWidth: d => d.hot ? 3 : 1.5
}),
Plot.dot(cells, {x: "x", y: "y", fill: "fill", r: 14, stroke: "white", strokeWidth: 1.5}),
Plot.text(cells, {x: "x", y: "y", text: "id", fill: "ink", fontWeight: "bold"}),
Plot.frame()
]
});
}
// the same graph as an adjacency matrix: the selected node's row is highlighted
gr_matrix_plot = (sel, size = 270) => {
const i = gr_ids.indexOf(sel);
const cells = [];
gr_nodes.forEach((a, r) => gr_nodes.forEach((b, c) => cells.push({
row: a.id, col: b.id,
v: gr_toy_adj[r].has(c) ? 1 : 0,
fill: gr_toy_adj[r].has(c) ? (r === i ? "tomato" : "steelblue") : (r === i ? "#fde0dd" : "#f2f2f2")
})));
return Plot.plot({
width: size, height: size, marginTop: 24, marginLeft: 24, marginRight: 4, marginBottom: 4,
x: {domain: gr_ids, axis: "top", label: null},
y: {domain: gr_ids, label: null},
marks: [
Plot.cell(cells, {x: "col", y: "row", fill: "fill", inset: 1}),
Plot.text(cells, {x: "col", y: "row", text: d => d.v, fill: d => d.v ? "white" : "#aaa", fontSize: 12})
]
});
}
// graph and adjacency matrix side by side
// (a function, so every slide cell gets a fresh element; a named cell holding an element cannot be displayed again)
gr_toy_view = sel => html`<div style="display:flex; align-items:center; justify-content:center; gap:10px">${gr_toy_plot(sel, 290)}${gr_matrix_plot(sel, 270)}</div>`
// caption: neighbour set and degree of the selected node
gr_toy_text = sel => {
const i = gr_ids.indexOf(sel);
const nb = [...gr_toy_adj[i]].map(j => gr_ids[j]).sort();
return html`<p style="text-align:center; margin-top:0">N(${sel}) = {${nb.join(", ")}} → deg(${sel}) = <b>${nb.length}</b></p>`;
}
// ---------- slide 2: a spatial neighbours graph ----------
// toy tissue (70 cells, fixed seed): a dense blob in the upper left plus a sparse background
gr_points = {
const rnd = cen_rng(21);
const gauss = () => Math.sqrt(-2 * Math.log(1 - rnd())) * Math.cos(2 * Math.PI * rnd());
const pts = [];
const ok = (p, gap) => p.x > 0.03 && p.x < 0.97 && p.y > 0.03 && p.y < 0.97 &&
pts.every(q => Math.hypot(p.x - q.x, p.y - q.y) >= gap);
const add = (n, propose, gap) => {
let placed = 0, tries = 0;
while (placed < n && tries++ < 20000) {
const p = propose();
if (ok(p, gap)) { pts.push(p); placed++; }
}
};
add(35, () => ({x: 0.30 + 0.08 * gauss(), y: 0.66 + 0.08 * gauss()}), 0.025);
add(35, () => ({x: rnd(), y: rnd()}), 0.06);
return pts;
}
// radius graph: i ~ j if their Euclidean distance is at most r
gr_radius_graph = (pts, r) => {
const adj = pts.map(() => new Set());
pts.forEach((p, i) => pts.forEach((q, j) => {
if (i < j && Math.hypot(p.x - q.x, p.y - q.y) <= r) { adj[i].add(j); adj[j].add(i); }
}));
return adj;
}
// the tissue and its graph; cells without any neighbour are drawn in red
gr_plot = (pts, adj, size) => {
const edges = [];
adj.forEach((nb, i) => nb.forEach(j => {
if (i < j) edges.push({x1: pts[i].x, y1: pts[i].y, x2: pts[j].x, y2: pts[j].y});
}));
const cells = pts.map((p, i) => ({x: p.x, y: p.y, fill: adj[i].size === 0 ? "tomato" : "steelblue"}));
return Plot.plot({
width: size, height: size, margin: 0,
x: {domain: [0, 1], axis: null},
y: {domain: [0, 1], axis: null},
marks: [
Plot.link(edges, {x1: "x1", y1: "y1", x2: "x2", y2: "y2", stroke: "#bbb", strokeOpacity: 0.7, strokeWidth: 1}),
Plot.dot(cells, {x: "x", y: "y", fill: "fill", r: 5.5, stroke: "white", strokeWidth: 1}),
Plot.frame()
]
});
}
// the two graphs on the same cells: kNN with k = 4, and radius with r = 0.12 (unit-square coordinates)
gr_knn_adj = cen_graph(gr_points, 4)
gr_rad_adj = gr_radius_graph(gr_points, 0.12)