如何使用 networx 从图中获取概率分布
How to use networx to get the probability distribution from a graph
我需要使用 networkx
库根据同一图的概率分布绘制图 G5
的最大连通图。我已经设法使用 networkx.largest_connected_components(Graph)
函数来获取相同的最大连接组件的列表。现在我需要访问同一图的概率分布并将其针对最大连通分量绘制,以便我可以分析概率增加对最大连通分量的影响,下面是我的 python 文件...
import matplotlib.pyplot as plt
import networkx as nx
import random
random.seed()
n = random.randint(0, 1000)
s = random.randint(1,20)
prob = 0.5
G5 = nx.gnp_random_graph(n,prob,s, False)
##get the largest connected components using nx
largest=max(nx.largest_connected_components(G5),key=len)
##get the probability distribution
##i need help with using networkx to get the probability distribution
##of the graph G5 so i can plot with matplotlib
plt.xlabel('Probablity')
plt.ylabel('Largest Connected Component')
plt.show()
试试这个:
import networkx as nx
n_nodes = 1000 # number of nodes in the graph
n = 100 # number of points in the graph
h = 1 / (n - 1)
x = [h * i for i in range(n)]
y = [max(nx.connected_components(nx.gnp_random_graph(n_nodes, prob)),
key=len)
for prob in x]
现在你有了 x 和 y,你可以在图表中绘制它们
我需要使用 networkx
库根据同一图的概率分布绘制图 G5
的最大连通图。我已经设法使用 networkx.largest_connected_components(Graph)
函数来获取相同的最大连接组件的列表。现在我需要访问同一图的概率分布并将其针对最大连通分量绘制,以便我可以分析概率增加对最大连通分量的影响,下面是我的 python 文件...
import matplotlib.pyplot as plt
import networkx as nx
import random
random.seed()
n = random.randint(0, 1000)
s = random.randint(1,20)
prob = 0.5
G5 = nx.gnp_random_graph(n,prob,s, False)
##get the largest connected components using nx
largest=max(nx.largest_connected_components(G5),key=len)
##get the probability distribution
##i need help with using networkx to get the probability distribution
##of the graph G5 so i can plot with matplotlib
plt.xlabel('Probablity')
plt.ylabel('Largest Connected Component')
plt.show()
试试这个:
import networkx as nx
n_nodes = 1000 # number of nodes in the graph
n = 100 # number of points in the graph
h = 1 / (n - 1)
x = [h * i for i in range(n)]
y = [max(nx.connected_components(nx.gnp_random_graph(n_nodes, prob)),
key=len)
for prob in x]
现在你有了 x 和 y,你可以在图表中绘制它们