我正在写一个关于MST算法在Python2.7中传递巨大图形(比如1亿5千万条边)的应用程序。图形是用邻接列表设置的,使用一个经典类,方法如下:
def insertArcW(self, tail, head, weight):
if head in self.nodes and tail in self.nodes:
self.adj[tail].addAsLast(head)
self.adj[tail].addAsLast(weight)
def insertNode(self, e):
newnode = Node(self.nextId, e)
self.nextId += 1
我还使用了链表(用数组创建)和来自python标准库(版本2.7)的队列。 有了这段代码,插入速度非常快(因为节点数比边数少):
n = []
for i in xrange(int(file_List[0])):
n.append(G.insertNode(i))
插入边时出现问题:
for e in xrange(len(arc_List))
G.insertArcW(n[arc_List[e][0]].index, n[arc_List[e][1]].index,arc_List[e][2])
G.insertArcW(n[arc_List[e][1]].index, n[arc_List[e][0]].index,arc_List[e][2])
它的工作很好,有一百万个边缘,但更多它会吃掉我所有的内存(4GB,64位),但没有冻结!它可以在很长的时间内建立图形!考虑到这样做时CPU的使用率被限制在19/25%,有没有一种方法可以在多进程或多线程中做这样的事情?比如用两个核同时做相同的操作,但数据不同来构建图形?我的意思是一个核心与一半的边缘工作,另一个核心与另一半。 我对这个“编程之地”几乎是陌生的,尤其是在Python中。你知道吗
编辑:通过使用这个功能,我为节点和边设置了两个列表!我需要一个“.txt”文件来获取信息。插入insertArcW和insertNode内存在2.4GB到2.6GB之间振荡。现在我可以说这是稳定的(可能是由于“删除”了两个巨大的边和节点列表),但总是以相同的速度。代码:
f = open(graph + '.txt','r')
v = f.read()
file_List = re.split('\s+',v)
arc_List = []
n = []
p = []
for x in xrange(0,int(file_List[1])):
arc_List.append([0,0,0])
for i in xrange(int(file_List[0])):
n.append(G.insertNode(i))
for weight in xrange(1,int(file_List[1])+1):
p.append(weight)
random.shuffle(p)
i = 0
r = 0
while r < int(file_List[1]):
for k in xrange(2,len(file_List),2):
arc_List[r][0] = int(file_List[k])
arc_List[r][1] = int(file_List[k+1])
arc_List[r][2] = float(p[i])
G.insertArcW(n[arc_List[r][0]].index, n[arc_List[r][1]].index,arc_List[r][2])
G.insertArcW(n[arc_List[r][1]].index, n[arc_List[r][0]].index,arc_List[r][2])
print r
i+=1
r+=1
f.close()
目前没有回答
相关问题 更多 >
编程相关推荐