【发布时间】:2014-02-09 02:27:59
【问题描述】:
我正在尝试解决与图论相关的问题,但似乎无法记住/找到/理解正确/最佳的方法,所以我想我会问专家...
我有一个来自两个节点(示例代码中的 1 和 10)的路径列表。我试图找到要删除的最小节点数以切断所有路径。我也只能删除某些节点。
我目前已将其实现(如下)作为蛮力搜索。这在我的测试集上运行良好,但在扩展到具有 100K 中的路径和 100 中的可用节点的图形时将成为一个问题(因素问题)。现在,我并不关心删除节点的顺序,但我会在某些时候考虑到这一点(切换集合以在下面的代码中列出)。
我相信应该有一种方法可以使用最大流量/最小切割算法来解决这个问题。不过,我正在阅读的所有内容都以某种方式超出了我的想象。做这种事情已经好几年了,我似乎什么都不记得了。
所以我的问题是:
1) 除了测试所有组合并取最小集合之外,有没有更好的方法来解决这个问题?
2) 如果是这样,您能否解释一下,或者最好提供伪代码来帮助解释?我猜可能有一个库已经以某种方式做到了这一点(我最近一直在寻找和使用 networkX,但对其他人开放)
3) 如果不是(或什至如此),关于如何多线程/处理解决方案的建议?我想尝试从计算机上获得我所能获得的每一点性能。 (我在这个问题上找到了一些很好的线程,我只是没有机会实现,所以我想我会碰巧同时问。我首先想让一切正常工作,然后再进行优化。)
4) 关于使代码更“Pythonic”的一般建议(可能也有助于提高性能)。我知道我可以做出一些改进,而且对 Python 还是很陌生。
感谢您的帮助。
#!/usr/bin/env python
def bruteForcePaths(paths, availableNodes, setsTested, testCombination, results, loopId):
#for each node available, we are going to
# check if we have already tested set with node
# if true- move to next node
# if false- remove the paths effected,
# if there are paths left,
# record combo, continue removing with current combo,
# if there are no paths left,
# record success, record combo, continue to next node
#local copy
currentPaths = list(paths)
currentAvailableNodes = list(availableNodes)
currentSetsTested = set(setsTested)
currentTestCombination= set(testCombination)
currentLoopId = loopId+1
print "loop ID: %d" %(currentLoopId)
print "currentAvailableNodes:"
for set1 in currentAvailableNodes:
print " %s" %(set1)
for node in currentAvailableNodes:
#add to the current test set
print "%d-current node: %s current combo: %s" % (currentLoopId, node, currentTestCombination)
currentTestCombination.add(node)
# print "Testing: %s" % currentTestCombination
# print "Sets tested:"
# for set1 in currentSetsTested:
# print " %s" % set1
if currentTestCombination in currentSetsTested:
#we already tested this combination of nodes so go to next node
print "Already test: %s" % currentTestCombination
currentTestCombination.remove(node)
continue
#get all the paths that don't have node in it
currentRemainingPaths = [path for path in currentPaths if not (node in path)]
#if there are no paths left
if len(currentRemainingPaths) == 0:
#save this combination
print "successful combination: %s" % currentTestCombination
results.append(frozenset(currentTestCombination))
#add to remember we tested combo
currentSetsTested.add(frozenset(currentTestCombination))
#now remove the node that was add, and go to the next one
currentTestCombination.remove(node)
else:
#this combo didn't work, save it so we don't test it again
currentSetsTested.add(frozenset(currentTestCombination))
newAvailableNodes = list(currentAvailableNodes)
newAvailableNodes.remove(node)
bruteForcePaths(currentRemainingPaths,
newAvailableNodes,
currentSetsTested,
currentTestCombination,
results,
currentLoopId)
currentTestCombination.remove(node)
print "-------------------"
#need to pass "up" the tested sets from this loop
setsTested.update(currentSetsTested)
return None
if __name__ == '__main__':
testPaths = [
[1,2,14,15,16,18,9,10],
[1,2,24,25,26,28,9,10],
[1,2,34,35,36,38,9,10],
[1,3,44,45,46,48,9,10],
[1,3,54,55,56,58,9,10],
[1,3,64,65,66,68,9,10],
[1,2,14,15,16,7,10],
[1,2,24,7,10],
[1,3,34,35,7,10],
[1,3,44,35,6,10],
]
setsTested = set()
availableNodes = [2, 3, 6, 7, 9]
results = list()
currentTestCombination = set()
bruteForcePaths(testPaths, availableNodes, setsTested, currentTestCombination, results, 0)
print "results:"
for result in sorted(results, key=len):
print result
更新: 我使用 itertool 重新编写了代码以生成组合。它使代码更清晰,更快(并且应该更容易进行多进程。现在尝试找出建议的主节点和多进程功能。
def bruteForcePaths3(paths, availableNodes, results):
#start by taking each combination 2 at a time, then 3, etc
for i in range(1,len(availableNodes)+1):
print "combo number: %d" % i
currentCombos = combinations(availableNodes, i)
for combo in currentCombos:
#get a fresh copy of paths for this combiniation
currentPaths = list(paths)
currentRemainingPaths = []
# print combo
for node in combo:
#determine better way to remove nodes, for now- if it's in, we remove
currentRemainingPaths = [path for path in currentPaths if not (node in path)]
currentPaths = currentRemainingPaths
#if there are no paths left
if len(currentRemainingPaths) == 0:
#save this combination
print combo
results.append(frozenset(combo))
return None
【问题讨论】:
标签: python algorithm graph-algorithm mathematical-optimization python-multithreading