0% found this document useful (0 votes)
11 views10 pages

Daa Exp7

Uploaded by

aniketpethe007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Daa Exp7

Uploaded by

aniketpethe007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name Aniket Shailesh Pethe

UID no. 2022300081

Experiment No. 8

AIM: To implement Graph Coloring problem using backtracking

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:

PROGRAM: #include <stdbool.h>


#include <stdio.h>
#include <time.h>
#define MAX_VERTICES 100

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");
}

bool isSafe(int n, int v, bool


graph[MAX_VERTICES][MAX_VERTICES], int color[], int c) {
for (int i = 0; i < n; i++)
if (graph[v][i] && color[i] != 0 && c == color[i])
return false;
return true;
}

bool graphColoringUtil(int n, bool


graph[MAX_VERTICES][MAX_VERTICES], int m, int color[], int v)
{
// If all vertices are assigned a color then return true
if (v == n)
return true;

for (int c = 1; c <= m; c++) {


if (isSafe(n, v, graph, color, c)) {
color[v] = c;
if (graphColoringUtil(n, graph, m, color, v + 1)
== true)
return true;
color[v] = 0;
}
}
return false;
}

bool graphColoringNaive(int n, bool


graph[MAX_VERTICES][MAX_VERTICES], int m) {
int color[MAX_VERTICES];
int colorCounter = 0;
for (int i = 0; i < n; i++)
color[i] = 0;

// Try all possible colors


bool found = false;
while (!found) {
if (colorCounter == n) {
// If all vertices are assigned a color, print
solution
found = true;
fprintf(file,"Solution exists:");
for (int i = 0; i < n; i++)
fprintf(file," C%d ", color[i]);
fprintf(file,"\n");
return true;
}
int v;
for (v = 0; v < n; v++)
if (color[v] == 0)
break;

for (int c = 1; c <= m; c++) {


if (isSafe(n, v, graph, color, c)) {
color[v] = c;
colorCounter++;
break;
}
}

if (color[v] == 0) {
colorCounter--;
if (colorCounter == 0) {
fprintf(file,"Solution does not exist\n");
return false;
}
}
}
return false;
}

bool graphColoring(int n, bool


graph[MAX_VERTICES][MAX_VERTICES], int m) {
int color[MAX_VERTICES];
for (int i = 0; i < n; i++)
color[i] = 0;

if (graphColoringUtil(n, graph, m, color, 0) == false) {


fprintf(file,"Solution does not exist\n");
return false;
}

printSolution(n, color);
return true;
}

void printMatrix(bool graph[MAX_VERTICES][MAX_VERTICES],int


n){
fprintf(file,"Adjacency Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fprintf(file,"%d ", graph[i][j]);
}
fprintf(file,"\n");
}
fprintf(file,"\n");
}

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:

DRY RUN: Graph Coloring:


CONCLUSION:
Using the backtracking method, I have solved the Graph coloring problem.

You might also like