Minimum Cost Maximum Flow from a Graph using Bellman Ford Algorithm | Python3 program to implement the above approach ; Stores the found edges ; Stores the number of nodes ; Stores the capacity of each edge ; Stores the cost per unit flow of each edge ; Stores the distance from each node and picked edges for each node ; Function to check if it is possible to have a flow from the src to sink ; Initialise found [ ] to false ; Initialise the dist [ ] to INF ; Distance from the source node ; Iterate untill src reaches N ; If already found ; Evaluate while flow is still in supply ; Obtain the total value ; If dist [ k ] is > minimum value ; Update ; If dist [ k ] is > minimum value ; Update ; Update src to best for next iteration ; Return the value obtained at sink ; Function to obtain the maximum Flow ; If a path exist from src to sink ; Set the default amount ; Return pair total cost and sink ; Driver Code ; Creating an object flow
from sys import maxsize NEW_LINE from typing import List NEW_LINE found = [ ] NEW_LINE N = 0 NEW_LINE cap = [ ] NEW_LINE flow = [ ] NEW_LINE cost = [ ] NEW_LINE dad = [ ] NEW_LINE dist = [ ] NEW_LINE pi = [ ] NEW_LINE INF = maxsize // 2 - 1 NEW_LINE def search ( src : int , sink : int ) -> bool : NEW_LINE INDENT found = [ False for _ in range ( N ) ] NEW_LINE dist = [ INF for _ in range ( N + 1 ) ] NEW_LINE dist [ src ] = 0 NEW_LINE while ( src != N ) : NEW_LINE INDENT best = N NEW_LINE found [ src ] = True NEW_LINE for k in range ( N ) : NEW_LINE INDENT if ( found [ k ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( flow [ k ] [ src ] != 0 ) : NEW_LINE INDENT val = ( dist [ src ] + pi [ src ] - pi [ k ] - cost [ k ] [ src ] ) NEW_LINE if ( dist [ k ] > val ) : NEW_LINE INDENT dist [ k ] = val NEW_LINE dad [ k ] = src NEW_LINE DEDENT DEDENT if ( flow [ src ] [ k ] < cap [ src ] [ k ] ) : NEW_LINE INDENT val = ( dist [ src ] + pi [ src ] - pi [ k ] + cost [ src ] [ k ] ) NEW_LINE if ( dist [ k ] > val ) : NEW_LINE INDENT dist [ k ] = val NEW_LINE dad [ k ] = src NEW_LINE DEDENT DEDENT if ( dist [ k ] < dist [ best ] ) : NEW_LINE INDENT best = k NEW_LINE DEDENT DEDENT src = best NEW_LINE DEDENT for k in range ( N ) : NEW_LINE INDENT pi [ k ] = min ( pi [ k ] + dist [ k ] , INF ) NEW_LINE DEDENT return found [ sink ] NEW_LINE DEDENT def getMaxFlow ( capi : List [ List [ int ] ] , costi : List [ List [ int ] ] , src : int , sink : int ) -> List [ int ] : NEW_LINE INDENT global cap , cost , found , dist , pi , N , flow , dad NEW_LINE cap = capi NEW_LINE cost = costi NEW_LINE N = len ( capi ) NEW_LINE found = [ False for _ in range ( N ) ] NEW_LINE flow = [ [ 0 for _ in range ( N ) ] for _ in range ( N ) ] NEW_LINE dist = [ INF for _ in range ( N + 1 ) ] NEW_LINE dad = [ 0 for _ in range ( N ) ] NEW_LINE pi = [ 0 for _ in range ( N ) ] NEW_LINE totflow = 0 NEW_LINE totcost = 0 NEW_LINE while ( search ( src , sink ) ) : NEW_LINE INDENT amt = INF NEW_LINE x = sink NEW_LINE while x != src : NEW_LINE INDENT amt = min ( amt , flow [ x ] [ dad [ x ] ] if ( flow [ x ] [ dad [ x ] ] != 0 ) else cap [ dad [ x ] ] [ x ] - flow [ dad [ x ] ] [ x ] ) NEW_LINE x = dad [ x ] NEW_LINE DEDENT x = sink NEW_LINE while x != src : NEW_LINE INDENT if ( flow [ x ] [ dad [ x ] ] != 0 ) : NEW_LINE INDENT flow [ x ] [ dad [ x ] ] -= amt NEW_LINE totcost -= amt * cost [ x ] [ dad [ x ] ] NEW_LINE DEDENT else : NEW_LINE INDENT flow [ dad [ x ] ] [ x ] += amt NEW_LINE totcost += amt * cost [ dad [ x ] ] [ x ] NEW_LINE DEDENT x = dad [ x ] NEW_LINE DEDENT totflow += amt NEW_LINE DEDENT return [ totflow , totcost ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = 0 NEW_LINE t = 4 NEW_LINE cap = [ [ 0 , 3 , 1 , 0 , 3 ] , [ 0 , 0 , 2 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 6 ] , [ 0 , 0 , 0 , 0 , 2 ] , [ 0 , 0 , 0 , 0 , 0 ] ] NEW_LINE cost = [ [ 0 , 1 , 0 , 0 , 2 ] , [ 0 , 0 , 0 , 3 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 ] ] NEW_LINE ret = getMaxFlow ( cap , cost , s , t ) NEW_LINE print ( " { } ▁ { } " . format ( ret [ 0 ] , ret [ 1 ] ) ) NEW_LINE DEDENT