-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3e154c
commit dee9f99
Showing
10 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { default as dagCoordsSpread } from "./src/layout/coords/spread"; | ||
export { default as dagHierarchy } from "./src/dag/hierarchy"; | ||
export { default as dagLayerLongestPath } from "./src/layout/layering/longestPath"; | ||
export { default as dagLayerSimplex } from "./src/layout/layering/simplex"; | ||
export { default as dagLayout } from "./src/layout/index"; | ||
export { default as dagStratify } from "./src/dag/stratify"; | ||
export { default as topologicalSort } from "./src/topologicalSort"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import solver from "javascript-lp-solver"; | ||
|
||
export default function(dag) { | ||
// use null prefixes to prevent clash | ||
const variables = {}; | ||
const ints = {}; | ||
const constraints = {}; | ||
dag.eachDepth(node => { | ||
const nid = `\0${node.id}`; | ||
ints[nid] = 1; | ||
constraints[nid] = {"min": 0}; | ||
const variable = variables[nid] = { | ||
opt: node.parents.length - node.children.length, | ||
[nid]: 1, | ||
}; | ||
node.parents.forEach(parent => { | ||
variable[`${parent.id}\0${node.id}`] = 1; | ||
}); | ||
node.children.forEach(child => { | ||
const edge = `${node.id}\0${child.id}`; | ||
constraints[edge] = {"min": 1}; | ||
variable[edge] = -1; | ||
}); | ||
}); | ||
const assignment = solver.Solve({ | ||
optimize: "opt", | ||
opType: "min", | ||
constraints: constraints, | ||
variables: variables, | ||
ints: ints, | ||
}); | ||
// lp solver doesn't assign some zeros | ||
dag.eachDepth(n => n.layer = assignment[`\0${n.id}`] || 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const tape = require("tape"), | ||
fs = require("fs"), | ||
d3_dag = require("../../../"); | ||
|
||
const [square, grafo] = [ | ||
"test/data/square.json", | ||
"test/data/grafo.json", | ||
].map(file => d3_dag.dagStratify()(JSON.parse(fs.readFileSync(file)))); | ||
|
||
function toLayers(dag) { | ||
const layers = []; | ||
dag.eachDepth(n => (layers[n.layer] || (layers[n.layer] = [])).push(parseInt(n.id))); | ||
layers.forEach(l => l.sort((a, b) => a - b)); | ||
return layers; | ||
} | ||
|
||
tape("simplex() works for square", test => { | ||
d3_dag.dagLayerLongestPath(square); | ||
const layers = toLayers(square); | ||
test.equals(layers.length, 3); | ||
test.deepEquals(layers, [[0], [1, 2], [3]]); | ||
test.end(); | ||
}); | ||
|
||
tape("simplex() works for grafo", test => { | ||
d3_dag.dagLayerSimplex(grafo); | ||
const layers = toLayers(grafo); | ||
test.equals(layers.length, 8); | ||
const cost = grafo.links().reduce((s, l) => s + l.target.layer - l.source.layer, 0); | ||
test.equals(cost, 30); | ||
// XXX There are two possible configurations | ||
test.deepEquals( | ||
layers, | ||
[[1, 8], [0, 14], [16, 21], [10, 12], [2, 4, 19], [9, 11, 13, 15, 17], [3, 6, 18, 20], [5, 7]]); | ||
test.end(); | ||
}); |