TheAlgorithms/Python · error · Exception

You should execute algorithm before using its result!

Error message

You should execute algorithm before using its result!

What it means

Raised by MaximumFlowAlgorithmExecutor.get_maximum_flow (graphs/edmonds_karp_multiple_source_and_sink.py:89) when the result is requested before execute() has run. The executor sets self.executed only inside execute(); before that, maximum_flow still holds the sentinel -1, so reading it would silently return a wrong value — the guard converts that into a loud failure.

Source

Thrown at graphs/edmonds_karp_multiple_source_and_sink.py:89

    def execute(self):
        if not self.executed:
            self._algorithm()
            self.executed = True

    # You should override it
    def _algorithm(self):
        pass


class MaximumFlowAlgorithmExecutor(FlowNetworkAlgorithmExecutor):
    def __init__(self, flow_network):
        super().__init__(flow_network)
        # use this to save your result
        self.maximum_flow = -1

    def get_maximum_flow(self):
        if not self.executed:
            raise Exception("You should execute algorithm before using its result!")

        return self.maximum_flow


class PushRelabelExecutor(MaximumFlowAlgorithmExecutor):
    def __init__(self, flow_network):
        super().__init__(flow_network)

        self.preflow = [[0] * self.verticies_count for i in range(self.verticies_count)]

        self.heights = [0] * self.verticies_count
        self.excesses = [0] * self.verticies_count

    def _algorithm(self):
        self.heights[self.source_index] = self.verticies_count

        # push some substance to graph
        for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index]):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Call algorithm.execute() before algorithm.get_maximum_flow()
  2. Prefer the network-level API: network.set_maximum_flow_algorithm(ExecutorClass); network.find_maximum_flow() — it sequences execute/result correctly
  3. If execute() can raise, guard the result read so it only happens on success

Example fix

# before
algorithm = PushRelabelExecutor(network)
result = algorithm.get_maximum_flow()  # raises

# after
algorithm = PushRelabelExecutor(network)
algorithm.execute()
result = algorithm.get_maximum_flow()
Defensive patterns

Strategy: validation

Validate before calling

algorithm = PushRelabelExecutor(network)
algorithm.execute()
if not algorithm.executed:
    raise RuntimeError("executor did not run")
flow = algorithm.get_maximum_flow()

Prevention

When it happens

Trigger: Creating an executor (e.g. PushRelabelExecutor(network)) and immediately calling get_maximum_flow(); refactoring that reorders result collection before the execute call; calling getMaximumFlow on a fresh executor after an exception aborted execute() midway.

Common situations: Result-read code moved above the compute call during refactoring; exception handling around execute() that falls through to result reading; misunderstanding that find_maximum_flow on the network calls execute() for you, but direct executor use does not.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/b87b8124118ffa08. Report an issue: GitHub.