0% found this document useful (0 votes)
5 views4 pages

N Queens

The document contains C code implementations for two classic problems: the N-Queen problem and the Graph Coloring problem. The N-Queen problem solution uses backtracking to find all possible placements of queens on a chessboard, while the Graph Coloring problem assigns colors to vertices of a graph ensuring no two adjacent vertices share the same color. Both implementations include functions for checking safety, printing solutions, and handling user input.

Uploaded by

adiisattyaa
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)
5 views4 pages

N Queens

The document contains C code implementations for two classic problems: the N-Queen problem and the Graph Coloring problem. The N-Queen problem solution uses backtracking to find all possible placements of queens on a chessboard, while the Graph Coloring problem assigns colors to vertices of a graph ensuring no two adjacent vertices share the same color. Both implementations include functions for checking safety, printing solutions, and handling user input.

Uploaded by

adiisattyaa
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/ 4

Name Anubhab Bera

Class 11500123033

1. N-queen problem ..

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

int x[100]; // Array to store the position of queens

// Function to check if it's safe to place the queen at (k, i)


bool place(int k, int i) {
for (int j = 0; j < k; j++) { // Fix the indexing to 0-based
if (x[j] == i || abs(x[j] - i) == abs(j - k)) {
return false;
}
}
return true;
}

// Function to print the solution


void printSolution(int n) {
for (int i = 0; i < n; i++) { // Fix the indexing to 0-based
printf("%d ", x[i] + 1); // Print 1-based positions
}
printf("\n");
}

// Function to solve the N-Queen problem using backtracking


void nQueen(int k, int n) {
for (int i = 0; i < n; i++) { // Fix the indexing to 0-based
if (place(k, i)) {
x[k] = i; // Place the queen at position i in row k
if (k == n - 1) { // If it's the last row, print the solution
printSolution(n);
} else {
nQueen(k + 1, n); // Move to the next row
}
}
}
}

int main() {
int n;
printf("Enter the number of queens: ");
scanf("%d", &n);
printf("The possible combinations are:\n");
nQueen(0, n); // Start from row 0
return 0;
}
Out Put....
2. Graph colouring problem

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

#define V 4 // Number of vertices

// Check if color c can be assigned to vertex v


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

// Recursive utility function to solve the coloring problem


bool graphColoringUtil(int graph[V][V], int m, int color[], int v) {
if (v == V)
return true; // All vertices are assigned a color

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


if (isSafe(v, graph, color, c)) {
color[v] = c; // Assign color
if (graphColoringUtil(graph, m, color, v + 1))
return true; // Recur to assign colors to the rest
color[v] = 0; // Backtrack
}
}

return false; // If no color can be assigned


}

// Solves the problem and prints the result


void graphColoring(int graph[V][V], int m) {
int color[V] = {0};

if (!graphColoringUtil(graph, m, color, 0)) {


printf("Solution does not exist.\n");
return;
}

printf("Assigned Colors:\n");
for (int i = 0; i < V; i++)
printf("Vertex %d -> Color %d\n", i, color[i]);
}

int main() {
int graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
};

int m = 3; // Number of colors


graphColoring(graph, m);

return 0;
}

Output :

You might also like