1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
bool canColor(int &v, vector<bool> &graph, int colors[], int &j, int &vertex_count)
{
for (int i = 0; i < vertex_count; i++)
if (
//graph[v][i] && j == colors[i]
graph[v*vertex_count+i] && j == colors[i])
return false;
return true;
}
void recursiveColor(vector<bool> &graph, int &used_colors, int colors[], int v, int &vertex_count)
{
if(v == vertex_count)
return;
for(int j = 1; j <= used_colors; j++)
{
if(canColor(v, graph, colors, j, vertex_count))
{
colors[v] = j;
recursiveColor(graph, used_colors, colors, v+1, vertex_count);
}
}
}
void graphColor(vector<bool> graph, int used_colors, int vertex_count)
{
clock_t start;
double duration;
start = clock();
int v = 0;
int colors[vertex_count];
for(int i = 0; i < vertex_count; i++)
{
colors[i] = 0;
}
recursiveColor(graph, used_colors, colors, v, vertex_count);
for (int i = 0; i < vertex_count; i++)
cout<< "Vertex " << i+1 << " Color: "<< colors[i]<<endl;
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
cout<<"time taken: "<< duration <<endl;
}
|