/
dinit.py
executable file
·89 lines (86 loc) · 2.05 KB
/
dinit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from Data import graphs, init_residuals,Ds,breadth_first,Neighbours
import numpy as np
def max_flow_print(ID):
s=Ds[ID]
t=s+1
flow=np.zeros((t+1,t+1))
graph=graphs[ID]
residual=init_residuals[ID]
dist=1
f= open('dinit_'+str(ID)+'.txt', 'w')
while not(np.isinf(dist)):
d=0
while d<t+1:
f.write(" ".join(map(str, residual[d])))
f.write("\n")
d+=1
#find the shortest s-t path
st_path,dist=breadth_first(residual,s,t)
#find the maximum avaible flow
i=0
K=np.inf
while i<np.size(st_path)-1:
a=st_path.item(i)
b=st_path.item(i+1)
if graph.item((a,b))-flow.item((a,b)) < K :
K=graph.item((a,b))-flow.item((a,b))
i+=1
#update residual and flow
i=0
while i<np.size(st_path)-1:
a=st_path.item(i)
b=st_path.item(i+1)
#update flow
flow.itemset((a,b),flow.item((a,b))+K)
flow.itemset((b,a),flow.item((b,a))-K)
#update residual
if graph.item((a,b))==flow.item((a,b)):
residual.itemset((a,b),0)
elif flow.item((a,b))>0 :
residual.itemset((b,a),1)
i+=1
d=0
while d<t+1:
f.write(" ".join(map(str, residual[d])))
f.write("\n")
d+=1
f.close
f1 = open('dinit_flow_'+str(ID)+'.txt', 'w')
f1.write(str(np.sum(flow[s])))
f1.close
def max_flow(ID):
print('-')
s=Ds[ID]
t=s+1
flow=np.zeros((t+1,t+1))
graph=graphs[ID]
residual=init_residuals[ID]
neighbours=Neighbours[ID]
t_visited=True
while t_visited:
#find the shortest s-t path
st_path,t_visited=breadth_first(residual,neighbours,s,t)
#find the maximum avaible flow
i=0
K=np.inf
while i<np.size(st_path)-1:
a=st_path.item(i)
b=st_path.item(i+1)
if graph.item((a,b))-flow.item((a,b)) < K :
K=graph.item((a,b))-flow.item((a,b))
i+=1
#update residual and flow
i=0
while i<np.size(st_path)-1:
a=st_path.item(i)
b=st_path.item(i+1)
#update flow
flow.itemset((a,b),flow.item((a,b))+K)
flow.itemset((b,a),flow.item((b,a))-K)
#update residual
if graph.item((a,b))==flow.item((a,b)):
residual.itemset((a,b),0)
elif flow.item((a,b))>0 :
residual.itemset((b,a),1)
i+=1
return(np.sum(flow[s]))