Public
Edited
Aug 30, 2023
5 forks
Importers
31 stars
Also listed in…
Algorithms
Hello
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
current = setup.update(permutations[sorting]())
Insert cell
// note: here the graph expect source and target to be 0-based and correspond to existing nodes.
graph = FileAttachment("miserables.json").json()
Insert cell
permutations = orders(graph, {distance})
Insert cell
orders = ({ nodes, links }, { distance = "manhattan" }) => {
const n = nodes.length;

const graph = reorder.graph().nodes(nodes).links(links).init();

let dist_adjacency;

const leafOrder = reorder
.optimal_leaf_order()
.distance(reorder.distance[distance]);

function computeLeaforder() {
const adjacency = reorder.graph2mat(graph);
return leafOrder(adjacency);
}

function computeLeaforderDist() {
if (!dist_adjacency) dist_adjacency = reorder.graph2valuemats(graph);
return reorder.valuemats_reorder(dist_adjacency, leafOrder);
}

function computeBarycenter() {
const barycenter = reorder.barycenter_order(graph),
improved = reorder.adjacent_exchange(
graph,
barycenter[0],
barycenter[1]
)[0];
const b = new Map(improved.map((lo, i) => [i, lo]));
return d3.range(improved.length).map((i) => b.get(i));
}

function computeRCM() {
return reorder.reverse_cuthill_mckee_order(graph);
}

function computeSpectral() {
return reorder.spectral_order(graph);
}

const orders = {
none: () => d3.range(n),
name: () =>
d3.range(n).sort((a, b) => d3.ascending(nodes[a].name, nodes[b].name)),
count: () => d3.range(n).sort((a, b) => nodes[b].count - nodes[a].count),
group: () =>
d3
.range(n)
.sort(
(a, b) =>
d3.ascending(nodes[a].group, nodes[b].group) ||
d3.ascending(nodes[a].name, nodes[b].name)
),
leafOrder: computeLeaforder,
leafOrderDist: computeLeaforderDist,
barycenter: computeBarycenter,
rcm: computeRCM,
spectral: computeSpectral
};

return orders;
}
Insert cell
orderingsOptions = [
"none",
"name",
"count",
"group",
"leafOrder",
"leafOrderDist",
"barycenter",
"rcm",
"spectral"
]
Insert cell
distanceOptions = ({
euclidean: "Euclidean (L2)",
manhattan: "Manhattan (L1)",
minkowski: "Minkowski",
chebyshev: "Chebyshev",
hamming: "Hamming",
jaccard: "Jaccard",
braycurtis: "Braycurtis"
})
Insert cell
reorder = require("reorder.js@2.2.6")
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more