{"record":{"id":"83e776b8b76c63b6","repo":"TheAlgorithms/Python","slug":"you-need-to-set-maximum-flow-algorithm-before","errorCode":null,"errorMessage":"You need to set maximum flow algorithm before.","messagePattern":"You need to set maximum flow algorithm before\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"graphs/edmonds_karp_multiple_source_and_sink.py","lineNumber":49,"sourceCode":"            size = len(self.graph) + 1\n            for room in self.graph:\n                room.insert(0, 0)\n            self.graph.insert(0, [0] * size)\n            for i in sources:\n                self.graph[0][i + 1] = max_input_flow\n            self.source_index = 0\n\n            size = len(self.graph) + 1\n            for room in self.graph:\n                room.append(0)\n            self.graph.append([0] * size)\n            for i in sinks:\n                self.graph[i + 1][size - 1] = max_input_flow\n            self.sink_index = size - 1\n\n    def find_maximum_flow(self):\n        if self.maximum_flow_algorithm is None:\n            raise Exception(\"You need to set maximum flow algorithm before.\")\n        if self.source_index is None or self.sink_index is None:\n            return 0\n\n        self.maximum_flow_algorithm.execute()\n        return self.maximum_flow_algorithm.getMaximumFlow()\n\n    def set_maximum_flow_algorithm(self, algorithm):\n        self.maximum_flow_algorithm = algorithm(self)\n\n\nclass FlowNetworkAlgorithmExecutor:\n    def __init__(self, flow_network):\n        self.flow_network = flow_network\n        self.verticies_count = flow_network.verticesCount\n        self.source_index = flow_network.sourceIndex\n        self.sink_index = flow_network.sinkIndex\n        # it's just a reference, so you shouldn't change\n        # it in your algorithms, use deep copy before doing that","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/edmonds_karp_multiple_source_and_sink.py#L31-L67","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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).","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"],"exampleFix":"# before\nnetwork = FlowNetwork(graph)\nnetwork.set_source_flow(list_of_sources, max_flow)\nnetwork.find_maximum_flow()  # raises\n\n# after\nnetwork = FlowNetwork(graph)\nnetwork.set_source_flow(list_of_sources, max_flow)\nnetwork.set_maximum_flow_algorithm(PushRelabelExecutor)\nnetwork.find_maximum_flow()","handlingStrategy":"validation","validationCode":"assert network.maximum_flow_algorithm is not None or False, \"call set_maximum_flow_algorithm first\"\nnetwork.set_maximum_flow_algorithm(PushRelabelExecutor)  # class, not instance\nnetwork.find_maximum_flow()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["graphs","max-flow","initialization","strategy-pattern"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}