Skip to content

Commit

Permalink
Calculate same-layer crossings
Browse files Browse the repository at this point in the history
  • Loading branch information
Anya committed Jun 28, 2019
1 parent 52b2b4f commit 56e7429
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/order/cross-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56e7429

Please sign in to comment.