Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to find all possible binary combinations with a condition

Tags:

c#

algorithm

Here is one is for you math brains out there. I have a matrix, actually its half a matrix, cut diagonally. Each element of the matrix can be a 1 or a 0. I need to find all the possible combinations of 1s and 0s for any matrix of width N.

This is easy enough, you can get the number of elements on this matrix given width N with formula for this example where N=7 this would give us 28 or the number of elements. Then you can get the combinations with formula.

So the formula would be formula to get all the possible combinations.

empty

Now here is where it gets tricky. There is one condition that must hold true for each result. The sum of the each set of elements on the matrix (shown below with each row represented) must be less than 4 for the first set (the one on the first row), less than 3 for all the other sets (these are constants regardless of the N value).

enter image description here

Here are what the sets for this example (N=7) look like. If you notice each row is represented. So for the first set if the combination is 0 1 0 1 0 1 0 this would be valid as its sum is < 4 (since its the first row). For the second set if the combination is 1 0 0 0 0 1 0 it is valid as it needs to be < 3.

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

I need to do this for huge matrices so brute forcing all possible permutations to find the ones that fall under this condition would be unfeasable. I need to find some sort of algorithm I can use to generate the valid matrices bottom up rather than top down. Maybe doing separate operations that can be composed later to yield a total set of results.

Any and all ideas are welcome.

like image 749
Marcelo Mason Avatar asked Sep 16 '15 03:09

Marcelo Mason


People also ask

How do you find all possible combinations?

To calculate combinations, we will use the formula nCr = n! / r! * (n - r)!, where n represents the total number of items, and r represents the number of items being chosen at a time. To calculate a combination, you will need to calculate a factorial.

How many combinations of 3 binary variables are there?

3^2 = 9 ways to choose binary operators (3 operators, two places to put them, each chosen freely)

How many combinations of 5 binary numbers are there?

The Five-bit Code Using these two states in a group of five-bits to represent each character gives a total of 32 unique combinations. You can create a list of these 32 five-bit combinations by starting with the '00000' combination first and then counting in binary until you reach the combination '11111'.

How many 8 digit binary combinations are there?

With 8 bits, or 8 binary digits, there exist 2^8=256 possible combinations. The following table shows some of these combinations. (The number enclosed in parentheses represents the decimal equivalent.)


1 Answers

A simple algorithm generating each solution recursively :

global File //A file where you will store your data
global N    //Your matrix size

//matrix contains the matrix we build (int[][])
//set contains the number of 1 we can use on a set (int[])
//c is the column number (int)
//r is the row number (int)
function f ( matrix, set, c, r ) :
  if ( c == N ):
    r = r + 1
    c = r

  if ( r == N ):
    write ( matrix in File )
    // Implement your own way of storing the matrix

  if ( set[r] > 0 AND (c+2 < N AND set[c+2] > 0) ):
    matrix[c][r] = 1
    set[c]--
    set[r]--
    f ( matrix, set, c+1, r )

  matrix[c][r] = 0
  f ( matrix, set, c+1, r)
end

//Calling our function with N = 5
N = 5
f([[0,0,0,0,0],[0,0,0,0,0],...], [3,2,2,2,2], 0, 0)

You can store each matrix in something else than a file but keep an eye on your memory consumption.

like image 54
SkyDream Avatar answered Nov 04 '22 11:11

SkyDream