I have here a function in a graph coloring algorithm which assigns color to a number of courses. But it chooses randomly, and I'd like to improve the algorithm and assign color more efficiently than picking one at random based on the available colors.
In this function, N is the number of time schedules.
void create_schedule(int **graph, list *courses, int numcourses){
int i, j, k, col=0;
for(i=0; i<numcourses; i++){
for(j=0; j<N; j++){
for(k=0; k<i; k++){
if(graph[i][k] > 0 && courses[k].color == j) break;
}
//color j was not yet chosen by adjacent nodes
if(k >= i){
courses[i].color = j;
break;
}
}
//if there is no color available, choose randomly
if(j >= N){
col = rand()%N;
courses[i].color = col;
}
}
}
An explanation would be great.
N? You might not "care for time" but for lage enough N (say, N>40) - you are unlikely to ever be able to find an optimal solution unless you own a very powerful cluster.