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):
a
- given sum of numbers in a
(for that row)).b
for each cell accordingly.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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With