{"record":{"id":"b87b8124118ffa08","repo":"TheAlgorithms/Python","slug":"you-should-execute-algorithm-before-using-its-resu","errorCode":null,"errorMessage":"You should execute algorithm before using its result!","messagePattern":"You should execute algorithm before using its result!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"graphs/edmonds_karp_multiple_source_and_sink.py","lineNumber":89,"sourceCode":"    def execute(self):\n        if not self.executed:\n            self._algorithm()\n            self.executed = True\n\n    # You should override it\n    def _algorithm(self):\n        pass\n\n\nclass MaximumFlowAlgorithmExecutor(FlowNetworkAlgorithmExecutor):\n    def __init__(self, flow_network):\n        super().__init__(flow_network)\n        # use this to save your result\n        self.maximum_flow = -1\n\n    def get_maximum_flow(self):\n        if not self.executed:\n            raise Exception(\"You should execute algorithm before using its result!\")\n\n        return self.maximum_flow\n\n\nclass PushRelabelExecutor(MaximumFlowAlgorithmExecutor):\n    def __init__(self, flow_network):\n        super().__init__(flow_network)\n\n        self.preflow = [[0] * self.verticies_count for i in range(self.verticies_count)]\n\n        self.heights = [0] * self.verticies_count\n        self.excesses = [0] * self.verticies_count\n\n    def _algorithm(self):\n        self.heights[self.source_index] = self.verticies_count\n\n        # push some substance to graph\n        for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index]):","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/edmonds_karp_multiple_source_and_sink.py#L71-L107","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call algorithm.execute() before algorithm.get_maximum_flow()","Prefer the network-level API: network.set_maximum_flow_algorithm(ExecutorClass); network.find_maximum_flow() — it sequences execute/result correctly","If execute() can raise, guard the result read so it only happens on success"],"exampleFix":"# before\nalgorithm = PushRelabelExecutor(network)\nresult = algorithm.get_maximum_flow()  # raises\n\n# after\nalgorithm = PushRelabelExecutor(network)\nalgorithm.execute()\nresult = algorithm.get_maximum_flow()","handlingStrategy":"validation","validationCode":"algorithm = PushRelabelExecutor(network)\nalgorithm.execute()\nif not algorithm.executed:\n    raise RuntimeError(\"executor did not run\")\nflow = algorithm.get_maximum_flow()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer network.find_maximum_flow(), which orders execute/result correctly","Never read results in an except/finally path after execute() failed"],"tags":["graphs","max-flow","initialization","ordering"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}