-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeFlow.py
169 lines (133 loc) · 4.17 KB
/
EdgeFlow.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
"""
Created on Tue May 16 11:55:30 2017
Test flow
@author: Kun
"""
#our notation is going to change
#E = E_B union E_N are basic and non basic edges, which refer to as E_T and E_X
#E_T and E_X are part of E_B, where |E_X| = |R|
def flow(V, E_B, f_X, d_N):
R = V[0]
N = V[1]
f_T = {}
#demand dictionary of nodes
d = {}
E_X = E_B[1]
E_T = E_B[0]
d.update(d_N)
for v in R:
d[v] = 0
#need to fix this so that we dont need to store E_X in memory
#shouldnt f_X be all 0? because there is no flow on external arcs
#below hsould be E_X and not E_NB
for e in E_X:
#tail set
if e[1] is None:
if e[0] is not None:
d[e[0]] = d[e[0]] - f_X[e]
else:
if e[0] is not None:
d[(e[0][0],e[1])] = d[(e[0][0],e[1])] + f_X[e]
d[(e[0][1],e[1])] = d[(e[0][1],e[1])] + f_X[e]
d[(e[1])] = d[(e[1])] + f_X[e]
#head
d[(e[0][0],e[0][1])] = d[(e[0][0],e[0][1])] - f_X[e]
else:
d[(e[1])] = d[(e[1])] + f_X[e]
unvisitedcount = dict.fromkeys(R, 0)
unvisitedcount.update(dict.fromkeys(N, 0))
#for v in V, unvisited(v) = number of hyperarcs of T_R incident to v
#tail set
tail_set = set()
#here should be E_T instead of E_B
for e in E_T:
if e[1] is None:
unvisitedcount[e[0]] += 1
elif e[0] is None:
unvisitedcount[e[1]] += 1
else:
#ij
unvisitedcount[e[0]] += 1
#k
unvisitedcount[e[1]] += 1
tail_set.add(e[1])
#i,k
unvisitedcount[(e[0][0],e[1])] +=1
tail_set.add((e[0][0],e[1]))
#j,k
unvisitedcount[(e[0][1],e[1])] +=1
tail_set.add((e[0][1],e[1]))
Queue = []
#leaf: non root node that is not in a tail set of any e in T_R
#great!
# print unvisitedcount
for v in N:
if v not in tail_set:
Queue.append(v)
# print Queue
while Queue:
v_curr = Queue.pop(0)
# print v_curr
for e in E_T:
if e[0] == v_curr:
e_v = e
elif e[1] == v_curr:
e_v = e
#need to handle error better
# print e_v
# ij = e_v[0]
# k = e_v[1]
# ik = (e_v[0][0],e_v[1])
# jk = (e_v[0][1],e_v[1])
f_T[e_v] = d[v_curr]
# if v_curr == ij:
# f_T[e_v] = d[v_curr]
# else:
# f_T[e_v] = -d[v_curr]
if e_v[1] is not None:
if e_v[0] is None:
w_list = [e_v[1]]
else:
ij = e_v[0]
k = e_v[1]
ik = (e_v[0][0],e_v[1])
jk = (e_v[0][1],e_v[1])
w_list = [ij, k, ik, jk]
else:
w_list = [e_v[0]]
# print w_list
#or if v_curr in w_list: remove
if v_curr in w_list:
w_list.remove(v_curr)
# print w_list
# if w_list is None:
# print "w_list is None, wtf"
for w in w_list:
if w is not None:
if w == ij:
d[w] = d[w] - f_T[e_v]
else:
d[w] = d[w] + f_T[e_v]
#d[w] = d[w] + f_T[e_v]
# print w
# print unvisitedcount[w]
# print "stooooooooooooop"
unvisitedcount[w] -= 1
if unvisitedcount[w] == 1 and w not in R:
Queue.append(w)
# print Queue
# print f_T
# print v_curr
for v in V[0]:
temp = d[v]
d[v] = -temp
d_R = {}
d_N = {}
for v in d:
if v in R:
d_R[v] = d[v]
for v in d:
if v in N:
d_N[v] = d[v]
return d_R, d_N, f_T