Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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
Algorithm Ford-Fulkerson-Max-Flow在Python中的实现_Algorithm_Ford Fulkerson - Fatal编程技术网

Algorithm Ford-Fulkerson-Max-Flow在Python中的实现

Algorithm Ford-Fulkerson-Max-Flow在Python中的实现,algorithm,ford-fulkerson,Algorithm,Ford Fulkerson,下面是我的python代码,用于在源为S和汇为D的多汇多源图(E)上执行Ford Fulkerson操作 最大流量为200万。我使用虚拟源和虚拟汇来解决这个问题。我知道我正确设置了虚拟源和汇。因为如果我使用Geek实现Geek,我的代码就会通过,但我真的想知道为什么我的代码不起作用。有人有什么建议吗 i_sources = len(E) n = i_sources +1 x = [[0]*(len(E[0])+2) for i in range(len(E)+2)] for i in rang

下面是我的python代码,用于在源为S和汇为D的多汇多源图(E)上执行Ford Fulkerson操作 最大流量为200万。我使用虚拟源和虚拟汇来解决这个问题。我知道我正确设置了虚拟源和汇。因为如果我使用Geek实现Geek,我的代码就会通过,但我真的想知道为什么我的代码不起作用。有人有什么建议吗

i_sources = len(E)
n = i_sources +1
x = [[0]*(len(E[0])+2)  for i in range(len(E)+2)]
for i in range(0, len(E)):
    E[i] += [0,0]
E += [[0]*len(E[0]) for i in range(2)]
for s in S: # set sources
    E[i_sources][s] = 2000000
for d in D: # set Drains
    E[d][n] = 2000000

l = [None]*len(E)
l[i_sources] = [float('Inf'), None, None]
q = [i_sources]
while(q != []):  # queue not empty
    i = q.pop(0)
    for j in range(0, len(E)):   # forward links
        if(l[j] == None and j != i and E[i][j] != 0):
            r = E[i][j]-x[i][j]
            if r>0:
                l[j] = [min(l[i][0], r), i, '+']

                q.append(j)
    for j in range(0, len(E)):   #reverse links
        if(l[j] == None and j != i and E[i][j] != 0):
            if x[j][i]>0:
                l[j] = [min(l[i][0], x[j][i]), i, '-']
                q.append(j)
    if(l[n] != None):  # backtrack if needed
        j = n
        while (j != i_sources):
            i = abs(l[j][1])
            if(l[j][2] == '+'):
                x[i][j] += l[n][0]
            else:
                x[j][i] -= l[n][0]
            j=i
        l = [None]*len(E)
        l[i_sources] = [float('Inf'), None, None]
        q = [i_sources]
sum_x = 0
for i in range(0, n):
    sum_x += x[i][n]
return min(sum_x, 2000000)

你是说福尔克森而不是福克纳吗?是的,对不起。。这就是我所指的。试着在算法中的重要点打印值。它们是你所期望的吗?我在相同的测试用例中失败了-你在这里发现问题了吗?