kamada_kawai_spring_layout
Kamada-Kawai spring layout for connected, undirected graphs.
Defined in: <boost/graph/kamada_kawai_spring_layout.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <boost/graph/circle_layout.hpp>
#include <boost/graph/topology.hpp>
#include <iostream>
#include <vector>
#include <iomanip>
struct Spring { double length; };
int main() {
using namespace boost;
using Graph = adjacency_list<vecS, vecS, undirectedS, no_property, Spring>;
Graph g(5);
add_edge(0, 1, Spring{1.0}, g);
add_edge(1, 2, Spring{1.0}, g);
add_edge(2, 3, Spring{1.0}, g);
add_edge(3, 4, Spring{1.0}, g);
add_edge(4, 0, Spring{1.0}, g);
using Topo = rectangle_topology<>;
Topo topo(0.0, 0.0, 100.0, 100.0);
using Point = Topo::point_type;
std::vector<Point> pos(num_vertices(g));
auto pos_map = make_iterator_property_map(pos.begin(), get(vertex_index, g));
circle_graph_layout(g, pos_map, 25.0);
// Bundled spring-length via member pointer — no edge_weight_t tag needed.
bool ok = kamada_kawai_spring_layout(g, pos_map, get(&Spring::length, g),
topo, side_length(50.0), layout_tolerance<double>(0.1));
std::cout << std::fixed << std::setprecision(1);
std::cout << "converged: " << (ok ? "yes" : "no") << "\n";
for (auto v : make_iterator_range(vertices(g))) {
std::cout << "vertex " << v << ": ("
<< pos[v][0] << ", " << pos[v][1] << ")\n";
}
}
converged: yes
vertex 0: (21.7, -0.0)
vertex 1: (7.7, 23.8)
vertex 2: (-20.2, 14.7)
vertex 3: (-20.2, -14.7)
vertex 4: (5.8, -22.0)
(1) Full version
template<typename Topology, typename Graph, typename PositionMap,
typename WeightMap, typename T, bool EdgeOrSideLength,
typename Done, typename VertexIndexMap,
typename DistanceMatrix, typename SpringStrengthMatrix,
typename PartialDerivativeMap>
bool kamada_kawai_spring_layout(
const Graph& g,
PositionMap position,
WeightMap weight,
const Topology& space,
unspecified edge_or_side_length,
Done done,
typename property_traits<WeightMap>::value_type spring_constant,
VertexIndexMap index,
DistanceMatrix distance,
SpringStrengthMatrix spring_strength,
PartialDerivativeMap partial_derivatives);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The graph, whose type |
OUT |
|
This property map is used to store the position of each vertex. The type
|
IN |
|
The weight or "length" of each edge in the graph. The weights must all be
non-negative, and the algorithm will throw a
|
IN |
|
The topology used to lay out the vertices. This parameter describes both the size and shape of the layout area, as well as its dimensionality; up to three dimensions are supported by the current implementation. Topologies are described in more detail (with a list of BGL-provided topologies) in separate documentation. |
IN |
|
Provides either the unit length |
IN |
|
A 4-argument function object that is passed the current value of delta_p
(i.e., the energy of vertex |
IN |
|
The constant multiplied by each spring’s strength. Larger values create
systems with more energy that can take longer to stabilize; smaller values
create systems with less energy that stabilize quickly but do not
necessarily result in pleasing layouts. |
IN |
|
A mapping from vertices to index values between 0 and
|
UTIL/OUT |
|
This parameter will be used to store the distance from every vertex to
every other vertex, which is computed in the first stages of the
algorithm. This value’s type must be a model of
|
UTIL/OUT |
|
This matrix will be used to store the strength of the spring between
every pair of vertices. This value’s type must be a model of
BasicMatrix with value type equal to the
value type of the weight map. |
UTIL |
|
A property map that will be used to store the partial derivatives of each
vertex with respect to the vertex’s current coordinates. This must be a
Read/Write Property Map
whose value type is |
(2) Without utility maps
template<typename Topology, typename Graph, typename PositionMap,
typename WeightMap, typename T, bool EdgeOrSideLength,
typename Done, typename VertexIndexMap>
bool kamada_kawai_spring_layout(
const Graph& g,
PositionMap position,
WeightMap weight,
const Topology& space,
unspecified edge_or_side_length,
Done done,
typename property_traits<WeightMap>::value_type spring_constant,
VertexIndexMap index);
Same parameters as (1), but without the distance, spring_strength, and partial_derivatives utility maps, which will be created internally.
(3) With default spring constant
template<typename Topology, typename Graph, typename PositionMap,
typename WeightMap, typename T, bool EdgeOrSideLength,
typename Done>
bool kamada_kawai_spring_layout(
const Graph& g,
PositionMap position,
WeightMap weight,
const Topology& space,
unspecified edge_or_side_length,
Done done,
typename property_traits<WeightMap>::value_type spring_constant =
typename property_traits<WeightMap>::value_type(1));
Same parameters as (2), but uses a default vertex index map of get(vertex_index, g).
(4) Minimal version
template<typename Topology, typename Graph, typename PositionMap,
typename WeightMap, typename T, bool EdgeOrSideLength>
bool kamada_kawai_spring_layout(
const Graph& g,
PositionMap position,
WeightMap weight,
const Topology& space,
unspecified edge_or_side_length);
Same parameters as (3), but uses a default done function of layout_tolerance instantiated over the value type of the weight map and a default spring constant of 1.
Description
This algorithm [52] performs graph layout (in two dimensions) for connected, undirected graphs. It operates by relating the layout of graphs to a dynamic spring system and minimizing the energy within that system. The strength of a spring between two vertices is inversely proportional to the square of the shortest distance (in graph terms) between those two vertices. Essentially, vertices that are closer in the graph-theoretic sense (i.e., by following edges) will have stronger springs and will therefore be placed closer together.
Prior to invoking this algorithm, it is recommended that the vertices be placed along the vertices of a regular n-sided polygon via circle_graph_layout.