我试图删除一个随机过程中的边作为p的函数,其中p是从0到1。在第一次迭代中,从图中随机删除0.1或10%的节点。在第二次迭代中,20%的剩余边被删除,以此类推。 我的错误发生在被删除的边再次出现在随机函数中。你知道吗
我的尝试:
import networkx as nx
import random
import numpy as np
graph = nx.fast_gnp_random_graph(20,0.3)
p_values = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
for i in p_values:
print(i)
for i in p_values:
array=[]
n=nx.number_of_edges(graph)
edges = list(graph.edges)
no_edges_del = int(n*i)
print(no_edges_del)
for j in range(no_edges_del):
chosen_edge = random.choice(edges)
print(chosen_edge)
print(chosen_edge[0])
graph.remove_edge(chosen_edge[0], chosen_edge[1])
GC = nx.number_of_nodes(max(nx.connected_component_subgraphs(graph), key=len))
array.append(GC/n)
错误-
Traceback (most recent call last):
File "1.py", line 26, in <module>
graph.remove_edge(chosen_edge[0], chosen_edge[1])
File "D:\anaconda\lib\site-packages\networkx\classes\graph.py", line 1011, in remove_edge
raise NetworkXError("The edge %s-%s is not in the graph" % (u, v))
networkx.exception.NetworkXError: The edge 14-15 is not in the graph
在for循环开始之前得到一组边。当从图形中删除边时,需要从该集中删除边,以便在以后的迭代中不再选择它们。你知道吗
或者,在选择要删除的边之前,从每次迭代的图中获取边集。你知道吗
解决方案如下。
在每次迭代中,考虑当前百分比
p
并删除p*number_of_remaining_edges
边。你知道吗相关问题 更多 >
编程相关推荐