Published
Edited
Oct 5, 2019
1 star
Also listed in…
Algorithms
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
A = (replay, +special || (Math.random() * nodes.length) | 0)
Insert cell
origins = {
const tree0 = shortest_tree({ graph, origins: [A] }).next().value;
const B = d3.scan(tree0.cost.map(d => (isFinite(d) ? -d : 0)));
const tree1 = shortest_tree({ graph, origins: [B] }).next().value;
const C = d3.scan(tree1.cost.map(d => (isFinite(d) ? -d : 0)));
const tree2 = shortest_tree({ graph, origins: [C] }).next().value;
const D = d3.scan(tree2.cost.map(d => (isFinite(d) ? -d : 0)));

return [C, D].sort();
}
Insert cell
Insert cell
Insert cell
src = "https://gist.githubusercontent.com/Fil/450db51a15fdc8bd34a12cb8a9961976/raw/680961da57d2b11a0e3e6993e5f79bd035729831"
Insert cell
nodes = d3.csv(`${src}/node_list.csv`, d3.autoType)
Insert cell
edges = d3.csv(`${src}/edge_list.csv`, d3.autoType)
Insert cell
Insert cell
nodesIndex = new Map(nodes.map((d, i) => [d.osmid, i]))
Insert cell
function createGraph(nodesIndex, edges) {
const graph = { sources: [], targets: [], costs:[] };
edges.forEach(({ u, v, length }) => {
const i = nodesIndex.get(u),
j = nodesIndex.get(v),
cost = length;
graph.sources.push(i);
graph.targets.push(j);
graph.costs.push(cost);

graph.sources.push(j);
graph.targets.push(i);
graph.costs.push(cost);
});

return graph;
}
Insert cell
graph = createGraph(nodesIndex, edges)
Insert cell
import { shortest_tree } from "@fil/dijkstra"
Insert cell
tree = shortest_tree({ graph, origins })
Insert cell
Insert cell
pts = nodes.map(d => projection([d.x, d.y]))
Insert cell
projection = d3.geoMercator().fitExtent([[10, 10], [width - 10, height - 10]], {
type: "MultiPoint",
coordinates: nodes.map(d => [d.x, d.y])
})
Insert cell
Insert cell
color = (special, replay, d3.scaleOrdinal(d3.schemeCategory10))
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
height = width * 0.9
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