# Need algorithm for generation of all variations of a 4x4 boolean table.

• September 25th, 2012, 04:39 PM
LittleGreenMidget
Need algorithm for generation of all variations of a 4x4 boolean table.
Hello everyone, I am currently try to solve a problem which requires the generation of all 2^16 tables and I'm afraid I have no clue how to do so efficiently. Could anyone please point me to an algorithm which could be utilized for such a task?

Though I have no bugs at the moment I'll include the code so far anyway.

Code :

```public class logicp1 { public static boolean[][][] tableGenerator( boolean[][] likeTable ) {   public static boolean checkRules(boolean[][] likeTable) { boolean isTrue = false;   // Rule 1 Dana Likes Cody in terms of table values it is necessary that likeTable[3][0] is always true. Thus : System.out.println(" Passed rule 1 "); if ( likeTable[3][0] == false ) return false; // Rule 2 Bess does not Like Dana. Therfore likeTable[1][3] is always false. Thus : if ( likeTable[1][3] == true) return false; System.out.println(" Passed rule 2 "); // Rule 3 Cody does not like Abby, therefore likeTable[2][0] is always False if ( likeTable[2][0] == true ) return false; System.out.println(" Passed rule 3 "); // Rule 4 Nobody likes someone who does not like her. This translates to an if statement. // if likeTable[x][y] == true then likeTable[y][x] must be true for ( int i = 0; i < 4; i++) { for ( int c = 0; c < 4; c++) { if (likeTable[i][c] != likeTable[c][i]) return false; } } System.out.println(" Passed rule 4 "); // Rule 5 Dana like everyone who likes Bess . This means that if [x][1] == true, then likeTable[3][x] == true for ( int i = 0; i < 4; i++){ if ( likeTable[i][1] == true && likeTable[3][i] == false ) return false; } System.out.println(" Passed rule 5 "); // Rule 6 Dana likes everyone Bess likes. This can be ensured through a for loop which takes the values of Bess and // replaced the values of Dana with them. e.g for i < 3; likeTable[3][i] gets likeTable[1][i] This also means that the dana array does not need to be manipulated during generation. for ( int i = 0; i < 4; i++){ if (likeTable[1][i] == true && likeTable[3][i] == false ) return false; } System.out.println(" Passed rule 6 "); // Rule 7 Everybody likes somebody Meaning that for any likeTable[i][0-3] one of htese values must be true. for ( int i = 0; i < 4; i++) { for ( int c = 0; c < 4; c++) { if (likeTable[i][c] == true) isTrue = true; } if(!isTrue) return false; } System.out.println(" Passed rule 7 "); return true; }   public static void printTable(boolean[][] likeTable) { String[] lines = new String[4]; for ( int i = 0; i < 4; i++){ for ( int c = 0; c < 4; c++){ if ( likeTable[i][c] == true) lines[i] += " [x] "; else{ lines[i] += " [ ] "; } } }   for ( int i = 0; i <4;i++) { System.out.println(lines[i]);} }         public static void main (String[] args){ boolean[][] likeTable = new boolean[4][4]; // boolean table created, true = like, false = dislike /* Abby Bess Cody Dana * Abby * Bess * Cody * Dana /* Algorithm : I will load the rules of the world into the program and store them as constant rules. I will then generate variations of the truth table and test against the states * below is the initialization of each rule */   System.out.println(likeTable[2][0]); likeTable[0][0] = true; likeTable[1][0] = true; likeTable[0][1] = true; likeTable[0][3] = true; likeTable[2][3] = true; likeTable[3][0] = true; likeTable[3][2] = true; printTable(likeTable); System.out.println(checkRules(likeTable)); printTable(likeTable); // Rule 7 Everybody likes somebody. Meaning that for any likeTable[i][0-3] one of htese values must be true. } } // The algorithm will complete its task by going through each rule and fulfilling it by altering the values of the Table. It will however ruleCheck after each alteration to ensure that the world's adherence ot the rules is maintained.```
• September 25th, 2012, 05:22 PM
newbie
Re: Need algorithm for generation of all variations of a 4x4 boolean table.
D...D.. Double POST!