Search code examples
ralgorithmmax-flowford-fulkerson

Ford-Fulkerson maximum flow step-by-step calculation?


I am currently studying Ford-Fulkerson algorithm based on this code found in R documentation:

nodes <- 1:6
arcs <- matrix(c(1,2,1, 1,3,7, 2,3,1, 2,4,3, 2,5,2, 3,5,4, 4,5,1, 4,6,6,
                5,6,2), byrow = TRUE, ncol = 3)
# Maximum flow with Ford-Fulkerson algorithm
maxFlowFordFulkerson(nodes, arcs, source.node = 2, sink.node = 6)

& got the following results:

$`s.cut`
[1] 2 3 1 5

$t.cut
[1] 4 6

$max.flow
[1] 6

It is stated in the result that maximum flow of this network is 6.

However, I have been trying to calculate the maximum flow by hand & no matter what I did, the maximum flow I obtained was only 5.

This is the possible graph I was able to map based on the code example: Max flow graph

& some of the possible flows I was able to get:

2 --> 4 --> 5 --> 6....................[Capacity: 1]

2 --> 5 --> 4 --> 6(backflow)...[Capacity: 1]

2 --> 5 --> 6............................[Capacity: 1]

2 --> 4 --> 6............................[Capacity: 2]

[Total capacity: 5]

Or

2 --> 3 --> 5 --> 6....................[Capacity: 1]

2 --> 4 --> 5 --> 6....................[Capacity: 1]

2 --> 5 --> 4 --> 6 (backflow)...[Capacity: 1]

2 --> 4 --> 6.............................[Capacity: 2]

[Total capacity: 5]

Did I misunderstood the process? Can anyone possibly write down the path to get a maximum flow of 6 step-by-step?


Solution

  • I think yours is correct (the max flow should be 5). Maybe you can try igraph for cross-check, e.g.,

    df <- as.data.frame(`colnames<-`(arcs,c("from","to","capacity")))
    g <- graph_from_data_frame(df)
    res <- max_flow(g,2,6)
    

    which gives

    > res
    $value
    [1] 5
    
    $flow
    [1] 0 0 0 3 2 0 0 3 2
    
    $cut
    [1] 4 9
    
    $partition1
    + 4/6 vertices, named, from bfc5f42:
    [1] 1 2 3 5
    
    $partition2
    + 2/6 vertices, named, from bfc5f42:
    [1] 4 6
    
    $stats
    $stats$nopush
    [1] 4
    
    $stats$norelabel
    [1] 3
    
    $stats$nogap
    [1] 2
    
    $stats$nogapnodes
    [1] 2
    
    $stats$nobfs
    [1] 1