Published
Edited
May 16, 2022
5 forks
4 stars
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);
return reorder.adjacent_exchange(graph, ...barycenter);
}

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) distance.",
manhattan: "Manhattan (L1) distance.",
minkowski: "Minkowski distance.",
chebyshev: "Chebyshev distance.",
hamming: "Hamming distance.",
jaccard: "Jaccard distance.",
braycurtis: "Braycurtis distance."
})
Insert cell
reorder = require("reorder.js@2.2.4")
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