最大网络流算法 push-relabel 的 python实现

# !/user/bin/python
#
PUSH-RELABEL ALGORITHM
#
Author Dan Liu
h = []
e
= []
f
= []
# V=[0,1,2,3]
#
E=[(0,1),(1,2),(1,3),(0,2),(2,3)]
#
G=(V,E)
#
C={(0,1):3,(1,2):3,(1,3):1,(0,2):4,(2,3):5}
#
s=0
#
t=3
V = [0, 1 , 2 , 3 , 4 , 5 ]
E
= [(0, 1 ),( 1 , 2 ),( 1 , 3 ),( 1 , 4 ),(0, 2 ),( 2 , 4 ),( 3 , 5 ),( 4 , 5 )]
G
= (V,E)
C
= {(0, 1 ): 3 ,( 1 , 2 ): 1 ,( 1 , 3 ): 3 ,( 1 , 4 ): 4 ,(0, 2 ): 2 ,( 2 , 4 ): 2 ,( 3 , 5 ): 2 ,( 4 , 5 ): 3 }
s
= 0
t
= 5
Ef
= []
cf
= {}
def  init_preflow(G,s):
    
for  i  in  G[ 1 ]:
        
if  i[0]  == s:
            i
= (i[ 1 ],i[0])            
        Ef.append(i)
        
    
for  i  in  C.items():
        key
= i[0]
        
if  i[0][0]  ==  s:
            key
= (i[0][ 1 ],i[0][0])
        cf[key]
= i[ 1 ]
    
for  u  in  G[0]:
        h.append(0)
        e.append(0)
    
for  u  in  G[0]:
        l
= []
        
for  v  in  G[0]:            
            l.append(0)
        f.append(l)
    
for  edge  in  G[ 1 ]:
        f[edge[0]][edge[
1 ]] = 0
        f[edge[0]][edge[
1 ]] = 0
    h[s]
= len(G[0])
    
for  edge  in  G[ 1 ]:
        
if  edge[0]  ==  s:
            u
= edge[ 1 ]
            tmp
= C.get(edge)
            f[s][u]
= tmp
            f[u][s]
=- tmp
            e[u]
= tmp
            e[s]
-= tmp
            
            
    
print   " h= " ,h
    
print   " f= " ,f
    
print   " e= " ,e

def  push_flow(u,v):    
    df
= min(e[u],cf.get((u,v)))
    f[u][v]
+= df
    f[v][u]
=- f[u][v]
    e[u]
-= df
    e[v]
+= df
    cf[(u,v)]
-= df    
    
if  cf[(u,v)] == 0:
        Ef.remove((u,v))
    
def  relabel(u):            
    Eh
= []
    
for  item  in  Ef:
        
if  item[0] == u:
            Eh.append(h[item[
1 ]])
    h[u]
= 1 + min(Eh)
    
def  check_e(e):
    
for  i  in  range(len(V)):
        
if  i  !=  s  and  i  !=  t:
            
if  e[i]  >  0:
                
return  i
    
return   - 1

def  check_push(u):
    
for  item  in  cf.items():        
        
if  (item[0][0])  ==  u  and  item[ 1 > and  (h[u] == (h[item[0][ 1 ]] + 1 )):
            
return  item[0][ 1 ]
    
return   - 1

def  check_relabel(u):
    
for  item  in  Ef:
        
if  item[0] == u:
            
if  h[u] > h[item[ 1 ]]:
                
return   1
    
return   - 1

def  generic_push_relabel(G):
    init_preflow(G,s)
    active_node
= check_e(e)
    
while  active_node  >  0:
        v
= check_push(active_node)
        is_high
= check_relabel(active_node)
        
if  v  !=   - 1 :
            
print  active_node, v
            push_flow(active_node, v)
        
elif  is_high  ==   - 1 :
        
# else:
            relabel(active_node)
        
print  e    
        active_node
= check_e(e)

generic_push_relabel(G)
print   " h= " ,h
print   " f= " ,f
print   " e= " ,e
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值