I'm doing again a work of optimal Graph Coloring, so, I need to generate all possible color combinations (the array represents the color for each node) for a graph. I got a lot of help here, as you can see in this question:
Generate all possible array combinations in C - Optimal Graph Colouring
For now, my code is:
void generatearray( int array[], int array_size, int idx = 0, int fixed = 0 )
{
int i;
if ( idx == array_size )
{
putchar('\n');
for( i = 0; i < array_size; i++ ) printf( "%i ", array[i] );
} else {
for( i = 0; i <= 3; i++ )
{
if ( fixed == i )
{
fixed++;
array[idx] = i;
return generatearray( array, array_size, idx + 1, fixed );
}
array[idx] = i;
generatearray( array, array_size, idx + 1, fixed );
}
}
}
int arr[3];
generatearray( arr, 3 );
The output, in this case, would be:
0 0 0
0 0 1
0 1 0
0 1 1
0 1 2
If 0 means blue and 2 means red, in Graph coloring, red-red-red is the same thing of blue-blue-blue. That's what my code do: it generates all possible different color combinations for a graph.
Now I need to improve my code, but I couldn't think of anything. I want it to generate arrays only with a given number of colors, because I'm using pthreads and I want each thread to deal with the graph with a number of colors.
For example: with 2 colors, the output would be:
0 0 1
0 1 0
0 1 1
And with 3 colors:
1 2 3
I don't need it to create arrays with less colors than the number set, as there's another threads doing this.
Could any of you guys help me with my code? Sorry if I didn't made myself clear at any point.