Skip to content

Instantly share code, notes, and snippets.

@Yaweni
Yaweni / contract_edges.py
Last active March 27, 2024 05:28 — forked from aanastasiou/contract_edges.py
Networkx Edge Contraction
#Edge contraction as per http://en.wikipedia.org/wiki/Edge_contraction
def contract_edges(G, nodes, new_node, ):
G2 = nx.MultiGraph()
G2.add_nodes_from(G.nodes(data=True))
G2.add_edges_from(G.edges(data=True))
# Add the node with its attributes
G2.add_node(new_node)
# Create the set of the edges that are to be contracted