Skip to content

Commit

Permalink
[Ordering] add ability to define order constraints within rank
Browse files Browse the repository at this point in the history
  • Loading branch information
izhan committed Jun 2, 2020
1 parent 963ab41 commit 742ff83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ var Graph = require("./graphlib").Graph;

module.exports = layout;

function layout(g, opts) {
var time = opts && opts.debugTiming ? util.time : util.notime;
function layout(g, inputOpts) {
var opts = inputOpts || {};
var time = opts.debugTiming ? util.time : util.notime;
time("layout", function() {
var layoutGraph =
time(" buildLayoutGraph", function() { return buildLayoutGraph(g); });
time(" runLayout", function() { runLayout(layoutGraph, time); });
time(" runLayout", function() { runLayout(layoutGraph, time, opts); });
time(" updateInputGraph", function() { updateInputGraph(g, layoutGraph); });
});
}

function runLayout(g, time) {
function runLayout(g, time, opts) {
time(" makeSpaceForEdgeLabels", function() { makeSpaceForEdgeLabels(g); });
time(" removeSelfEdges", function() { removeSelfEdges(g); });
time(" acyclic", function() { acyclic.run(g); });
Expand All @@ -42,7 +43,7 @@ function runLayout(g, time) {
time(" normalize.run", function() { normalize.run(g); });
time(" parentDummyChains", function() { parentDummyChains(g); });
time(" addBorderSegments", function() { addBorderSegments(g); });
time(" order", function() { order(g); });
time(" order", function() { order(g, opts); });
time(" insertSelfEdges", function() { insertSelfEdges(g); });
time(" adjustCoordinateSystem", function() { coordinateSystem.adjust(g); });
time(" position", function() { position(g); });
Expand Down
18 changes: 14 additions & 4 deletions lib/order/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ module.exports = order;
*
* 1. Graph nodes will have an "order" attribute based on the results of the
* algorithm.
*
*/
function order(g) {
function order(g, opts) {
var maxRank = util.maxRank(g),
downLayerGraphs = buildLayerGraphs(g, _.range(1, maxRank + 1), "inEdges"),
upLayerGraphs = buildLayerGraphs(g, _.range(maxRank - 1, -1, -1), "outEdges");
Expand All @@ -37,8 +38,12 @@ function order(g) {
var bestCC = Number.POSITIVE_INFINITY,
best;

for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2);
var constraints = opts.constraints || [];

for (var i = 0, lastBest = 0; lastBest < 2; ++i, ++lastBest) {
var biasRight = i % 2 === 1;
sweepLayerGraphs(downLayerGraphs, biasRight, constraints);
sweepLayerGraphs(upLayerGraphs, biasRight, constraints);

layering = util.buildLayerMatrix(g);
var cc = crossCount(g, layering);
Expand All @@ -58,8 +63,13 @@ function buildLayerGraphs(g, ranks, relationship) {
});
}

function sweepLayerGraphs(layerGraphs, biasRight) {
function sweepLayerGraphs(layerGraphs, biasRight, constraints) {
var cg = new Graph();

_.forEach(constraints, function(constraint) {
cg.setEdge(constraint.left, constraint.right);
});

_.forEach(layerGraphs, function(lg) {
var root = lg.graph().root;
var sorted = sortSubgraph(lg, root, cg, biasRight);
Expand Down

0 comments on commit 742ff83

Please sign in to comment.