graphopt_layout#
- graph_tool.draw.graphopt_layout(g, weight=None, pos=None, dim=2, niter=500, charge=0.001, mass=30.0, spring_length=0.0, spring_constant=1.0, max_move=5.0, cutoff=500.0, theta=0.6)[git][source]#
Calculate a force-directed layout of the graph with the GraphOpt model.
- Parameters:
- g
Graph Graph to be used (the edge directions are ignored).
- weight
EdgePropertyMap(optional, default:None) Non-negative edge weights, which multiply the spring constant. If not given, every edge has unit weight.
- pos
VertexPropertyMap(optional, default:None) Initial positions, which are modified in place. If not given, random positions in a square (or cube) of side \(20\sqrt{V}\) are used.
- dim
int(optional, default:2) Number of dimensions.
- niter
int(optional, default:500) Number of iterations.
- charge
float(optional, default:0.001) Electric charge \(q\) of every vertex.
- mass
float(optional, default:30.) Mass \(m\) of every vertex.
- spring_length
float(optional, default:0.) Rest length \(\ell\) of the springs.
- spring_constant
float(optional, default:1.) Spring constant \(k_s\).
- max_move
float(optional, default:5.) Largest move of a vertex along every axis in one iteration.
- cutoff
float(optional, default:500.) Distance beyond which vertices do not repel. If
None, there is no cutoff.- theta
float(optional, default:0.6) Opening angle of the Barnes-Hut approximation of the repulsion, in two dimensions. If
0, all pairs are summed exactly.
- g
- Returns:
- pos
VertexPropertyMap The vertex positions.
- pos
See also
fruchterman_reingold_layoutFruchterman-Reingold spring-block layout
arf_layoutARF spring-block layout
forceatlas2_layoutForceAtlas2 force-directed layout
sfdp_layoutMultilevel force-directed layout
Notes
In the GraphOpt model of Michael Schmuhl [schmuhl-graphopt], the vertices are electric charges that repel each other according to Coulomb’s law, with the force \(k_e q^2 / d_{ij}^2\) for distances below
cutoff, where \(k_e = 8.9875\times 10^9\) is Coulomb’s constant, and the edges are springs that follow Hooke’s law, with the force \(k_s w_{ij}(d_{ij} - \ell)\), shared equally by the two endpoints. In every iteration, every vertex moves by its total force divided by its mass, limited tomax_movealong every axis. There is no cooling, so that the vertices may keep oscillating around a stable configuration.In two dimensions, the repulsion is computed with the Barnes-Hut approximation [barnes-hierarchical-1986], so that every iteration takes time \(O(V\log V + E)\); otherwise, or with
theta=0, it takes time \(O(V^2 + E)\). The initial positions are drawn with NumPy’s random number generator.Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
References
[schmuhl-graphopt]Michael Schmuhl, “graphopt”, https://web.archive.org/web/20220611030748/http://www.schmuhl.org/graphopt/
[barnes-hierarchical-1986]Josh Barnes and Piet Hut, “A hierarchical O(N log N) force-calculation algorithm”, Nature 324, 446-449 (1986), DOI: 10.1038/324446a0 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["football"] >>> pos = gt.graphopt_layout(g) >>> gt.graph_draw(g, pos=pos, vertex_fill_color=g.vp.value, ... output="football-graphopt.pdf") <...>
GraphOpt layout of the network of American college football games, with the vertex colors given by the conferences.#