NiceGraph.js is a graph drawing library in JavaScript based on Simulated Annealing algorithm.
Basically, the algorithm takes a random drawing of a graph and tries to modify it in order to get a "better" drawing. It calculates the "energy" of each drawing and tries to choose the one with the lowest energy. Energy is increased because of five factors:
- Too close vertices
- Vertices too close to the borders of the canvas
- Too long edges
- Vertices too close to the edges
- Crossed edges
- sajs.js - Implementation of the Simulated Annealing method. May be used for any optimization problem.
- niceGraph.js - Implementation of the graph drawing algorithm described in [Davidson, Harel, Drawing graphs nicely using simulated annealing, ACM Transactions on Graphics, 1996, pp. 301-331] available here
- playground.html - UI to play with the algorithm settings
var NG = NiceGraph({
graph: {
edges: { // adjacency list
1: [2],
2: []
},
vertexCount: 2
},
bounds: { // canvas size
width: 800,
height: 500
},
SAInitialTemperature: 100, // affects number of iterations
SATemperatureDecreaseRate: 0.95,
nodeDistanceFactor: 100,
borderDistanceFactor: 20,
edgeLengthFactor: 100,
nodeEdgeDistanceFactor: 30,
edgeCrossingsFactor: 0
});
NG.SAInit();
NG.SARun();
var vertices = NG.getPositions();
Sample output:
{
"1": {
"x": 263.77582507027535,
"y": 303.58632941743184
},
"2": {
"x": 170.52686833599822,
"y": 319.76684884782725
}
}
- Performance is terrible. Most of the energy functions should be optimized.
- Some visualization of the values of energy functions during the process is required to monitor and properly debug the algorithm.
- Some energy functions may be modified to take into account the properties of the input graph, e.g. edge density.
Scripts are licensed under the MIT license