Public
Edited
Feb 18, 2023
Insert cell
Insert cell
Insert cell
networkDescription = [
[
{ id: "head", sid: 1 },
{ sid: 2 },
{ sid: 3 },
{ sid: 2 },
{ sid: 3 },
{ sid: 2 },
{ ref: "head" }
],
[{ sid: 4 }, { sid: 1, id: "tail" }, { sid: 4 }],
[{ ref: "head" }, { ref: "tail" }]
]
Insert cell
normalNetwork = normalizeDesc(networkDescription, ({ sid }) => ({ sid }))
Insert cell
[...listLinks(normalNetwork)]
Insert cell
Insert cell
Insert cell
longestPaths = {
const allPaths = Object.keys(network).flatMap((id) => [
...findAllPaths(network, id)
]);
const maxLength = Math.max(...allPaths.map((p) => p.length));
const longestPaths = allPaths.filter((p) => p.length === maxLength);

longestPaths.sort((path1, path2) => {
for (let i = 0; i < path1.length; i++) {
const n1 = network[path1[i]];
const n2 = network[path2[i]];
const d = n1.sid - n2.sid;
if (d !== 0) {
return d;
}
}
});

return longestPaths.map((p) => {
const isLinkInPath = (id1, id2) => {
const i = p.indexOf(id1);
return (i >= 0 && p[i - 1] === id2) || p[i + 1] === id2;
};
return {
cmp: p.map((id, i) => network[id]),
idMap: Object.fromEntries(p.map((id, i) => [id, i])),
restNetwork: Object.fromEntries(
Object.entries(network)
.map(([id, node]) => [
id,
{
...node,
links: new Set(
[...node.links].filter((id2) => !isLinkInPath(id, id2))
)
}
])
.filter(([, node]) => node.links.size > 0)
)
};
});
}
Insert cell
function findCandidates(chained, rest) {
}
Insert cell
function* findAllPaths(links, path) {
const paths = [
...from(links).pipe(
flatMap((link) => {
if (!link.includes(path[0])) {
return [];
}
const n = link[0] === path[0] ? link[1] : link[0];
return findAllPaths(
links.filter((l) => l !== link),
[n, ...path]
);
})
)
];

if (paths.length > 0) {
yield* paths;
} else {
yield path;
}
}
Insert cell
function normalizeDesc(graph, getCmp) {
const idMap = {};
for (let i = 0; i < graph.length; i++) {
const chain = graph[i];
for (let j = 0; j < chain.length; j++) {
const node = chain[j];
if ("id" in node) {
idMap[node.id] = [i, j];
}
}
}
return graph.map((chain) =>
chain.map((node) =>
"ref" in node ? { ref: idMap[node.ref] } : getCmp(node)
)
);
}
Insert cell
Insert cell
Insert cell
import { ForceGraph } from "@d3/force-directed-graph"
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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