Skip to content

Commit e06aeb1

Browse files
committed
spelling + bug fix
1 parent 1baa222 commit e06aeb1

File tree

4 files changed

+11
-181
lines changed

4 files changed

+11
-181
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
venv
12
.venv
23
.idea
34
__pycache__

main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,17 @@ def step(self):
282282

283283
def start(self):
284284
try:
285-
intervall = int(self.ent_time.get())
285+
interval = int(self.ent_time.get())
286286

287287
self.opt_algorithm["state"] = tk.DISABLED
288288
self.btn_step["state"] = tk.DISABLED
289289
self.btn_start["state"] = tk.DISABLED
290290
self.ent_time["state"] = tk.DISABLED
291291

292-
self._jop = window.after(intervall, self.start)
292+
self._jop = window.after(interval, self.start)
293293
self.step()
294294
except ValueError:
295-
messagebox.showerror("Error", "intervall must be an integer")
295+
messagebox.showerror("Error", "interval must be an integer")
296296

297297
def stop(self):
298298
self.btn_step["state"] = tk.NORMAL
@@ -328,7 +328,7 @@ def help(self):
328328
""")
329329

330330

331-
class TestEnviroment(tk.Frame):
331+
class TestEnvironment(tk.Frame):
332332

333333
def __init__(self, parent):
334334
super().__init__(parent)
@@ -434,9 +434,9 @@ def start_test(self):
434434
frame_visualization.pack()
435435
tabs.add(frame_visualization, text="Visualization")
436436

437-
frame_test_envoirement = TestEnviroment(tabs)
438-
frame_test_envoirement.pack()
439-
tabs.add(frame_test_envoirement, text="Test environment")
437+
frame_test_environment = TestEnvironment(tabs)
438+
frame_test_environment.pack()
439+
tabs.add(frame_test_environment, text="Test environment")
440440

441441
tabs.pack(expand=True, fill="both")
442442

max_flow.py

Lines changed: 3 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def preflow():
141141
edge.flow = edge.capacity
142142
excess[edge.end] += edge.flow
143143

144-
active.append(edge.end)
145-
in_queue[edge.end] = True
144+
if edge.end != target:
145+
active.append(edge.end)
146+
in_queue[edge.end] = True
146147

147148
edges.append(edge)
148149
return edges, excess, label, source
@@ -189,165 +190,3 @@ def relabel(node: int):
189190
if u not in (source, target) and excess[u] > 0:
190191
active.append(u)
191192
in_queue[u] = True
192-
193-
194-
"""
195-
class MaxFlow(ABC):
196-
197-
def __init__(self, graph: Graph, source: int, target: int):
198-
self.graph = graph
199-
self.source = source
200-
self.target = target
201-
202-
@abstractmethod
203-
def step(self):
204-
pass
205-
206-
207-
class FordFulkerson(MaxFlow):
208-
209-
def __init__(self, graph: Graph, source: int, target: int, path_algo=dfs):
210-
super().__init__(graph, source, target)
211-
self.path_algo = path_algo
212-
213-
def step(self):
214-
if result := self.path_algo(self.graph, self.source, self.target):
215-
parent, *_ = result
216-
path_flow = math.inf
217-
218-
tmp = self.target
219-
path = deque()
220-
while tmp != self.source:
221-
path_flow = min(path_flow, parent[tmp].residual_capacity())
222-
path.appendleft(parent[tmp])
223-
tmp = parent[tmp].start
224-
225-
tmp = self.target
226-
while tmp != self.source:
227-
parent[tmp].adjust(path_flow)
228-
tmp = parent[tmp].start
229-
230-
return list(path)
231-
232-
233-
class EdmondsKarp(FordFulkerson):
234-
235-
def __init__(self, graph: Graph, source: int, target: int):
236-
super().__init__(graph, source, target, bfs)
237-
238-
239-
class CapacityScaling(MaxFlow):
240-
241-
def __init__(self, graph: Graph, source: int, target: int):
242-
super().__init__(graph, source, target)
243-
max_capacity = max(e.capacity for e in graph.get_edges())
244-
self.delta = 2 ** math.floor(math.log(max_capacity, 2))
245-
246-
def step(self):
247-
if result := bfs_capacity(self.graph, self.source, self.target, self.delta):
248-
parent, *_ = result
249-
path_flow = math.inf
250-
251-
tmp = self.target
252-
path = deque()
253-
while tmp != self.source:
254-
path_flow = min(path_flow, parent[tmp].residual_capacity())
255-
path.appendleft(parent[tmp])
256-
tmp = parent[tmp].start
257-
258-
tmp = self.target
259-
while tmp != self.source:
260-
parent[tmp].adjust(path_flow)
261-
tmp = parent[tmp].start
262-
263-
return list(path)
264-
else:
265-
if self.delta > 1:
266-
self.delta /= 2
267-
return self.step()
268-
269-
270-
class Dinic(MaxFlow):
271-
272-
def __init__(self, graph: Graph, source: int, target: int):
273-
super().__init__(graph, source, target)
274-
275-
def step(self):
276-
if result := bfs(self.graph, self.source, self.target):
277-
_, level = result
278-
start = [0] * (self.graph.number_of_nodes() + 1)
279-
edges = []
280-
while flow := self.blocking_flow(self.source, math.inf, start, level, edges):
281-
pass
282-
return edges, level
283-
284-
def blocking_flow(self, u: int, flow: int, start: list[int], level: list[int], edges: list):
285-
# dfs in acyclic layer graph
286-
287-
if u == self.target:
288-
return flow
289-
290-
while start[u] < self.graph.get_degree(u):
291-
edge = self.graph.get_edges_by_node(u)[start[u]] # edge.start == u
292-
if level[edge.end] == level[edge.start] + 1 and edge.residual_capacity() > 0:
293-
curr_flow = min(flow, edge.residual_capacity())
294-
curr_flow = self.blocking_flow(edge.end, curr_flow, start, level, edges)
295-
296-
if curr_flow and curr_flow > 0:
297-
edge.adjust(curr_flow)
298-
edges.append(edge)
299-
return curr_flow
300-
start[u] += 1
301-
302-
303-
class GoldbergTarjan(MaxFlow): # TODO
304-
305-
def __init__(self, graph: Graph, source: int, target: int):
306-
super().__init__(graph, source, target)
307-
self.excess = [0] * graph.number_of_nodes()
308-
self.label = [0] * graph.number_of_nodes()
309-
self.active = deque()
310-
self.preflow_executed = False
311-
312-
def preflow(self):
313-
self.preflow_executed = True
314-
self.label[self.source] = self.graph.number_of_nodes()
315-
for edge in self.graph.get_edges_by_node(self.source):
316-
if not edge.reverse_edge:
317-
edge.flow = edge.capacity
318-
self.excess[edge.end] += edge.flow
319-
self.active.append(edge.end)
320-
321-
def push(self, u):
322-
for edge in self.graph.get_edges_by_node(u):
323-
if edge.reverse_edge:
324-
continue
325-
if edge.flow == edge.capacity:
326-
continue
327-
328-
v = edge.end
329-
if self.label[u] > self.label[v]:
330-
flow = min(edge.residual_capacity(), self.excess[u])
331-
self.excess[u] -= flow
332-
self.excess[v] += flow
333-
edge.adjust(flow)
334-
335-
def relabel(self, u):
336-
self.label[u] = 1 + min(self.label[edge.v]
337-
for edge in self.graph.get_edges_by_node(u)
338-
if edge.residual_capacity() > 0)
339-
340-
def step(self):
341-
if not self.preflow_executed:
342-
return self.preflow()
343-
344-
if self.active:
345-
u = self.active.popleft()
346-
if any(self.excess[edge.end] > 0 for edge in self.graph.get_edges_by_node(u)):
347-
return self.push(u)
348-
else:
349-
return self.relabel(u)
350-
351-
if u not in (s, t) and self.excess[u] > 0:
352-
self.active.append(u)
353-
"""

random_graph.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,4 @@ def generate(n: int, max_capacity: int) -> tuple[int, int, Graph]:
4444
if edge.end == target and not edge.reverse:
4545
edge.capacity = (degree_target + 1) * max_capacity
4646

47-
# TODO
48-
"""
49-
while True:
50-
parent, dist = max_flow.bfs(graph, source, target)
51-
if dist[target] <= 3:
52-
pass # add nodes between source and target
53-
else:
54-
break
55-
"""
56-
5747
return source, target, graph

0 commit comments

Comments
 (0)