Skip to content

Instantly share code, notes, and snippets.

@AlphaGit
Created April 20, 2014 21:27
Show Gist options
  • Save AlphaGit/11125710 to your computer and use it in GitHub Desktop.
Save AlphaGit/11125710 to your computer and use it in GitHub Desktop.
Sugiyama algorithm: removing fake nodes
var unlinkFakeNode = function (fakeNode) {
// fake node: remove and connect nodes that it is connecting
var previousNodes = fakeNode.previousNodes.slice(0); // copy since it's going to be modified
previousNodes.forEach(function (previousNode) {
// previous --> next1, previous --> next2, etc
fakeNode.nextNodes.forEach(function (nextNode) {
sugiyamaService.connectNodes(previousNode, nextNode);
});
// previous --X--> fake
sugiyamaService.disconnectNodes(previousNode, fakeNode);
});
var nextNodes = fakeNode.nextNodes.slice(0); // copy since it's going to be modified
nextNodes.forEach(function (nextNode) {
sugiyamaService.disconnectNodes(fakeNode, nextNode);
});
};
var removeFakeNodesColumn = function (column) {
var rowIndex = column.length;
while (rowIndex--) {
var node = column[rowIndex];
if (sugiyamaService.isFakeNode(node)) {
unlinkFakeNode(node);
column.splice(rowIndex, 1);
}
}
};
sugiyamaService.removeFakeNodes = function (grid) {
for (var columnIndex = 0; columnIndex < grid.length; columnIndex++) {
removeFakeNodesColumn(grid[columnIndex]);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment