我正在尝试实施 Krager 的 Min。python中的cut算法解决以下问题。这个问题来自斯坦福的 edx 课程,“算法:设计与分析,第 1 部分”
该文件包含一个简单无向图的邻接表表示。有 200 个顶点标记为 1 到 200。文件中的第一列表示顶点标签,特定行(除第一列之外的其他条目)告诉所有与该顶点相邻的顶点。例如,第 6 行看起来像:“6 155 56 52 120 ......”。这只是意味着标签为 6 的顶点与标签为 155、56、52、120、......等的顶点相邻(即共享一条边)
运行最小割问题的随机收缩算法,并在上图中使用它来计算最小割。
它涉及以下邻接列表:https ://pastebin.pl/view/39b087f8
这是我在 python 中编写的代码——我知道实现很幼稚,但我只想在优化之前把它弄好。
import random
import copy
with open('Contraction hwk.txt') as f: #Please input the correct file path
proto = f.readlines()
proto2 = [(x.replace('\t',' ').strip('\n')).split() for x in proto]
adjl = [[int(x) for x in i] for i in proto2]
solnset = []
for x in range(1000):#Repeated a 100 times cause the algo is not always correct
wadjl = copy.deepcopy(adjl)
nodes = list(range(1,len(adjl)+1))
while len(nodes) > 2:
randnodeval = random.choice(nodes) #Select a random node
randnode = wadjl[randnodeval-1] #Assign the random node to a var --> randnode
randedgeval = random.choice(randnode[1:]) # Picks another random node(edge node) that is adjacent to the initial this will contract with the original random node
if randedgeval == randnodeval:
continue
for node in wadjl[randedgeval-1][1:]:#This loop will go to all nodes adjacent to the edge node and replace the value of the edge node with the random node
if node == randnodeval:#If the node is the same as the random node it removes the node as an adjacent node (Deletion of edge node from random node)
modnode = wadjl[node-1][1:]
edgevalnode = modnode.index(randedgeval)
wadjl[node-1].pop(edgevalnode+1)
continue
modnode = wadjl[node-1][1:]
edgevalnode = modnode.index(randedgeval)
wadjl[node-1][edgevalnode+1] = randnodeval
randnodeidx = wadjl[randedgeval-1][1:].index(randnodeval)#This yeilds the index of the random node in the adjaceny list of the edge node
wadjl[randedgeval-1].pop(randnodeidx+1)#The random node is removed from that list(Deletion of random node from edgenode)
randnode.extend(wadjl[randedgeval-1][1:])#The adjacency list of the edge node is merged with that of the random node
del wadjl[randedgeval-1][1:]#The edge node is deleted
try:#repeates/edges to itself are removed from the random node
repeats = [i for i,x in enumerate(randnode) if x == randnodeval]
for repeate in repeats:
randnode.pop(repeat)
except:
pass
#Edge node is removed from the nodes list
noderemove = nodes.index(randedgeval)
nodes.pop(noderemove)
for entry in wadjl:
if len(entry) > 1:
minc = len(entry) - 1 #edges in min cut case
solnset.append(minc) #append solution to solution set
break
# print(solnset)
print(min(solnset))
根据一些互联网搜索,答案似乎是 17。我得到的答案是 20,而且我不认为我的算法实现是正确的,因为解决方案集太多样化了。我相信如果实施得当,解决方案应该经常出现在这个集合中。