How to combine two egdes and nodes in to one that has common starting nodes in Networkx?

Viewed 901

I am quite new for networkx and I am asking help from the Stackeroverflow community.

I am trying to combine nodes and edges that have a common starting node as shown below in the figure. The arrow shows the expected result.

Italian Trulli

nodes_to_combine = [n for n in graph.nodes if len(list(graph.neighbors(n))) == 2]
for node in nodes_to_combine:
    graph.add_edge(*graph.neighbors(node))
nx.draw(graph, with_labels=True)

Can anyone help me to figure out this?

1 Answers

NetworkX has no functions to merge nodes in the graph so it should be implemented manually. Here is the example without attributes merging (it can has its own logic):

def merge(G, n1, n2):
    # Get all predecessors and successors of two nodes
    pre = set(G.predecessors(n1)) | set(G.predecessors(n2))
    suc = set(G.successors(n1)) | set(G.successors(n2))
    # Create the new node with combined name
    name = str(n1) + '/' + str(n2)
    # Add predecessors and successors edges
    # We have DiGraph so there should be one edge per nodes pair
    G.add_edges_from([(p, name) for p in pre])
    G.add_edges_from([(name, s) for s in suc])
    # Remove old nodes
    G.remove_nodes_from([n1, n2])

Here is how it works:

import networkx as nx

G = nx.DiGraph()
G.add_edges_from([
    ('0','20'),
    ('10','20'),
    ('10','30'),
    ('20','40'),
    ('30','50'),
])
nx.draw(
    G,
    pos=nx.nx_agraph.graphviz_layout(G, prog='dot'),
    node_color='#FF0000',
    with_labels=True
)

enter image description here

merge(G, '20', '30')
nx.draw(
    G,
    pos=nx.nx_agraph.graphviz_layout(G, prog='dot'),
    node_color='#FF0000',
    with_labels=True
)

enter image description here

Related