Last active
June 9, 2023 03:23
-
-
Save Zulko/7629206 to your computer and use it in GitHub Desktop.
This is a function to merge several nodes into one in a Networkx graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a function to merge several nodes into one in a Networkx graph | |
def merge_nodes(G,nodes, new_node, attr_dict=None, **attr): | |
""" | |
Merges the selected `nodes` of the graph G into one `new_node`, | |
meaning that all the edges that pointed to or from one of these | |
`nodes` will point to or from the `new_node`. | |
attr_dict and **attr are defined as in `G.add_node`. | |
""" | |
G.add_node(new_node, attr_dict, **attr) # Add the 'merged' node | |
for n1,n2,data in G.edges(data=True): | |
# For all edges related to one of the nodes to merge, | |
# make an edge going to or coming from the `new gene`. | |
if n1 in nodes: | |
G.add_edge(new_node,n2,data) | |
elif n2 in nodes: | |
G.add_edge(n1,new_node,data) | |
for n in nodes: # remove the merged nodes | |
G.remove_node(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bilalzahid make a pull request!!