Connecting nodes of one graph to a node of another graph

Viewed 35

do you know how to connect all nodes of one graph to a node of another graph?

for node in messages:
    graph.add_edge(graph.nodes[22], node, label="channel_of")

messages is a graph and both graphs are of the same type. graph.nodes[22], which is a channel, returns the node, but not as the node itself but as a dictionary. That is why the single message node cannot be connected to the channel. How can I create an edge between the channel and all nodes of messages?

Many thanks in advance!

1 Answers

It seems you should create another graph that will contain graph and messages graphs

messages = nx.empty_graph(10)
graph = nx.empty_graph(2)

space = nx.union(messages, graph, rename=('m-', 'c-'))

for node in messages:
    space.add_edge(f'c-{0}', f'm-{node}', label="channel_of")

nx.draw_networkx(space, with_labels=True,
                 node_size=400, node_color='#7d99f5', font_weight='bold')

enter image description here

Related