contracted_nodes¶

contracted_nodes
(G, u, v, self_loops=True)[source]¶ Returns the graph that results from contracting
u
andv
.Node contraction identifies the two nodes as a single node incident to any edge that was incident to the original two nodes.
Parameters:  G (NetworkX graph) – The graph whose nodes will be contracted.
 v (u,) – Must be nodes in
G
.  self_loops (Boolean) – If this is
True
, any edges joiningu
andv
inG
become selfloops on the new node in the returned graph.
Returns: A new graph object of the same type as
G
(leavingG
unmodified) withu
andv
identified in a single node. The right nodev
will be merged into the nodeu
, so onlyu
will appear in the returned graph.Return type: Networkx graph
Examples
Contracting two nonadjacent nodes of the cycle graph on four nodes yields the path graph (ignoring parallel edges):
>>> import networkx as nx >>> G = nx.cycle_graph(4) >>> M = nx.contracted_nodes(G, 1, 3) >>> P3 = nx.path_graph(3) >>> nx.is_isomorphic(M, P3) True
See also
Notes
This function is also available as
identified_nodes
.