我试图实现一个函数来生成一个完整的图形。功能还可以,但是当我尝试用28k节点运行它时,我的笔记本电脑崩溃了。我的实施有问题吗?
def make_complete_graph(num_nodes):
"""
Takes the number of nodes num_nodes and returns a dictionary corresponding
to a complete directed graph with the specified number of nodes. A
complete graph contains all possible edges subject to the restriction
that self-loops are not allowed. The nodes of the graph should be numbered
0 to num_nodes - 1 when num_nodes is positive. Otherwise, the function
returns a dictionary corresponding to the empty graph.
"""
if num_nodes<=0:
return dict()
else:
return {key:set(range(num_nodes))-set([key]) for key in range(num_nodes)}
我使用在anaconda下运行的Spyder,我的操作系统是ubuntu 15.04
编辑:我做了改进但仍然崩溃def make_complete_graph(num_nodes):
"""
Takes the number of nodes num_nodes and returns a dictionary corresponding
to a complete directed graph with the specified number of nodes. A
complete graph contains all possible edges subject to the restriction
that self-loops are not allowed. The nodes of the graph should be numbered
0 to num_nodes - 1 when num_nodes is positive. Otherwise, the function
returns a dictionary corresponding to the empty graph.
"""
nodes = set(range(num_nodes))
if num_nodes<=0:
return dict()
else:
return {key:nodes.difference(set([key])) for key in nodes}