我試圖設計一個需要全球定位數據的項目,例如城市和州名以及緯度和位置。我也會在每一對城市之間有距離。我想用所有這些信息製作一個圖表,並操縱它來執行一些圖表算法。我決定擁有包含每個位置數據的城市對象。現在我應該有一個散列函數來區分對象嗎?我應該如何處理組合節點和移除邊的圖算法?我應該如何創建我的對象,以便它能很好地與networkx協同工作?
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]))
它的形狀非常糟糕。我正在重寫我的原始程序,但這是我從以前的版本中採取的方法。
怎麼樣一些示例代碼? – Spaceghost 2011-01-19 18:54:23