我应该如何创建我的对象,使它能很好地与networkx一起工作?

2024-04-19 10:31:53 发布

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

我正试图设计一个项目,利用全球定位数据,比如城市和州名以及纬度和位置。我还要计算出每一对城市之间的距离。我想制作一个包含所有这些信息的图形,并对其进行操作以执行一些图形算法。我决定使用包含每个位置数据的城市对象。现在我应该有一个散列函数来区分对象吗?我该如何处理合并节点和移除边的图算法?在

def minCut(self):
    """Returns the lowest-cost set of edges that will disconnect a graph"""

    smcut = (float('infinity'), None)
    cities = self.__selectedcities[:]
    edges = self.__selectededges[:]
    g = self.__makeGRAPH(cities, edges)
    if not nx.is_connected(g):
        print("The graph is already diconnected!")
        return
    while len(g.nodes()) >1:
        stphasecut = self.mincutphase(g)
        if stphasecut[2] < smcut:
            smcut = (stphasecut[2], None) 
        self.__merge(g, stphasecut[0], stphasecut[1])
    print("Weight of the min-cut:  "+str(smcut[1]))

它的状况非常糟糕。我正在重写我原来的程序,但这是我从上一个版本采用的方法。在


Tags: ofthe数据对象self算法none图形
3条回答

合并节点时,可以创建新节点并将这些新节点散列到已合并的城市列表中。例如,在上面的代码中,您可以将新节点命名为len(g),并将其散列到stphasecut[0]+stphasecut[1]#假设stphasecut[1]和[2]是列表。在

根据您安装的networkx版本,有一个min_cut的内置实现。在

我安装了1.0RC1软件包,但它不可用。。但我升级到了1.4,闵琰切就在那里。在

以下是一个(愚蠢的)例子:

import networkx as nx
g = nx.DiGraph()
g.add_nodes_from(['London', 'Boston', 'NY', 'Dallas'])
g.add_edge('NY', 'Boston', capacity)
g.add_edge('Dallas', 'Boston')
g.add_edge('Dallas', 'London')
# add capacity to existing edge
g.edge['Dallas']['London']['capacity'] = 2
# create edge with capacity attribute
g.add_edge('NY', 'London', capacity=3)
print nx.min_cut(g, 'NY', 'London')

您不需要为city对象创建散列函数,您可以将city对象直接传递给Networkx-从教程“节点可以是任何散列对象,例如文本字符串、图像、XML对象、另一个图形、自定义节点对象等。”

您可以迭代城市列表并将它们添加为节点,然后迭代距离信息以生成图形。在

你看过教程了吗?http://networkx.lanl.gov/tutorial/tutorial.html

相关问题 更多 >