-
Notifications
You must be signed in to change notification settings - Fork 4
/
graph.py
192 lines (165 loc) · 5.28 KB
/
graph.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import math
from typing import List
class Node:
def __init__(self, v: int):
self.value: int = v
self.d = math.inf
self.inList: List[Arc] = []
self.outList: List[Arc] = []
self.predecessor: Node = None
self.pred_arc: Arc = None
self.num: int = None
self.labeled = False
self.contained = False
self.previously = False
self.in_degree = 0
self.order = 0
self.mass_balance = 0
self.flow_balance = 0
self.active_forward_arc = 0
self.active_reverse_arc = 0
def __str__(self):
return self.num.__str__()
def __repr__(self):
return self.num.__str__()
def __eq__(self, other):
if not isinstance(other, Node):
# don't attempt to compare against unrelated types
return NotImplemented
return self.num == other.num
class Arc:
def __init__(self, tail: Node, head: Node, cost=0, capacity=math.inf, flow=0):
self.cost = cost
self.capacity = capacity
self.flow = flow
self.residual_forward_capacity = capacity - flow
self.residual_reverse_capacity = flow
self.head = head
self.tail = tail
def __str__(self):
return self.tail.__str__() + " -> " + self.head.__str__()
def __repr__(self):
return self.tail.__str__() + " -> " + self.head.__str__()
def set_flow(self, flow):
if flow < 0:
return
if flow <= self.capacity:
delta = flow - self.flow
self.flow = flow
else:
delta = self.capacity - self.flow
self.flow = self.capacity
self.residual_forward_capacity = self.capacity - self.flow
self.residual_reverse_capacity = self.flow
self.tail.mass_balance -= delta
self.head.mass_balance += delta
class Path:
def __init__(self):
self.node_list: List[Node] = []
self.arc_list: List[Arc] = []
self.reverse = False
self.cycle = False
self.flow = 0
self.cost = 0
class Graph:
def __init__(self):
self.node_list: List[Node] = []
self.arc_list: List[Arc] = []
self.ordered: List[Node] = []
self.active_node_list: List[Node] = []
self.paths: List[Path] = []
self.s: Node = None
self.t: Node = None
self.negative = False
self.neg_cycle = False
self.nCycle = self.t
self.not_feasible = False
self.mcf_error = False
self.C = -math.inf
self.nodes_number = 0
self.exec_time = 0.0
self.is_ordered = False
self.source_flow = 0
self.times = 0
def number(self):
i = 1
for n in self.node_list:
n.num = i
i += 1
def initialize(self):
for n in self.node_list:
n.d = math.inf
n.labeled = False
n.previously = False
n.contained = False
n.predecessor = None
n.pred_arc = None
self.s.d = 0
self.nCycle = self.t
self.neg_cycle = False
self.order()
def previously(self):
for n in self.node_list:
n.previously = False
def contained(self):
for n in self.node_list:
n.contained = False
def negative_cost_detector(self):
self.nodes_number = len(self.node_list)
for a in self.arc_list:
self.C = max(self.C, a.cost)
if a.cost < 0:
self.negative = True
def order(self):
for n in self.node_list:
n.in_degree = 0
n.order = 0
next_n = 0
for a in self.arc_list:
a.head.in_degree += 1
zero_in_degree_list = []
for n in self.node_list:
if n.in_degree == 0:
zero_in_degree_list.append(n)
while len(zero_in_degree_list) > 0:
n = zero_in_degree_list.pop(0)
next_n += 1
n.order = next_n
self.ordered.append(n)
for a in n.outList:
a.head.in_degree -= 1
if a.head.in_degree == 0:
zero_in_degree_list.append(a.head)
self.is_ordered = next_n >= len(self.node_list)
def set_residual(self):
for a in self.arc_list:
a.residual_forward_capacity = a.capacity - a.flow
a.residual_reverse_capacity = a.flow
def reset_flows(self):
for n in self.node_list:
n.mass_balance = 0
n.active_forward_arc = 0
for a in self.arc_list:
a.flow = 0
self.set_residual()
def reverse_breadth_first_search(self):
self.previously()
q = [self.t]
self.t.previously = True
self.t.d = 0
while len(q) > 0:
n = q.pop(0)
for a in n.inList:
if not a.tail.previously:
a.tail.previously = True
a.tail.d = n.d + 1
q.append(a.tail)
def set_source_residual_flow(self):
self.source_flow = 0
for a in self.s.outList:
self.source_flow += a.residual_forward_capacity
def get_cost(self):
total_cost = 0
for a in self.arc_list:
total_cost += a.flow * a.cost
return total_cost