0% found this document useful (0 votes)
0 views12 pages

Ai 4

The document contains two algorithms: the N Queen Problem using Branch and Bound and the M Coloring Problem using backtracking. The N Queen Problem algorithm places queens on a chessboard while ensuring no two queens threaten each other, and the M Coloring Problem assigns colors to graph vertices without adjacent vertices sharing the same color. Both algorithms include code implementations in C++ and demonstrate their respective solutions.

Uploaded by

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

Ai 4

The document contains two algorithms: the N Queen Problem using Branch and Bound and the M Coloring Problem using backtracking. The N Queen Problem algorithm places queens on a chessboard while ensuring no two queens threaten each other, and the M Coloring Problem assigns colors to graph vertices without adjacent vertices sharing the same color. Both algorithms include code implementations in C++ and demonstrate their respective solutions.

Uploaded by

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

Name : Rutu Mahesh Ghatge Roll no.

15

Code :
N Queen Problem using Branch And Bound

#include<bits/stdc++.h>

using namespace std;

int N;

// function for printing the solution

void printSol(vector<vector<int>>board)

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

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

cout<<board[i][j]<<" ";

cout<<"\n";

/* Optimized isSafe function

isSafe function to check if current row contains or current left diagonal or current right diagonal
contains any queen or not if

yes return false

else return true

*/
bool isSafe(int row ,int col ,vector<bool>rows ,
vector<bool>left_digonals ,vector<bool>Right_digonals)

if(rows[row] == true || left_digonals[row+col] == true || Right_digonals[col-row+N-1] == true){

return false;

return true;

// Recursive function to solve N-queen Problem

bool solve(vector<vector<int>>& board ,int col ,vector<bool>rows ,


vector<bool>left_digonals ,vector<bool>Right_digonals)

// base Case : If all Queens are placed

if(col>=N){

return true;

/* Consider this Column and move in all rows one by one */

for(int i = 0;i<N;i++)

if(isSafe(i,col,rows,left_digonals,Right_digonals) == true)

rows[i] = true;
left_digonals[i+col] = true;

Right_digonals[col-i+N-1] = true;

board[i][col] = 1; // placing the Queen in board[i][col]

/* recur to place rest of the queens */

if(solve(board,col+1,rows,left_digonals,Right_digonals) == true){

return true;

// Backtracking

rows[i] = false;

left_digonals[i+col] = false;

Right_digonals[col-i+N-1] = false;

board[i][col] = 0; // removing the Queen from board[i][col]

return false;

int main()

// Taking input from the user


cout<<"Enter the no of rows for the square Board : ";

cin>>N;

// board of size N*N

vector<vector<int>>board(N,vector<int>(N,0));

// array to tell which rows are occupied

vector<bool>rows(N,false);

// arrays to tell which diagonals are occupied

vector<bool>left_digonals(2*N-1,false);

vector<bool>Right_digonals(2*N-1,false);

bool ans = solve(board , 0, rows,left_digonals,Right_digonals);

if(ans == true){

// printing the solution Board

printSol(board);

else{

cout<<"Solution Does not Exist\n";

}
Output :
Code :
M Coloring problem using backtracking
#include <bits/stdc++.h>

using namespace std;

// Number of vertices in the graph

#define V 4

void printSolution(int color[]);

/* A utility function to check if

the current color assignment

is safe for vertex v i.e. checks

whether the edge exists or not

(i.e, graph[v][i]==1). If exist

then checks whether the color to

be filled in the new vertex(c is

sent in the parameter) is already

used by its adjacent

vertices(i-->adj vertices) or

not (i.e, color[i]==c) */

bool isSafe(int v, bool graph[V][V], int color[], int c)

for (int i = 0; i < V; i++)

if (graph[v][i] && c == color[i])

return false;
return true;

/* A recursive utility function

to solve m coloring problem */

bool graphColoringUtil(bool graph[V][V], int m, int color[],

int v)

/* base case: If all vertices are

assigned a color then return true */

if (v == V)

return true;

/* Consider this vertex v and

try different colors */

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

/* Check if assignment of color

c to v is fine*/

if (isSafe(v, graph, color, c)) {

color[v] = c;
/* recur to assign colors to

rest of the vertices */

if (graphColoringUtil(graph, m, color, v + 1)

== true)

return true;

/* If assigning color c doesn't

lead to a solution then remove it */

color[v] = 0;

/* If no color can be assigned to

this vertex then return false */

return false;

/* This function solves the m Coloring

problem using Backtracking. It mainly

uses graphColoringUtil() to solve the

problem. It returns false if the m

colors cannot be assigned, otherwise

return true and prints assignments of

colors to all vertices. Please note


that there may be more than one solutions,

this function prints one of the

feasible solutions.*/

bool graphColoring(bool graph[V][V], int m)

// Initialize all color values as 0.

// This initialization is needed

// correct functioning of isSafe()

int color[V];

for (int i = 0; i < V; i++)

color[i] = 0;

// Call graphColoringUtil() for vertex 0

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

cout << "Solution does not exist";

return false;

// Print the solution

printSolution(color);

return true;

}
/* A utility function to print solution */

void printSolution(int color[])

cout << "Solution Exists:"

<< " Following are the assigned colors"

<< "\n";

for (int i = 0; i < V; i++)

cout << " " << color[i] << " ";

cout << "\n";

// Driver code

int main()

/* Create following graph and test

whether it is 3 colorable

(3)---(2)

| /|

| / |

|/ |

(0)---(1)

*/
bool graph[V][V] = {

{ 0, 1, 1, 1 },

{ 1, 0, 1, 0 },

{ 1, 1, 0, 1 },

{ 1, 0, 1, 0 },

};

// Number of colors

int m = 3;

// Function call

graphColoring(graph, m);

return 0;

}
Output :

You might also like