Daa Exp7
Daa Exp7
Experiment No. 8
ALGORITHM:
The objective is to assign colors to vertices of a graph in such a way
that no two adjacent vertices share the same color. Using a
backtracking algorithm to solve the graph coloring problem involves
systematically exploring potential solutions while backtracking from
partial solutions that cannot be completed to find a complete and
valid solution.
PSEUDOCODE
If index == number.of.vertices
return TRUE.
Else
for every k= 1 to M :
assign color to the current vertex, v, (color[v]=k)
if colour(graph,vertex+1,color)==TRUE, return true
else ,
mark the colour as unassigned, (colour[v]=0) (backtracking step).
If none of the combination satisfies
return FALSE.
TIME COMPLEXITY:
FILE* file;
void printSolution(int n, int color[]) {
fprintf(file,"Solution exists:");
for (int i = 0; i < n; i++)
fprintf(file," C%d ", color[i]);
fprintf(file,"\n");
}
if (color[v] == 0) {
colorCounter--;
if (colorCounter == 0) {
fprintf(file,"Solution does not exist\n");
return false;
}
}
}
return false;
}
printSolution(n, color);
return true;
}
int main() {
file = fopen("exp8output.txt","w");
clock_t start,end;
double avg_ex_time = 0;
//Problem 1
fprintf(file,"Problem 1:\n");
int n = 4;
bool graph1[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},
};
int m = 3;
printMatrix(graph1,n);
fprintf(file,"Backtracking approach:\n");
start = clock();
graphColoring(n, graph1, m);
end = clock();
avg_ex_time = ((double)(end - start)) /
(CLOCKS_PER_SEC);
fprintf(file,"Time Taken: %lf\n", avg_ex_time);
fprintf(file,"\nNaive Approach:\n");
start = clock();
graphColoringNaive(n,graph1,m);
end = clock();
avg_ex_time = ((double)(end - start)) / (CLOCKS_PER_SEC);
fprintf(file,"Time taken: %lf\n", avg_ex_time);
fprintf(file,"-----------------------------------------------
------\n");
//Problem 2
fprintf(file,"Problem 2:\n");
n=3;
bool graph2[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1},
{1, 0, 1},
{1, 1, 0}
};
m = 2;
printMatrix(graph2,n);
fprintf(file,"Backtracking Approach:\n");
start = clock();
graphColoring(n, graph2, m);
end = clock();
avg_ex_time = ((double)(end - start)) /
(CLOCKS_PER_SEC);
fprintf(file,"Time Taken: %lf\n", avg_ex_time);
fprintf(file,"\nNaive Approach:\n");
start = clock();
graphColoringNaive(n,graph2,m);
end = clock();
avg_ex_time = ((double)(end - start)) / (CLOCKS_PER_SEC);
fprintf(file,"Time taken: %lf\n", avg_ex_time);
fprintf(file,"-----------------------------------------------
------\n");
//Problem 3
fprintf(file,"Problem 3:\n");
n = 7;
bool graph3[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 0},
{0, 1, 1, 0, 1, 1, 0},
{0, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 1, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 0}
};
m = 3;
printMatrix(graph3,n);
fprintf(file,"Backtracking approach:\n");
start = clock();
graphColoring(n, graph3, m);
end = clock();
avg_ex_time = ((double)(end - start)) /
(CLOCKS_PER_SEC);
fprintf(file,"Time Taken: %lf\n", avg_ex_time);
fprintf(file,"\nNaive Approach:\n");
start = clock();
graphColoringNaive(n,graph3,m);
end = clock();
avg_ex_time = ((double)(end - start)) / (CLOCKS_PER_SEC);
fprintf(file,"Time taken: %lf\n", avg_ex_time);
fprintf(file,"-----------------------------------------------
-----\n");
//Problem 4
fprintf(file,"Problem 4:\n");
n = 10;
bool graph4[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
{0, 0, 1, 0, 1, 0, 1, 1, 1, 0},
{0, 0, 0, 1, 1, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0}
};
m = 3;
printMatrix(graph4,n);
fprintf(file,"Backtracking approach:\n");
start = clock();
graphColoring(n, graph4, m);
end = clock();
avg_ex_time = ((double)(end - start)) /
(CLOCKS_PER_SEC);
fprintf(file,"Time Taken: %lf\n", avg_ex_time);
fprintf(file,"\nNaive Approach:\n");
start = clock();
graphColoringNaive(n,graph4,m);
end = clock();
avg_ex_time = ((double)(end - start)) / (CLOCKS_PER_SEC);
fprintf(file,"Time taken: %lf\n", avg_ex_time);
fprintf(file,"-----------------------------------------------
------\n");
//Problem 5
fprintf(file,"Problem 5:\n");
n = 8;
bool graph5[MAX_VERTICES][MAX_VERTICES] = {
{0, 1, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 1, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 0, 1, 0, 1, 1},
{0, 0, 0, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 1, 1, 0}
};
m = 4;
printMatrix(graph5,n);
fprintf(file,"Backtracking approach:\n");
start = clock();
graphColoring(n, graph5, m);
end = clock();
avg_ex_time = ((double)(end - start)) /
(CLOCKS_PER_SEC);
fprintf(file,"Time Taken: %lf\n", avg_ex_time);
fprintf(file,"\nNaive Approach:\n");
start = clock();
graphColoringNaive(n,graph5,m);
end = clock();
avg_ex_time = ((double)(end - start)) / (CLOCKS_PER_SEC);
fprintf(file,"Time taken: %lf\n", avg_ex_time);
fprintf(file,"-----------------------------------------------
------\n");
return 0;
}
RESULT: