I started to work on adjacent node swapping to reduce edge crossings in the graph. I implemented the first part, which is positioning (on the x-axis) each node at the median of the positions of nodes pointing at it from previous layers.
That's when I noticed things still looked messed up and that some nodes were pointing to nodes that were in the same vertical layer. I reread the book to make sure that's not supposed to happen.
Then, I wrote a test to confirm that it was happening:
for (
let i = 0;
i <= layeredNodesResult[layeredNodesResult.length - 1].layer;
++i
) {
let layerNodes = layeredNodesResult.filter((e) => e.layer === i);
// console.log('Layer', i, layerNodes);
t.ok(
layerNodes.every((node) =>
layerNodes.every(
(otherNode) =>
!otherNode.nodesPointingAtThisNode.includes(node.endeavor.id)
)
),
`Layer ${i} does not have nodes pointing at each other.`
);
}
And yes, it is happening. So, I have to step back and see what I did wrong in the layering part of the algorithm instead moving forward with edge crossing reduction.