Public
Edited
Jun 14, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tree1 = createGraph(n, [
[0, 1],
[1, 2],
[1, 4],
[4, 3],
[3, 5]
]);
Insert cell
tree2 = createGraph(n, [
[5, 4],
[4, 3],
[4, 2],
[2, 1],
[1, 0]
])
Insert cell
Insert cell
function serializeTree(root) {
if (root === null) {
return "";
}

const labels = [];
for (const child of root.children) {
labels.push(serializeTree(child));
}
labels.sort((a, b) => b.length - a.length);

let result = "";
for (const label of labels) {
result += label;
}
return "(" + result + ")";
}

Insert cell
function isIsomorphic(n, g1, g2) {
const root1 = getCentersOfTree(n, g1);
const tree1 = createRootedTree(g1, root1[0]);
const serializedTree1 = serializeTree(tree1);

const root2 = getCentersOfTree(n, g2);
for (const root of root2) {
const tree = createRootedTree(g2, root);
const serializedTree2 = serializeTree(tree);

if (serializedTree1 === serializedTree2) {
return true;
}
}

return false;
}
Insert cell
{
const isomorphic = isIsomorphic(n, tree1, tree2);
return "Given trees are " + (isomorphic ? "" : " not ") + "isomorphic";
}
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