diff --git a/lib/order/index.js b/lib/order/index.js index 9d3f3622..ee8f345e 100644 --- a/lib/order/index.js +++ b/lib/order/index.js @@ -1,13 +1,13 @@ "use strict"; var _ = require("../lodash"), - initOrder = require("./init-order"), - crossCount = require("./cross-count"), - sortSubgraph = require("./sort-subgraph"), - buildLayerGraph = require("./build-layer-graph"), - addSubgraphConstraints = require("./add-subgraph-constraints"), - Graph = require("../graphlib").Graph, - util = require("../util"); + initOrder = require("./init-order"), + crossCount = require("./cross-count"), + sortSubgraph = require("./sort-subgraph"), + buildLayerGraph = require("./build-layer-graph"), + addSubgraphConstraints = require("./add-subgraph-constraints"), + Graph = require("../graphlib").Graph, + util = require("../util"); module.exports = order; @@ -27,43 +27,43 @@ module.exports = order; * algorithm. */ function order(g) { - var maxRank = util.maxRank(g), + var bestCC = Number.POSITIVE_INFINITY, + best; + for (var j = 0; j < 4; j++) { + var maxRank = util.maxRank(g), downLayerGraphs = buildLayerGraphs(g, _.range(1, maxRank + 1), "inEdges"), upLayerGraphs = buildLayerGraphs(g, _.range(maxRank - 1, -1, -1), "outEdges"); - var layering = initOrder(g); - assignOrder(g, layering); - - var bestCC = Number.POSITIVE_INFINITY, - best; + var layering = initOrder(g); + assignOrder(g, layering); - for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) { - sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2); + for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) { + sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2); - layering = util.buildLayerMatrix(g); - var cc = crossCount(g, layering); - if (cc < bestCC) { - lastBest = 0; - best = _.cloneDeep(layering); - bestCC = cc; + layering = util.buildLayerMatrix(g); + var cc = crossCount(g, layering); + if (cc < bestCC) { + lastBest = 0; + best = _.cloneDeep(layering); + bestCC = cc; + } } } - assignOrder(g, best); } function buildLayerGraphs(g, ranks, relationship) { - return _.map(ranks, function(rank) { + return _.map(ranks, function (rank) { return buildLayerGraph(g, rank, relationship); }); } function sweepLayerGraphs(layerGraphs, biasRight) { var cg = new Graph(); - _.forEach(layerGraphs, function(lg) { + _.forEach(layerGraphs, function (lg) { var root = lg.graph().root; var sorted = sortSubgraph(lg, root, cg, biasRight); - _.forEach(sorted.vs, function(v, i) { + _.forEach(sorted.vs, function (v, i) { lg.node(v).order = i; }); addSubgraphConstraints(lg, cg, sorted.vs); @@ -71,9 +71,23 @@ function sweepLayerGraphs(layerGraphs, biasRight) { } function assignOrder(g, layering) { - _.forEach(layering, function(layer) { - _.forEach(layer, function(v, i) { - g.node(v).order = i; + var thisNode; + _.each(layering, function (layer, j) { + + layering[j] = _.sortBy(layer, function (v) { + + thisNode = g.node(v); + + if (thisNode.edgeLabel) { + return thisNode.edgeLabel.sortrank; + } else { + return thisNode.sortrank; + } + }); + + _.each(layering[j], function (v2, i) { + g.node(v2).order = i; }); }); } + diff --git a/lib/version.js b/lib/version.js index dec155cb..c640f4e6 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1 +1 @@ -module.exports = "0.8.3-pre"; +module.exports = "0.8.4-pre";