diff --git a/lib/order/cross-count.js b/lib/order/cross-count.js index 2f8b6b0f..ebee0e37 100644 --- a/lib/order/cross-count.js +++ b/lib/order/cross-count.js @@ -22,12 +22,37 @@ module.exports = crossCount; */ function crossCount(g, layering) { var cc = 0; - for (var i = 1; i < layering.length; ++i) { - cc += twoLayerCrossCount(g, layering[i-1], layering[i]); + for (var i = 0; i < layering.length; ++i) { + cc += singleLayerCrossCount(g, layering[i]); + if (i > 0) { + cc += twoLayerCrossCount(g, layering[i-1], layering[i]); + } } return cc; } +function singleLayerCrossCount(g, layer) { + var layerRank = g.node(layer[0]).rank; + + var layerIndex = _.zipObject( + layer, + _.map(layer, function (v, i) { return i; }) + ); + + var cc = 0; + _.forEach(layer, function(n, i) { + _.forEach(g.inEdges(n), function(e) { + if (g.node(e.v).rank == layerRank) { + var otherPos = layerIndex[e.v]; + if (Math.abs(otherPos - i) > 1) { + cc++; + } + } + }); + }); + return cc; +} + function twoLayerCrossCount(g, northLayer, southLayer) { // Sort all of the edges between the north and south layers by their position // in the north layer and then the south. Map these edges to the position of