Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max flow in bipartite graph using Ford Fulkerson to determine values to suffice to sum

Tags:

I'm trying to figure how I should use the Ford Fulkerson algorithm in this situation The situation is kinda sudoku-like. We have a matrix a which contains integer values. The last column of each row and last row of each column, contains the sum of the entire row / column.

Example:

int[][] a = {{1, 3, 5, 9},
             {4, 2, 1, 7},
             {5, 5, 6, *}} // * Is not determined since the sums 
                           // * do not count as summable values.

This thing is, that the values within the matrix are not always correct. The values for the sum are not always correct, for example:

int[][] a = {{1, 3, 3, 9},
             {2, 3, 1, 7},
             {5, 5, 6, *}} // * Is not determined since the sums do 
                           // * not count as summable values.

There is a matrix b which contains the max difference a cell can have to meet the given sum. For example

int[][] b = {{1, 0, 3},
             {2, 1, 2}}

For example for the value of a[0][0], which is 1, the max differences is the value at b[0][0], which is 1, so the value at a[0][0] can be changed to 0 or 2 maximum (and all the numbers in between, but for this example we only have 2 options).

My question is: Given a matrix a (with invalid values for a given sum) and a matrix b with the max differences which can be used to meet the required sum, how can I determine wether it's even possible with the given maximum differences and how do I get a valid result of such a matrix (if thus exists).

My current approach (which not works):

  • Make a bipartite graph of the rows and columns, so you have a source, a sink and a node for each row and column.
  • Then connect each row to each column.
  • Then connect the source to each row.
  • Then connect each column to the sink.
  • Set capacities for the edges from the source to each row-node to the Math.Abs(current sum of numbers in a - given sum of numbers in a (for that row)).
  • Same for the edges from each column-node to the sink (but for the column sums this time).
  • Set the capacities between nodes for the rows to the columns to the given max differences in b for each cell accordingly.
  • Use Ford Fulkerson to determine the max flow.

I don't know how I should use my results of the algorithm to determine the correct values for matrix a to meet the given sum for each row and column.

like image 350
Robinhopok Avatar asked Apr 06 '16 13:04

Robinhopok


People also ask

Can we use Ford Fulkerson method to solve maximum bipartite matching?

Maximum bipartite matching solutionSuch a problem can be solved very effectively by the Ford Fulkerson algorithm, which connects and disconnects all possible edges in the graph till the maximum match number is found, such as the highest edge count.


1 Answers

So I found the solution to this problem myself:

If you have values matrix A[i][j] and differences matrix B[i][j], you'd have to substract A - B for each I, j. Then you have to create a bipartite graph by using the rows as the left nodes and the columns as the right nodes.

You then have to connect each of the row nodes to the column nodes and vice versa. Also connect the source to all the row nodes and connect all the column nodes to the sink.

The capacity for each edge from source to row node is the current sum of numbers and the same goes for the edge capaties of the column nodes to sink.

Each capacity between row-node and column-node is the current B[i][j] * 2. Then you should have a complete network.

Use Ford Fulkerson with Edmonds Karp. The flow between each row-node and column-node is the number that should be added up to the current A[i][j].

Your resulting A matrix will be your answer.

like image 86
Robinhopok Avatar answered Sep 28 '22 04:09

Robinhopok