0% found this document useful (0 votes)
2 views7 pages

assignment 4

The document contains two implementations of classic algorithm problems in C: the N-Queens problem and the Graph Coloring problem. The N-Queens implementation solves the 4x4 board configuration and prints the solutions, while the Graph Coloring implementation allows user input for the number of nodes and adjacency matrix to find possible colorings. Both codes include functions for checking safety and backtracking to find solutions.

Uploaded by

xjxjxjfjfjf0
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)
2 views7 pages

assignment 4

The document contains two implementations of classic algorithm problems in C: the N-Queens problem and the Graph Coloring problem. The N-Queens implementation solves the 4x4 board configuration and prints the solutions, while the Graph Coloring implementation allows user input for the number of nodes and adjacency matrix to find possible colorings. Both codes include functions for checking safety and backtracking to find solutions.

Uploaded by

xjxjxjfjfjf0
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/ 7

Implement N queens problem

Code

#include <stdio.h>
#include <stdbool.h>

#define N 4

void print_board(int board[N][N]) {


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1) {
printf("Q ");
} else {
printf(". ");
}
}
printf("\n");
}
printf("\n");
}
bool is_safe(int board[N][N], int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i][col] == 1) {
return false;
}
}
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) {
return false;
}
}
for (int i = row - 1, j = col + 1; i >= 0 && j < N; i--, j++) {
if (board[i][j] == 1) {
return false;
}
}

return true;
}
bool solve_n_queens(int board[N][N], int row) {
if (row == N) {
print_board(board);
return true;
}

for (int col = 0; col < N; col++) {


if (is_safe(board, row, col)) {
board[row][col] = 1;
if (solve_n_queens(board, row + 1)) {
return true;
}
board[row][col] = 0;
}
}

return false;
}

int main() {
int board[N][N] = {0};

printf("Solving the 4x4 N-Queens problem...\n");


if (!solve_n_queens(board, 0)) {
printf("No solution found.\n");
}

return 0;
}

Output

Question :-

Implement Graph Coloring Problem taking input from user


Code

#include<stdio.h>

static int m, n;

static int c=0;

static int count=0;

int g[50][50];

int x[50];

void nextValue(int k);

void GraphColoring(int k);

int main()

int i, j;

int temp;

printf("\nEnter the number of nodes: " );

scanf("%d", &n);

/*

printf("\nIf edge exists then enter 1 else enter 0 \n");

for(i=1; i<=n; i++)


printf("\nEnter Adjacency Matrix:\n");

for(i=1;i<=n;i++)
{

for(j=1;j<=n;j++)

scanf("%d", &g[i][j]);

printf("\nPossible Solutions are\n");

for(m=1;m<=n;m++)

if(c==1)

break;

GraphColoring(1);

printf("\nThe chromatic number is %d", m-1);

printf("\nThe total number of solutions is %d", count);


return 0;

}
void GraphColoring(int k)

int i;
while(1)

nextValue(k);

if(x[k]==0)

return;

if(k==n)

c=1;

for(i=1;i<=n;i++)

printf("%d ", x[i]);

count++;

printf("\n");

else

GraphColoring(k+1);

}
void nextValue(int k)
{

int j;

while(1)

x[k]=(x[k]+1)%(m+1);

if(x[k]==0)

return;

for(j=1;j<=n;j++)

if(g[k][j]==1&&x[k]==x[j])

break;

if(j==(n+1))

return;

Output

You might also like