shortenPaths = (links, activeNodeIds) => {
const g = new Graph(links);
let changed = true;
while (changed) {
changed = false;
let nodes = Object.values(g.nodeMap);
for (let node of nodes) {
if (node.in.length === 1 && node.out.length === 1) {
const nodeIn = g.nodeMap[node.in[0]];
const nodeOut = g.nodeMap[node.out[0]];
if (!activeNodeIds.includes(node.id)) {
g.removeNode(node);
g.addLink(nodeIn, nodeOut);
delete g.nodeMap[node.id];
changed = true;
}
}
}
}
return g.toLinks();
}