combinatorics - Generate all possible array combinations in C - Optimal Graph Colouring -


i need generate arrays possible combinations, question i've found here:

combinatorics: generate "states" - array combinations

i'm doing simple work of optimal graph coloring, so, i'm trying generate possible color combinations (the array represents color each node). code working but, doing unnecessary work. in situation, [1, 1, 2] same thing of [2, 2, 1], don't need test if valid graph again.

i can't think of anything, first know if there's simple code doing want to.

for now, code this:

void generatearray(int array[], int array_size, int idx){      int i;      if(idx == array_size){         putchar('\n');         for(i = 0; < array_size; i++) printf("%i ", array[i]);      }     else for(i = 0; <= 3; i++){         array[idx] = i;         generatearray(array, array_size, idx+1);     }  } 

and print:

    [0, 0, 0]     [0, 0, 1]     [0, 0, 2]     [0, 0, 3]     [0, 1, 0]     [0, 1, 1]     ...     [3, 3, 0]     [3, 3, 1]     [3, 3, 2]     [3, 3, 3] 

try this:

void generatearray( int array[], int array_size, int idx = 0, int fixed = 0 ) {    int i;     if ( idx == array_size )    {        putchar('\n');        for( = 0; < array_size; i++ ) printf( "%i ", array[i] );     } else {         for( = 0; <= 3; i++ )        {           if ( fixed == )           {              fixed++;              array[idx] = i;              return generatearray( array, array_size, idx + 1, fixed );           }           array[idx] = i;           generatearray( array, array_size, idx + 1, fixed );        }    } }  int arr[6]; generatearray( arr, 6 ); 

old broken answer:

void generatearray(int array[], int array_size, int idx){     int i;     if(idx == array_size){        putchar('\n');        for(i = 0; < array_size; i++) printf("%i ", array[i]);     }    else if ( idx == 0 ) {        array[idx] = 0;        generatearray(array, array_size, idx+1);    } else {        for(i = 0; <= 3; i++){        array[idx] = i;        generatearray(array, array_size, idx+1);        }     } } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -