| 1 | <HTML> |
|---|
| 2 | <!-- |
|---|
| 3 | -- Copyright (c) 2004 Trustees of Indiana University |
|---|
| 4 | -- |
|---|
| 5 | -- Distributed under the Boost Software License, Version 1.0. |
|---|
| 6 | -- (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 7 | -- http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 8 | --> |
|---|
| 9 | |
|---|
| 10 | <Head> |
|---|
| 11 | <Title>Boost Graph Library: Fruchterman-Reingold Force-Directed Layout</Title> |
|---|
| 12 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
|---|
| 13 | ALINK="#ff0000"> |
|---|
| 14 | <IMG SRC="../../../boost.png" |
|---|
| 15 | ALT="C++ Boost" width="277" height="86"> |
|---|
| 16 | |
|---|
| 17 | <BR Clear> |
|---|
| 18 | <img src="figs/python.gif" alt="(Python)"/> |
|---|
| 19 | <TT>fruchterman_reingold_force_directed_layout</TT> |
|---|
| 20 | </H1> |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | <P> |
|---|
| 24 | <PRE> |
|---|
| 25 | <i>// named parameter version</i> |
|---|
| 26 | template<typename Graph, typename PositionMap, typename Dim, typename Param, |
|---|
| 27 | typename Tag, typename Rest> |
|---|
| 28 | void |
|---|
| 29 | fruchterman_reingold_force_directed_layout |
|---|
| 30 | (const Graph& g, |
|---|
| 31 | PositionMap position, |
|---|
| 32 | Dim width, |
|---|
| 33 | Dim height, |
|---|
| 34 | const bgl_named_params<Param, Tag, Rest>& params); |
|---|
| 35 | |
|---|
| 36 | <i>// non-named parameter version</i> |
|---|
| 37 | template<typename Graph, typename PositionMap, typename Dim, |
|---|
| 38 | typename AttractiveForce, typename RepulsiveForce, |
|---|
| 39 | typename ForcePairs, typename DisplacementMap, typename Cooling> |
|---|
| 40 | void |
|---|
| 41 | fruchterman_reingold_force_directed_layout |
|---|
| 42 | (const Graph& g, |
|---|
| 43 | PositionMap position, |
|---|
| 44 | Dim width, |
|---|
| 45 | Dim height, |
|---|
| 46 | AttractiveForce fa, |
|---|
| 47 | RepulsiveForce fr, |
|---|
| 48 | ForcePairs fp, |
|---|
| 49 | Cooling cool, |
|---|
| 50 | DisplacementMap displacement); |
|---|
| 51 | |
|---|
| 52 | template<typename Graph, typename PositionMap, typename Dim> |
|---|
| 53 | void |
|---|
| 54 | fruchterman_reingold_force_directed_layout(const Graph& g, |
|---|
| 55 | PositionMap position, |
|---|
| 56 | Dim width, |
|---|
| 57 | Dim height); |
|---|
| 58 | </PRE> |
|---|
| 59 | |
|---|
| 60 | <P> This algorithm [<A |
|---|
| 61 | HREF="bibliography.html#fruchterman91">58</A>] performs layout of |
|---|
| 62 | unweighted, undirected graphs. Unlike the <a |
|---|
| 63 | href="kamada_kawai_spring_layout.html">Kamada-Kawai</a> layout |
|---|
| 64 | algorithm, this algorithm directly supports the layout of disconnected |
|---|
| 65 | graphs (but see the <tt>force_pairs</tt> named parameter). It is a |
|---|
| 66 | <em>force-directed</em> algorithm, meaning that vertex layout is |
|---|
| 67 | determined by the forces pulling vertices together and pushing them |
|---|
| 68 | apart. Attractive forces occur between adjacent vertices only, whereas |
|---|
| 69 | repulsive forces occur between every pair of vertices. Each iteration |
|---|
| 70 | computes the sum of the forces on each vertex, then moves the vertices |
|---|
| 71 | to their new positions. The movement of vertices is mitigated by the |
|---|
| 72 | <i>temperature</i> of the system for that iteration: as the algorithm |
|---|
| 73 | progresses through successive iterations, the temperature should |
|---|
| 74 | decrease so that vertices settle in place. The cooling schedule, |
|---|
| 75 | attractive forces, and repulsive forces can be provided by the user. |
|---|
| 76 | |
|---|
| 77 | <p> The vertices are often placed randomly prior to execution of this algorithm via <a href="random_layout.html"><tt>random_graph_layout</tt></a>. |
|---|
| 78 | |
|---|
| 79 | <h3>Where Defined</h3> |
|---|
| 80 | |
|---|
| 81 | <a href="../../../boost/graph/fruchterman_reingold.hpp"><tt>boost/graph/fruchterman_reingold.hpp</tt></a> |
|---|
| 82 | |
|---|
| 83 | <h3>Parameters</h3> |
|---|
| 84 | |
|---|
| 85 | IN: <tt>const Graph& g</tt> |
|---|
| 86 | <blockquote> |
|---|
| 87 | The graph object on which the algorithm will be applied. |
|---|
| 88 | The type <tt>Graph</tt> must be a model of |
|---|
| 89 | <a href="./VertexAndEdgeListGraph.html">Vertex And Edge List Graph</a>.<br> |
|---|
| 90 | <b>Python</b>: This parameter is named <tt>graph</tt> in Python. |
|---|
| 91 | </blockquote> |
|---|
| 92 | |
|---|
| 93 | IN/OUT: <tt>PositionMap position</tt> |
|---|
| 94 | <blockquote> |
|---|
| 95 | The property map that stores the position of each vertex. It should |
|---|
| 96 | typically be initialized with the vertices at random locations (use |
|---|
| 97 | <a href="random_layout.html"><tt>random_graph_layout</tt></a>). The |
|---|
| 98 | type <tt>PositionMap</tt> must be a model of <a |
|---|
| 99 | href="../../property_map/LvaluePropertyMap.html">Lvalue Property |
|---|
| 100 | Map</a> such that the vertex descriptor type of <tt>Graph</tt> is |
|---|
| 101 | convertible to its key type. Its value type must be a structure |
|---|
| 102 | with fields <tt>x</tt> and <tt>y</tt>, representing the coordinates |
|---|
| 103 | of the vertex.<br> |
|---|
| 104 | <b>Python</b>: The position map must be a <tt>vertex_point2d_map</tt> for |
|---|
| 105 | the graph.<br> |
|---|
| 106 | <b>Python default</b>: <tt>graph.get_vertex_point2d_map("position")</tt> |
|---|
| 107 | </blockquote> |
|---|
| 108 | |
|---|
| 109 | IN: <tt>Dim width</tt> |
|---|
| 110 | <blockquote> |
|---|
| 111 | The width of the display area in which layout should occur. On |
|---|
| 112 | termination of the algorithm, the <tt>x</tt> coordinates of all |
|---|
| 113 | vertices will fall in <tt>[-width/2, width/2]</tt>. |
|---|
| 114 | </blockquote> |
|---|
| 115 | |
|---|
| 116 | IN: <tt>Dim height</tt> |
|---|
| 117 | <blockquote> |
|---|
| 118 | The height of the display area in which layout should occur. On |
|---|
| 119 | termination of the algorithm, the <tt>y</tt> coordinates of all |
|---|
| 120 | vertices will fall in <tt>[-height/2, height/2]</tt>. |
|---|
| 121 | </blockquote> |
|---|
| 122 | |
|---|
| 123 | <h3>Named Parameters</h3> |
|---|
| 124 | |
|---|
| 125 | IN: <tt>attractive_force(AttractiveForce fa)</tt> |
|---|
| 126 | <blockquote> |
|---|
| 127 | Computes the magnitude of the attractive force between two adjacent |
|---|
| 128 | vertices. The function object <tt>fa</tt> must accept four |
|---|
| 129 | parameters: the edge descriptor, <tt>k</tt>, the distance between the |
|---|
| 130 | vertices, and the graph. <tt>k</tt> is the square root of the ratio |
|---|
| 131 | of the display area to the number of vertices. <br> |
|---|
| 132 | <b>Default:</b> <tt>square_distance_attractive_force()</tt>, which |
|---|
| 133 | computes the attractive force as <code>dist<sup>2</sup>/k</code>.<br> |
|---|
| 134 | <b>Python</b>: Any callable Python object that matches the signature will suffice. |
|---|
| 135 | </blockquote> |
|---|
| 136 | |
|---|
| 137 | IN: <tt>repulsive_force(RepulsiveForce fr)</tt> |
|---|
| 138 | <blockquote> |
|---|
| 139 | Computes the magnitude of the repulsive force between any two |
|---|
| 140 | vertices. The function object <tt>fa</tt> must accept five |
|---|
| 141 | parameters: the two vertex descriptors, <tt>k</tt>, the distance between the |
|---|
| 142 | vertices, and the graph. <tt>k</tt> is the square root of the ratio |
|---|
| 143 | of the display area to the number of vertices. <br> |
|---|
| 144 | <b>Default:</b> <tt>square_distance_repsulsive_force()</tt>, which |
|---|
| 145 | computes the repulsive force as <code>k<sup>2</sup>/dist</code>.<br> |
|---|
| 146 | <b>Python</b>: Any callable Python object that matches the signature will suffice. |
|---|
| 147 | </blockquote> |
|---|
| 148 | |
|---|
| 149 | IN: <tt>force_pairs(ForcePairs fp)</tt> |
|---|
| 150 | <blockquote> |
|---|
| 151 | Enumerates the pairs of vertices on which the repulsive force should |
|---|
| 152 | be applied. <tt>fp</tt> is a function object taking two parameters: |
|---|
| 153 | the graph <tt>g</tt> and a binary function object that should be |
|---|
| 154 | passed each pair of vertices to be considered. The basic formulation |
|---|
| 155 | of the Fruchterman-Reingold algorithm computes repulsive forces |
|---|
| 156 | between all pairs of vertices (pass <tt>all_force_pairs()</tt> for |
|---|
| 157 | this parameter), which is functional for disconnected graphs but |
|---|
| 158 | tends to push the connected components toward the edges of the |
|---|
| 159 | display area. The grid variant of the algorithm places a grid over |
|---|
| 160 | the display area and only computes repulsive forces among vertices |
|---|
| 161 | within each rectangle in the grid. The grid variant can be more |
|---|
| 162 | efficient than the basic formulation and tends to produce better |
|---|
| 163 | layouts for disconnected graphs, but is not better overall: pass |
|---|
| 164 | <tt>make_grid_force_pairs(width, height, position, g)</tt> as this |
|---|
| 165 | parameter to use the grid variant. Other enumeration strategies may |
|---|
| 166 | yield better results for particular graphs. <br> |
|---|
| 167 | <b>Default:</b> <tt>make_grid_force_pairs(width, height, position, g)</tt><br> |
|---|
| 168 | <b>Python</b>: Unsupported parameter. |
|---|
| 169 | </blockquote> |
|---|
| 170 | |
|---|
| 171 | IN: <tt>cooling(Cooling cool)</tt> |
|---|
| 172 | <blockquote> |
|---|
| 173 | Determines the cooling schedule for the algorithm, which affects the |
|---|
| 174 | rate of movement of vertices and termination of the algorithm. The |
|---|
| 175 | <tt>cool</tt> parameter is a nullary function object (i.e., one that |
|---|
| 176 | takes no arguments) and returns the temperature for the current |
|---|
| 177 | iteration. When the returned temperature is zero, the algorithm |
|---|
| 178 | terminates. Cooling schedules should begin with some initial |
|---|
| 179 | temperature and gradually reduce the temperature to zero.<br> |
|---|
| 180 | <b>Default:</b> <tt>linear_cooling<double>(100)</tt><br> |
|---|
| 181 | <b>Python</b>: Any callable Python object that matches the signature will suffice. |
|---|
| 182 | </blockquote> |
|---|
| 183 | |
|---|
| 184 | UTIL: <tt>displacement_map(DisplacementMap displacement)</tt> |
|---|
| 185 | <blockquote> |
|---|
| 186 | The displacement map is used to compute the amount by which each |
|---|
| 187 | vertex will move in each step. The <tt>DisplacementMap</tt> type |
|---|
| 188 | carries the same requirements as the <tt>PositionMap</tt> type.<br> |
|---|
| 189 | <b>Default:</b> An <tt>iterator_property_map</tt> with a value type |
|---|
| 190 | of <tt>simple_point<double></tt> and using the given vertex index map.<br> |
|---|
| 191 | <b>Python:</b> Unsupported parameter. |
|---|
| 192 | </blockquote> |
|---|
| 193 | |
|---|
| 194 | IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt> |
|---|
| 195 | <blockquote> |
|---|
| 196 | This maps each vertex to an integer in the range <tt>[0, |
|---|
| 197 | num_vertices(g))</tt>. This is only necessary when no |
|---|
| 198 | displacement map is provided. |
|---|
| 199 | The type <tt>VertexIndexMap</tt> must be a model of <a |
|---|
| 200 | href="../../property_map/ReadablePropertyMap.html">Readable Property |
|---|
| 201 | Map</a>. The value type of the map must be an integer type. The |
|---|
| 202 | vertex descriptor type of the graph needs to be usable as the key |
|---|
| 203 | type of the map.<br> |
|---|
| 204 | <b>Default:</b> <tt>get(vertex_index, g)</tt> |
|---|
| 205 | Note: if you use this default, make sure your graph has |
|---|
| 206 | an internal <tt>vertex_index</tt> property. For example, |
|---|
| 207 | <tt>adjacenty_list</tt> with <tt>VertexList=listS</tt> does |
|---|
| 208 | not have an internal <tt>vertex_index</tt> property. |
|---|
| 209 | <br> |
|---|
| 210 | <b>Python:</b> Unsupported parameter. |
|---|
| 211 | </blockquote> |
|---|
| 212 | |
|---|
| 213 | <b>Python</b> IN: <tt>bool progressive</tt> |
|---|
| 214 | <blockquote> |
|---|
| 215 | When <tt>false</tt>, performs a random layout of the graph before |
|---|
| 216 | running the Fruchterman-Reingold algorithm. If <tt>true</tt>, the |
|---|
| 217 | algorithm is executing starting with the vertex configuration in |
|---|
| 218 | the <tt>position</tt> map.<br> |
|---|
| 219 | <b>Default</b>: <tt>False</tt>. |
|---|
| 220 | </blockquote> |
|---|
| 221 | |
|---|
| 222 | <H3>Complexity</H3> |
|---|
| 223 | |
|---|
| 224 | <P> The time complexity is <i>O(|V|<sup>2</sup> + |E|)</i> for each |
|---|
| 225 | iteration of the algorithm in the worse case. The average case for the |
|---|
| 226 | grid variant is <i>O(|V| + |E|)</i>. The number of iterations is |
|---|
| 227 | determined by the cooling schedule. |
|---|
| 228 | |
|---|
| 229 | <H3>Example</H3> |
|---|
| 230 | <a href="../example/fr_layout.cpp">libs/graph/example/fr_layout.cpp</a> |
|---|
| 231 | |
|---|
| 232 | <br> |
|---|
| 233 | <HR> |
|---|
| 234 | <TABLE> |
|---|
| 235 | <TR valign=top> |
|---|
| 236 | <TD nowrap>Copyright © 2004</TD><TD> |
|---|
| 237 | <A HREF="../../../people/doug_gregor.html">Doug Gregor</A>, Indiana University |
|---|
| 238 | </TD></TR></TABLE> |
|---|
| 239 | |
|---|
| 240 | </BODY> |
|---|
| 241 | </HTML> |
|---|