Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 10x 10x 10x 10x 203x 97x 121x 121x 75x 71x 10x 10x 10x 69x 67x 10x 11x 21x 10x 10x 11x 11x 26x 26x 11x 5x 11x 9x 9x 9x 9x 9x 55x 55x 55x 9x 20x 9x 20x 20x 66x 110x 66x 66x 46x 20x 66x 20x 66x 66x 66x 9x 13x 13x 13x | import { WebGraphEdge, WebGraphNode } from "@/objects/Knowledge";
/**
* Where each page of a website source's link graph sits: a radial tree.
*
* The seed URLs sit at the centre and every link hop is one ring further out,
* so the distance from the middle *is* the crawl depth. Within a ring a page
* is placed at the mean angle of the pages that link to it from the ring
* inside, which keeps a listing's children fanned out beside it rather than
* scattered round the circle. A ring grows past its minimum radius when it
* holds more pages than fit, so labels do not collide however wide a front
* page's fan-out is.
*
* Pages no stored link reaches (from a sitemap, or whose linker has gone)
* form one outer ring of their own.
*
* Rings are ellipses twice as wide as they are tall. The canvas is wide, and
* a label is wide and short: side by side at the top of a ring neighbours
* need a label's width between them, stacked at its sides only a node's
* height. A circle sized for the first wastes half the canvas on the second,
* and fitting it zooms the labels out of legibility.
*/
/** Horizontal stretch of every ring. */
export const ASPECT = 2;
/** Minimum distance between neighbours on a ring, vertically — one node's
* height; `ASPECT` times it horizontally, one label's width. */
export const NODE_SPACING = 72;
/** Minimum distance between rings, vertically (`ASPECT` times it across). */
export const RING_GAP = 100;
export interface Point {
x: number;
y: number;
}
const TAU = Math.PI * 2;
const wrap = (a: number) => ((a % TAU) + TAU) % TAU;
/** The mean of some angles, on the circle (so 350° and 10° average to 0°). */
function circularMean(angles: number[]): number | null {
if (angles.length === 0) return null;
const x = angles.reduce((s, a) => s + Math.cos(a), 0);
const y = angles.reduce((s, a) => s + Math.sin(a), 0);
if (Math.abs(x) < 1e-9 && Math.abs(y) < 1e-9) return angles[0];
return wrap(Math.atan2(y, x));
}
/** Rings in drawing order: depth 0, 1, … then the unreached pages. */
export function ringsOf(nodes: WebGraphNode[]): WebGraphNode[][] {
const byDepth = new Map<number, WebGraphNode[]>();
const unreached: WebGraphNode[] = [];
for (const n of nodes) {
if (n.depth === null || n.depth === undefined) unreached.push(n);
else byDepth.set(n.depth, [...(byDepth.get(n.depth) ?? []), n]);
}
const rings = [...byDepth.keys()]
.sort((a, b) => a - b)
.map((d) => byDepth.get(d) as WebGraphNode[]);
if (unreached.length) rings.push(unreached);
return rings;
}
/**
* Each ring's radius, from how many pages it holds. Outward, a ring is at
* least `RING_GAP` past the last and big enough for its pages. Then inward:
* a crowded ring (a front page's forty stories) must not leave the rings
* inside it bunched at the centre, or every crawl path becomes one long
* spoke — so ring `i` is at least `i / j` of any ring `j` outside it, keeping
* the rings evenly spread.
*/
export function ringRadii(sizes: number[]): number[] {
const radii: number[] = [];
sizes.forEach((count, index) => {
const needed = (count * NODE_SPACING) / TAU;
// A lone seed sits dead centre; several seeds share a small ring.
radii.push(
index === 0
? count === 1
? 0
: Math.max(needed, NODE_SPACING / 2)
: Math.max(radii[index - 1] + RING_GAP, needed),
);
});
for (let i = radii.length - 2; i >= 1; i--) {
radii[i] = Math.max(radii[i], (radii[i + 1] * i) / (i + 1));
}
return radii;
}
export function layoutLinkGraph(
nodes: WebGraphNode[],
edges: WebGraphEdge[],
): Record<string, Point> {
const positions: Record<string, Point> = {};
const angle: Record<string, number> = {};
const incoming = new Map<string, string[]>();
const neighbours = new Map<string, string[]>();
for (const e of edges) {
incoming.set(e.target, [...(incoming.get(e.target) ?? []), e.source]);
neighbours.set(e.target, [...(neighbours.get(e.target) ?? []), e.source]);
neighbours.set(e.source, [...(neighbours.get(e.source) ?? []), e.target]);
}
const rings = ringsOf(nodes);
const radii = ringRadii(rings.map((r) => r.length));
rings.forEach((ring, index) => {
const radius = radii[index];
// Where each page would like to be: beside whatever links to it.
const wanted = ring.map((n, i) => {
const placed = (incoming.get(n.id) ?? []).filter((s) => s in angle);
const fallback = (neighbours.get(n.id) ?? []).filter((s) => s in angle);
const mean =
circularMean(placed.map((s) => angle[s])) ??
circularMean(fallback.map((s) => angle[s]));
return { node: n, want: mean ?? (TAU * i) / ring.length, order: i };
});
wanted.sort((a, b) => a.want - b.want || a.order - b.order);
// Evenly spaced slots, turned so the pages land as near as they can to
// where they wanted to be.
const step = TAU / ring.length;
const offset =
circularMean(wanted.map((w, i) => wrap(w.want - i * step))) ?? 0;
wanted.forEach((w, i) => {
const a = wrap(offset + i * step);
angle[w.node.id] = a;
positions[w.node.id] = {
x: Math.round(ASPECT * radius * Math.cos(a)),
y: Math.round(radius * Math.sin(a)),
};
});
});
return positions;
}
/** A crawl path: the link that first reached a page, one ring further out. */
export function isTreeEdge(
edge: WebGraphEdge,
depthOf: Record<string, number | null>,
): boolean {
const from = depthOf[edge.source];
const to = depthOf[edge.target];
return from !== null && from !== undefined && to === from + 1;
}
|