fruchterman_reingold_force_directed_layout
Force-directed layout for unweighted, undirected graphs using the Fruchterman-Reingold algorithm.
Complexity: O(|V|2 + |E|) per iteration (worst case); O(|V| + |E|) average case for grid variant
Defined in: <boost/graph/fruchterman_reingold.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/topology.hpp>
#include <iostream>
#include <vector>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, undirectedS>;
using Point = rectangle_topology<>::point_type;
int main() {
Graph g(5);
add_edge(0, 1, g); add_edge(1, 2, g);
add_edge(2, 3, g); add_edge(3, 4, g); add_edge(4, 0, g);
minstd_rand gen(42);
rectangle_topology<> topology(gen, 0.0, 0.0, 100.0, 100.0);
std::vector<Point> positions(num_vertices(g));
for (auto& p : positions) { p = topology.random_point(); }
auto pos_map = make_iterator_property_map(positions.begin(), get(vertex_index, g));
fruchterman_reingold_force_directed_layout(g, pos_map, topology);
for (auto v : make_iterator_range(vertices(g))) {
auto& p = positions[v];
std::cout << "vertex " << v << ": (" << p[0] << ", " << p[1] << ")\n";
}
}
vertex 0: (3.22949, 44.7223)
vertex 1: (27.9055, 95.3527)
vertex 2: (83.7227, 87.5095)
vertex 3: (93.4898, 32.0392)
vertex 4: (43.7456, 5.54459)
(1) Named parameter version
template<typename Graph, typename PositionMap, typename Topology,
typename Param, typename Tag, typename Rest>
void fruchterman_reingold_force_directed_layout(
const Graph& g,
PositionMap position,
const Topology& space,
const bgl_named_params<Param, Tag, Rest>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The graph object on which the algorithm will be applied. The type
|
IN/OUT |
|
The property map that stores the position of each vertex. It should
typically be initialized with the vertices at random locations (use
|
IN |
|
The topology used to lay out the vertices. This parameter describes both the size and shape of the layout area. Topologies are described in more detail (with a list of BGL-provided topologies) in separate documentation. |
IN |
|
Named parameters passed via |
| Direction | Parameter | Description |
|---|---|---|
IN |
|
Computes the magnitude of the attractive force between two adjacent
vertices. The function object |
IN |
|
Computes the magnitude of the repulsive force between any two vertices.
The function object |
IN |
|
Enumerates the pairs of vertices on which the repulsive force should be
applied. |
IN |
|
Determines the cooling schedule for the algorithm, which affects the rate
of movement of vertices and termination of the algorithm. The |
UTIL |
|
The displacement map is used to compute the amount by which each vertex
will move in each step. The |
IN |
|
This maps each vertex to an integer in the range
|
(2) Positional version
template<typename Graph, typename PositionMap, typename Topology,
typename AttractiveForce, typename RepulsiveForce,
typename ForcePairs, typename DisplacementMap,
typename Cooling>
void fruchterman_reingold_force_directed_layout(
const Graph& g,
PositionMap position,
const Topology& space,
AttractiveForce fa,
RepulsiveForce fr,
ForcePairs fp,
Cooling cool,
DisplacementMap displacement);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The graph object on which the algorithm will be applied. The type
|
IN/OUT |
|
The property map that stores the position of each vertex. Its value type
must be |
IN |
|
The topology used to lay out the vertices. |
IN |
|
Computes the magnitude of the attractive force between two adjacent vertices. |
IN |
|
Computes the magnitude of the repulsive force between any two vertices. |
IN |
|
Enumerates the pairs of vertices on which the repulsive force should be applied. |
IN |
|
Determines the cooling schedule for the algorithm. |
UTIL |
|
The displacement map used to compute vertex movement in each step. |
Description
This algorithm
[53] performs layout of unweighted, undirected graphs. Unlike the Kamada-Kawai layout algorithm, this algorithm directly supports the layout of disconnected graphs (but see the force_pairs named parameter). It is a force-directed algorithm, meaning that vertex layout is determined by the forces pulling vertices together and pushing them apart. Attractive forces occur between adjacent vertices only, whereas repulsive forces occur between every pair of vertices. Each iteration computes the sum of the forces on each vertex, then moves the vertices to their new positions. The movement of vertices is mitigated by the temperature of the system for that iteration: as the algorithm progresses through successive iterations, the temperature should decrease so that vertices settle in place. The cooling schedule, attractive forces, and repulsive forces can be provided by the user.
The vertices are often placed randomly prior to execution of this algorithm via random_graph_layout.