TheAlgorithms/Python · error · Exception
You need to set maximum flow algorithm before.
Error message
You need to set maximum flow algorithm before.
What it means
Raised by FlowNetwork.find_maximum_flow (graphs/edmonds_karp_multiple_source_and_sink.py:49) when find_maximum_flow is called before set_maximum_flow_algorithm. The network object only stores flow data; the actual max-flow computation is delegated to a strategy object (e.g. PushRelabelExecutor) that must be injected first, otherwise maximum_flow_algorithm is None.
Source
Thrown at graphs/edmonds_karp_multiple_source_and_sink.py:49
size = len(self.graph) + 1
for room in self.graph:
room.insert(0, 0)
self.graph.insert(0, [0] * size)
for i in sources:
self.graph[0][i + 1] = max_input_flow
self.source_index = 0
size = len(self.graph) + 1
for room in self.graph:
room.append(0)
self.graph.append([0] * size)
for i in sinks:
self.graph[i + 1][size - 1] = max_input_flow
self.sink_index = size - 1
def find_maximum_flow(self):
if self.maximum_flow_algorithm is None:
raise Exception("You need to set maximum flow algorithm before.")
if self.source_index is None or self.sink_index is None:
return 0
self.maximum_flow_algorithm.execute()
return self.maximum_flow_algorithm.getMaximumFlow()
def set_maximum_flow_algorithm(self, algorithm):
self.maximum_flow_algorithm = algorithm(self)
class FlowNetworkAlgorithmExecutor:
def __init__(self, flow_network):
self.flow_network = flow_network
self.verticies_count = flow_network.verticesCount
self.source_index = flow_network.sourceIndex
self.sink_index = flow_network.sinkIndex
# it's just a reference, so you shouldn't change
# it in your algorithms, use deep copy before doing thatView on GitHub (pinned to f5988cc097)
Solutions
- Register an executor before computing: network.set_maximum_flow_algorithm(PushRelabelExecutor), then network.find_maximum_flow()
- Pass the executor class, not an instance — the method instantiates it as algorithm(self)
- Order your setup code: build network -> set algorithm -> find flow
Example fix
# before network = FlowNetwork(graph) network.set_source_flow(list_of_sources, max_flow) network.find_maximum_flow() # raises # after network = FlowNetwork(graph) network.set_source_flow(list_of_sources, max_flow) network.set_maximum_flow_algorithm(PushRelabelExecutor) network.find_maximum_flow()
Defensive patterns
Strategy: validation
Validate before calling
assert network.maximum_flow_algorithm is not None or False, "call set_maximum_flow_algorithm first" network.set_maximum_flow_algorithm(PushRelabelExecutor) # class, not instance network.find_maximum_flow()
Prevention
- Always pair construction with set_maximum_flow_algorithm immediately
- Pass the executor class; the network instantiates it with itself
- Encapsulate the build->configure->solve sequence in one function so steps cannot be skipped
When it happens
Trigger: `network = FlowNetwork(graph); network.find_maximum_flow()` without ever calling set_maximum_flow_algorithm. Also assigning a plain function instead of an executor class: set_maximum_flow_algorithm expects a callable/class that takes the network as its single constructor argument.
Common situations: Copy-paste examples that skip the setup step; refactoring where the set_maximum_flow_algorithm call was dropped; passing an instance instead of a class (set_maximum_flow_algorithm(algorithm) calls algorithm(self), so it wants the class, not an instance).
Related errors
- You should execute algorithm before using its result!
- Algorithm is unable to find solution
- Negative cycle found
- No path from vertex: {self.source_vertex} to vertex: {target
- Vertex indexes must be in [0; size).
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/83e776b8b76c63b6.
Report an issue: GitHub.