This documents the development version of NetworkX. Documentation for the current release can be found here.
Returns the specified power of a graph.
The \(k\), denoted \(G^k\), is a graph on the same set of nodes in which two distinct nodes \(u\) and \(v\) are adjacent in \(G^k\) if and only if the shortest path distance between \(u\) and \(v\) in \(G\) is at most \(k\).
A NetworkX simple graph object.
- kpositive integer
The power to which to raise the graph
- NetworkX simple graph
Gto the power
If the exponent
kis not positive.
Gis not a simple graph.
This definition of “power graph” comes from Exercise 3.1.6 of Graph Theory by Bondy and Murty .
Bondy, U. S. R. Murty, Graph Theory. Springer, 2008.
The number of edges will never decrease when taking successive powers:
>>> G = nx.path_graph(4) >>> list(nx.power(G, 2).edges) [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)] >>> list(nx.power(G, 3).edges) [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
k`th power of a cycle graph on *n* nodes is the complete graph on *n* nodes, if `kis at least
n // 2:
>>> G = nx.cycle_graph(5) >>> H = nx.complete_graph(5) >>> nx.is_isomorphic(nx.power(G, 2), H) True >>> G = nx.cycle_graph(8) >>> H = nx.complete_graph(8) >>> nx.is_isomorphic(nx.power(G, 4), H) True