简体   繁体   English

在networkx python中,如何将一组新的节点和边连接到图中的每个节点?

[英]In networkx python, how to connect a new bunch of nodes and edges to each node in a graph?

Currently I created a graph as follow:目前我创建了一个图表如下:

import networkx as nx
edges = []
for i in range (10):
    edges.append((i,i+1))
edges += [(10,0), (1,10), (2,8), (3,7), (4,6), (4,10), (5,10)]
# create the graph
G = nx.Graph()
G.add_nodes_from([i for i in range (11)])
G.add_edges_from(edges)

Now what I need is to connect a random number of new nodes to each node of the above core network, according to a power law distribution with =3.现在我需要根据 =3 的幂律分布将随机数量的新节点连接到上述核心网络的每个节点。 So I got a new graph with power law distribution (for example: of 15 nodes):所以我得到了一个新的幂律分布图(例如:15个节点):

s1 = nx.utils.powerlaw_sequence(15, 3) #15 nodes, power-law exponent 3
G1 = nx.expected_degree_graph(s1, selfloops=False)

Now how can I connect this new graph to a certain node in my previous network?现在如何将这个新图连接到我之前网络中的某个节点? Tried add_nodes_from but they seem to overwrite previous nodes, which is odd;尝试过add_nodes_from但它们似乎覆盖了以前的节点,这很奇怪; and I can't make sure they're connected to a certain node.而且我无法确保它们已连接到某个节点。 Or is there any straightforward way to do this?或者有什么直接的方法可以做到这一点? Thanks for helping me out!谢谢你的协助!

The problem is due to the fact that nx.expected_degree_graph creates a graph whose nodes have labels 0... 14. If you try to join G and G1 , nodes with the same name are merged.问题是由于nx.expected_degree_graph创建了一个图,其节点的标签为 0... 14. 如果您尝试加入GG1 ,则具有相同名称的节点将被合并。

You need G1 to have nodes with unique labels.您需要G1拥有具有唯一标签的节点。 You can achieve such result by using the relabel_nodes function:您可以通过使用relabel_nodes function 来实现这样的结果:

relabel_dict = {node: node+len(G.nodes) for node in G1.nodes}
G1 = nx.relabel_nodes(G1, relabel_dict)

Now you can safely join G and G1 by using the compose function:现在您可以使用compose function 安全地加入GG1

new_G = nx.compose(G, G1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Graph Networkx 中找到具有公共节点的边? - How to find edges with common nodes in Graph Networkx? 使用networkx计算加权无向图中连接到一个节点的每个子图中的节点和边数 - Count the number of nodes and edges in each subgraph connected to one node in a weighted undirected graph with networkx 来自 pandas 的 Networkx 图,其中节点的边不连接其他节点和节点没有边 - Networkx graph from pandas with nodes with edges that don't connect other nodes and nodes with no edges 如何将边的二部列表转换为 NetworkX 中的图,每组节点都具有重叠标签? - How to convert a bipartite list of edges into a graph in NetworkX with each set of nodes having overlapping labels? 如何将带有(新节点,新边)的子图添加到 python 中的现有图 - how to add subgraph with (new nodes, new edges) to an existed graph in python 通过networkx并使用python在图中添加节点和边 - Add nodes & edges in graph via networkx and using python networkx python,有向/无向图,节点和边不一致? - networkx python, directed/undirected graph, inconsistent nodes and edges? 如何在Networkx图中过滤具有特殊字符的节点或边 - How to filter nodes or edges with special character in Networkx graph 如何更改networkX中二部图的节点和边缘的颜色? - How to change colours of nodes and edges of bipartite graph in networkX? 如何在Networkx中向网络分析图添加节点和边? - How to add nodes and edges to a network analysis graph in Networkx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM