-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkflowinstance.py
More file actions
57 lines (49 loc) · 1.73 KB
/
networkflowinstance.py
File metadata and controls
57 lines (49 loc) · 1.73 KB
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
# vim: set expandtab:ts=8:sw=4:softtabstop=4:smarttab
#!/usr/bin/env python
from edgelabeledgraph import EdgeLabeledGraph
from node import Node
class NetworkFlowInstance:
"""
Represent a network flow instance
"""
def __init__(self, elg, source, sink):
self.elg = elg
self.source = source
self.sink = sink
self.flow = {}
def __str__(self):
return "\"instance\" \n %s \n \"source\" \n %s \n \"sink\" \n %s" % (
self.elg, self.source, self.sink)
def find_path(self, source, sink, path):
if source is sink:
return path
for nal in self.get_edges(source):
if self.flow.get(nal) is None:
self.flow[nal] = 0
residual = nal.edgecap.capacity - self.flow[nal]
if residual > 0 and not (nal, residual) in path:
result = self.find_path(nal.sink, sink, path + [(edge, residual)])
if result is not None:
return result
def get_edges(self, node):
"""
; node -> listof(nodeandlabel)
"""
adj_list = self.elg.adjacencies
for adj in adj_list:
if adj.node == node:
return adj.successors
return None
def max_flow(self):
path = self.find_path(self.source, self.sink, [])
while path is not None:
flow = min(res for edge, res in path)
for edge, res in path:
self.flow[edge] += flow
self.flow[edge.redge] -+ flow
path = self.find_path([])
return sum(self.flow[edge] for edge in self.get_edges())
def __repr__(self):
return str(self)
def __name__(self):
return "NetworkFlowInstance"