Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
卡格尔';python 2.7中的s Min Cut实现没有给出正确的切割_Python_Algorithm_Python 2.7_Graph Theory_Graph Algorithm - Fatal编程技术网

卡格尔';python 2.7中的s Min Cut实现没有给出正确的切割

卡格尔';python 2.7中的s Min Cut实现没有给出正确的切割,python,algorithm,python-2.7,graph-theory,graph-algorithm,Python,Algorithm,Python 2.7,Graph Theory,Graph Algorithm,我通过将下面的数据保存在dataGraph.txt文件中来执行上面的代码。最小截数是1,正确的截数是(4,5),但在算法的单独迭代中,我得到(1,7)、(3,6)、(8,4)、(1,5)、(3,7)、(4,5)也作为最小截对 所以我的问题是,虽然切肉i.e.1是正确的,但是为什么我要把这些其他的对作为切肉呢 在下面的数据中,每行的第一个元素表示一个节点,该行的其余元素表示第一个元素连接到的节点。e、 G1连接到3、4和2 from random import randint from col

我通过将下面的数据保存在dataGraph.txt文件中来执行上面的代码。最小截数是1,正确的截数是(4,5),但在算法的单独迭代中,我得到(1,7)、(3,6)、(8,4)、(1,5)、(3,7)、(4,5)也作为最小截对

所以我的问题是,虽然切肉i.e.1是正确的,但是为什么我要把这些其他的对作为切肉呢

在下面的数据中,每行的第一个元素表示一个节点,该行的其余元素表示第一个元素连接到的节点。e、 G1连接到3、4和2

 from random import randint
 from collections import defaultdict

 ######################

 def getGraph(filename):
 '''
 The function takes a txt file as input and returns
 the Graph with 1st element of each row as a vertex.
 '''
     with open(filename, 'r') as f_in:
         G = defaultdict(list)
         for row in f_in:
             row = row.split()
             G[row[0]] = row[1 : ]
     return G

 ######################

 def getVEPair(range):
 '''
 The function takes the range as input and returns a
 pair of random integers.
 '''
     v = randint(1, range)
     e = randint(1, range)
     return v, e

 ######################

 def removeVEPair(G, V, E):
 '''
 The function takes Graph, Vertex  and  Edge as input
 and removes this pair from the Graph. The function then
 returns the resulting Graph.
 '''
     while E in G[V]:
         G[V].remove(E)
     return G

 ######################

 def contractNodes(G, V, E):
 '''
 The function takes a Graph, vertex and edge as the input
 and contracts the two nodes which the edge connects. The
 function then returns the resulting Graph.
 '''
     edges = G[E]
     for edge in edges:

         if edge != V:
             G[V].append(edge)
     return G

 ######################

 def removeNode(G, V, E):
 '''
 The function intakes the graph and the node to be deleted.
 The function deletes the node and updates all instances of
 the deleted node in the Graph.
 The function then returns the resulting Graph.
 '''
     del G[E]
     for Vertex in G:
         while E in G[Vertex]:
             G[Vertex].remove(E)
             if V != Vertex:
                 G[Vertex].append(V)
     return G

 ######################

 def kargerMinCut():
 '''
 The function takes a graph as input and returns the min cut
 of the graph.
 '''
     minCut = []
     for i in range(0, 100):
         G = getGraph('dataGraph.txt')
         while(len(G) > 2):
             v, e = getVEPair(8)
             V = str(v)
             E = str(e)
             keys = G.keys()
             if V in keys and E != V:
                 if E in G[V]:
                     G = removeVEPair(G, V, E)
                     G = contractNodes(G, V, E)
                     G = removeNode(G, V, E)
                 else:
                     continue
         print G
         for v in G:
             minCut.append(len(G[v]))
             break
     return minCut
 ######################

 minCut = kargerMinCut()
 print '######################'
 print minCut
 print min(minCut)

我认为说“正确的切割是(4,5)”是不正确的

我认为更正确的说法是“正确的切割是两组{1,2,3,4}和{5,6,7,8}”。割是将图的顶点分成两部分,割集(您似乎指的是)是连接这两个集的边集。在图的情况下,对应于最小割集的割集是{(4,5)}

为什么会得到(1,5)等“不正确”的结果?这是因为当收缩边并将两个节点合并为一个节点时,不会重新标记合并的节点。合并节点保留两个节点之一的名称。当到达终点时,算法碰巧找到了一个大小为1的切割,两个幸存节点的标签是最小切割两侧的节点的标签,这些节点恰好没有被删除。正如我所说,正确的切割是({1,2,3,4},{5,6,7,8}):值1和5只是切割两侧的代表

您需要调整代码,以便在收缩边并将两个节点合并为一个节点时调整节点标签。最后,可以从两个幸存节点的标签中读取剪切,并从连接两组节点的边中读取剪切集

1 3 4 2
2 1 4 3
3 1 2 4
4 5 3 2 1
5 4 8 6 7
6 8 7 5
7 5 8 6
8 5 7 6