From 49dce72b0840a5bbf2840f9ca20255325e9d8c8b Mon Sep 17 00:00:00 2001 From: Anya Date: Fri, 28 Jun 2019 12:11:01 +0200 Subject: [PATCH] Calculate same-layer crossings --- lib/order/cross-count.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/order/cross-count.js b/lib/order/cross-count.js index 2f8b6b0f..18a78882 100644 --- a/lib/order/cross-count.js +++ b/lib/order/cross-count.js @@ -22,8 +22,34 @@ 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 cc = 0; + if (layer.length) { + var layerRank = g.node(layer[0]).rank; + var layerIndex = _.zipObject( + layer, + _.map(layer, function (v, i) { return i; }) + ); + + _.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; }