graph_power#
- graph_tool.generation.graph_power(g, k)[git][source]#
Return the \(k\)-th power of a graph.
- Parameters:
- g
Graph Graph.
- k
int Power, i.e. maximum distance.
- g
- Returns:
- h
Graph The graph \(G^k\), with the same vertices as
g.
- h
See also
graph_compositionRelational composition of two graphs
graph_tool.structure.shortest_distanceDistances between vertices
graph_tool.structure.transitive_closureTransitive closure
Notes
In the \(k\)-th power of a graph, two distinct vertices are adjacent if their distance in the original graph is at most \(k\) (for directed graphs, \(u\to v\) if there is a directed path of length at most \(k\) from \(u\) to \(v\)). It is computed with a breadth-first search truncated at depth \(k\) from every vertex, in time \(O(N(N_k + E_k))\), where \(N_k\) and \(E_k\) are the numbers of vertices and edges within distance \(k\).
Examples
>>> g = gt.circular_graph(10) >>> print(gt.graph_power(g, 2).num_edges()) 20
The square of the cycle is drawn as follows, with the vertices placed on a circle:
>>> g = gt.graph_power(g, 2) >>> x = 2 * np.pi * np.arange(g.num_vertices()) / g.num_vertices() >>> pos = g.new_vp("vector<double>", ... vals=np.array([np.cos(x), np.sin(x)]).T) >>> gt.graph_draw(g, pos=pos, ... output="graph_power-cycle.pdf") <...>
Square of the cycle \(C_{10}\), with the vertices placed on a circle in the order of their indices.#