networkx 2.8.2 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturnsBackRef
contracted_edge(G, edge, self_loops=True, copy=True)

Edge contraction identifies the two endpoints of the edge as a single node incident to any edge that was incident to the original two nodes. A graph that results from edge contraction is called a minor of the original graph.

Parameters

G : NetworkX graph

The graph whose edge will be contracted.

edge : tuple

Must be a pair of nodes in G.

self_loops : Boolean

If this is True, any edges (including :None:None:`edge`) joining the endpoints of :None:None:`edge` in G become self-loops on the new node in the returned graph.

copy : Boolean (default True)

If this is True, a the contraction will be performed on a copy of G, otherwise the contraction will happen in place.

Raises

ValueError

If :None:None:`edge` is not an edge in G.

Returns

Networkx graph

A new graph object of the same type as G (leaving G unmodified) with endpoints of :None:None:`edge` identified in a single node. The right node of :None:None:`edge` will be merged into the left one, so only the left one will appear in the returned graph.

Returns the graph that results from contracting the specified edge.

See Also

contracted_nodes
quotient_graph

Examples

Attempting to contract two nonadjacent nodes yields an error:

This example is valid syntax, but raise an exception at execution
>>> G = nx.cycle_graph(4)
... nx.contracted_edge(G, (1, 3)) Traceback (most recent call last): ... ValueError: Edge (1, 3) does not exist in graph G; cannot contract it

Contracting two adjacent nodes in the cycle graph on n nodes yields the cycle graph on n - 1 nodes:

>>> C5 = nx.cycle_graph(5)
... C4 = nx.cycle_graph(4)
... M = nx.contracted_edge(C5, (0, 1), self_loops=False)
... nx.is_isomorphic(M, C4) True
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.minors.contraction.contracted_nodes networkx.algorithms.minors.contraction.contracted_edge

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/minors/contraction.py#537
type: <class 'function'>
Commit: