Skip to content

Instantly share code, notes, and snippets.

View aanastasiou's full-sized avatar

Athanasios Anastasiou aanastasiou

View GitHub Profile
@aanastasiou
aanastasiou / contract_edges.py
Last active March 26, 2024 17:18 — forked from Zulko/nx_merge_nodes.py
Networkx Edge Contraction
#Edge contraction as per http://en.wikipedia.org/wiki/Edge_contraction
def contract_edges(G,nodes, new_node, attr_dict=None, **attr):
'''Contracts the edges of the nodes in the set "nodes" '''
#Add the node with its attributes
G.add_node(new_node, attr_dict, **attr)
#Create the set of the edges that are to be contracted
cntr_edge_set = G.edges(nodes, data = True)
#Add edges from new_node to all target nodes in the set of edges that are to be contracted
#Possibly also checking that edge attributes are preserved and not overwritten,