Find maximum number of edge disjoint paths between two vertices | C # program to find maximum number of edge disjoint paths ; Number of vertices in given graph ; Returns true if there is a path from source ' s ' to sink ' t ' in residual graph . Also fills parent [ ] to store the path ; Create a visited array and mark all vertices as not visited ; Create a queue , enqueue source vertex and mark source vertex as visited ; Standard BFS Loop ; If we reached sink in BFS starting from source , then return true , else false ; Returns tne maximum number of edge - disjoint paths from s to t . This function is copy of goo . gl / wtQ4Ks forFulkerson ( ) discussed at http : ; Create a residual graph and fill the residual graph with given capacities in the original graph as residual capacities in residual graph Residual graph where rGraph [ i , j ] indicates residual capacity of edge from i to j ( if there is an edge . If rGraph [ i , j ] is 0 , then there is not ) ; This array is filled by BFS and to store path ; There is no flow initially ; Augment the flow while there is path from source to sink ; Find minimum residual capacity of the edges along the path filled by BFS . Or we can say find the maximum flow through the path found . ; update residual capacities of the edges and reverse edges along the path ; Add path flow to overall flow ; Return the overall flow ( max_flow is equal to maximum number of edge - disjoint paths ) ; Driver Code ; Let us create a graph shown in the above example
using System ; using System . Collections . Generic ; class GFG { static int V = 8 ; static bool bfs ( int [ , ] rGraph , int s , int t , int [ ] parent ) { bool [ ] visited = new bool [ V ] ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( s ) ; visited [ s ] = true ; parent [ s ] = - 1 ; while ( q . Count != 0 ) { int u = q . Peek ( ) ; q . Dequeue ( ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( visited [ v ] == false && rGraph [ u , v ] > 0 ) { q . Enqueue ( v ) ; parent [ v ] = u ; visited [ v ] = true ; } } } return ( visited [ t ] == true ) ; } static int findDisjointPaths ( int [ , ] graph , int s , int t ) { int u , v ; int [ , ] rGraph = new int [ V , V ] ; for ( u = 0 ; u < V ; u ++ ) for ( v = 0 ; v < V ; v ++ ) rGraph [ u , v ] = graph [ u , v ] ; int [ ] parent = new int [ V ] ; int max_flow = 0 ; while ( bfs ( rGraph , s , t , parent ) ) { int path_flow = int . MaxValue ; for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; path_flow = Math . Min ( path_flow , rGraph [ u , v ] ) ; } for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; rGraph [ u , v ] -= path_flow ; rGraph [ v , u ] += path_flow ; } max_flow += path_flow ; } return max_flow ; } public static void Main ( String [ ] args ) { int [ , ] graph = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int s = 0 ; int t = 7 ; Console . WriteLine ( " There ▁ can ▁ be ▁ maximum ▁ " + findDisjointPaths ( graph , s , t ) + " ▁ edge - disjoint ▁ paths ▁ from ▁ " + s + " ▁ to ▁ " + t ) ; } }