如何用某种概率连接两个图

2024-09-27 07:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有下一个图:red_graph和{},它们是以概率p1和{}随机构造的red_graph和{}来连接节点对:

red_graph = nx.fast_gnp_random_graph(N, p1)                                                                                                                                                                               
blue_graph = nx.fast_gnp_random_graph(N, p2)

现在我想将这两个图以某种概率连接成1(假设是q)。所以q-概率红色节点连接到蓝色节点。我在NetworkX文档中找不到任何与此相关的功能。有什么想法吗?在


Tags: 文档networkx节点randombluered概率graph
2条回答

您可以将整个过程分为三个步骤:

  1. 创建两个随机图(已经完成):red_graph和{}。在
  2. 将两个随机图合并为combined图。这只是一个棘手的问题,因为两个随机图中的节点具有相同的名称,但是您可以通过在blue_graph中的每个节点名中添加N来轻松解决这个问题。在
  3. 将边添加到combined图中,以概率red_graph和{}中的节点连接起来。您可以在一行中使用列表理解并获取red_graph和{}中的节点的^{}。在

下面是一个完整的示例,其结果随机图如下:

###############################################################################
# Step 0: Load required modules and set and parameters
###############################################################################
import networkx as nx, matplotlib.pyplot as plt
from itertools import product
from random import random

# Parameters
N  = 5
p1 = 0.5
p2 = 0.5
q  = 0.25

###############################################################################
# Step 1: Create the random graphs
###############################################################################
red_graph = nx.fast_gnp_random_graph(N, p1)
blue_graph = nx.fast_gnp_random_graph(N, p2)

###############################################################################
# Step 2: Combine the random graphs
###############################################################################
combined = nx.Graph()
red      = red_graph.nodes()
# rename the blue nodes
blue     = [ N + node for node in blue_graph.nodes() ] 
combined.add_nodes_from(red)
combined.add_edges_from(red_graph.edges())
combined.add_nodes_from(blue)
# Rename the blue edges with their new node names
combined.add_edges_from([ (N + u, N + v) for u, v in blue_graph.edges() ]) 

###############################################################################
# Step 3: Connect nodes in the blue/red graphs with probability q
###############################################################################
combined.add_edges_from([ (u, v) for u, v in product(red, blue) if random() < q ])


###############################################################################
# Step 4: Plot the graph, including the color of each node
###############################################################################
pos = nx.spring_layout(combined)
nx.draw_networkx_nodes(combined, pos=pos, nodelist=red, node_color='r')
nx.draw_networkx_nodes(combined, pos=pos, nodelist=blue, node_color='b')
nx.draw_networkx_edges(combined, pos=pos)
plt.show()

random graph

下面的代码允许子图也有不同数量的节点。它利用了nx.bipartite_random_graph,它有一个特别有效的实现:O(V+E),其中V是顶点数,E是边数。实现二部随机图的标准方法是O(N*M),其中这些是每个分区中的节点数。它使用了fast_gnp_random_graph作为O(V+E)而不是O(v2)的相同技巧。在

Nr= N
Nb = N
red_graph = nx.fast_gnp_random_graph(Nr, p1)
blue_graph = nx.fast_gnp_random_graph(Nb, p2)
main_graph = nx.bipartite_random_graph(Nr, Nb, q)

main_graph.add_edges_from(red_graph.edges_iter())
main_graph.add_edges_from(((Nr+x,Nr+y) for x,y in blue_graph.edges_iter()))

相关问题 更多 >

    热门问题