Experiment No 5 AOA
Experiment No 5 AOA
5
Aim:- Implement C program for Backtracking and branch and bound (N-queen, Sum of
subsets, Graph coloring).
Theory:-
A] N- queen:- The N Queen problem is a classic example of combinatorial optimization
problems solved using backtracking and branch and bound techniques. Here’s a brief
explanation:
Backtracking: In backtracking, we place queens one by one in different columns. For each
queen, we check for a safe position where there are no threats from other queens. If we find
such a position, we move to the next queen. If we can’t find a safe position, we backtrack
and change the position of the previous queen.
Branch and Bound: The branch and bound approach also places queens one by one, but it
uses bounds to avoid exploring parts of the search space that won’t lead to a solution. It
keeps track of the ‘threat’ areas of queens already placed and only considers positions for the
new queen that are not under attack. If a partial solution is not promising (i.e., it will lead to
a dead end), it is abandoned (“pruned”), and the algorithm backtracks to explore other
possibilities.
C] Graph Coloring:- The Graph Coloring problem is about assigning colors to the vertices
of a graph such that no two adjacent vertices have the same color. Here’s a short explanation
of how it’s solved using backtracking and branch and bound:
Backtracking:
• Assign colors to vertices one by one, starting from the first vertex.
1
• Before coloring a vertex, ensure that it doesn’t share a color with any adjacent
vertices.
• If a valid color assignment is not possible, backtrack and change the color of the
previous vertex.
Procedure:-
for i ← 1 to n do
if PLACE(k, i) then
x[k] ← i
if k == n then
print X[1….n]
else
N_QUEEN(k+1, n)
end
end
end
if FEASIBLE_SUB_SET(i) == 1 then
if(sum == W) then
printX[1…i]
end
else
X[i+1] ← 1
X[i+1] ← 0
end
Function FEASIBLE_SUB_SET(i)
return 0
end
return 1
if CHECK_VERTEX(i)==1 then
if i ==N then
3
print COLOR[1….n]
else
j←1
while (j≤ M) do
COLOR(i+1) ← j
j ← j+1
end
end
end
for j ← 1 to i-1 do
if Adjacent(i, j) then
return 0
end
end
end
return 1
Analysis:-
A] Time complexity of N- queen Problem:- The time complexity of the N-Queen problem
when solved using backtracking is O(N!). This is because, in the worst case, the algorithm
tries to place a queen in each row of every column, which leads to a factorial time
complexity due to the recursive nature of the solution. For the branch and bound solution, the
time complexity is also O(N!) in the worst case. However, the branch and bound method can
be faster in practice because it avoids exploring entire subtrees in the search space that
cannot lead to a solution, thanks to the bounding function.
B] Time complexity of Sum of Subsets:- The time complexity of the Sum of Subsets
problem when using backtracking is O(2^n), where n is the number of elements in the
4
set. This is because the algorithm explores all possible subsets of the given set in the worst
case, which leads to exponential time complexity. For the branch and bound approach, the
worst-case time complexity remains the same, O(2^n), because it also explores all possible
subsets. However, the branch and bound method can be more efficient in practice due to its
pruning strategy, which eliminates branches that cannot lead to a solution.
C] Time complexity of Graph Coloring:- The time complexity of the Graph Coloring
problem when using backtracking is O(m^V), where m is the number of colors and V is the
number of vertices. This is because the algorithm tries every possible combination of colors
for each vertex. For the branch and bound approach, the time complexity is not explicitly
defined in general terms but is expected to be better than backtracking due to the pruning of
branches that cannot lead to a valid coloring. However, since Graph Coloring is an NP-
complete problem, both approaches will have exponential time complexity in the worst case.
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int Board[16], count;
void Queen(int row, int n);
int PlaceQueen(int row, int column);
void DisplaySolution(int n);
int main()
{int n;
printf("\nEnter number of Queens: ");
scanf("%d", &n);
Queen(1, n);
return 0;
}
void Queen(int row, int n)
{
int col;
for (col = 1; col <= n; ++col)
{
if (PlaceQueen(row, col))
{
5
Board[row] = col; // Place queen in 'row' row and in 'col' column
if (row == n)
{
DisplaySolution(n); // Print the solution
}
else
{
Queen(row + 1, n);}}}}
int PlaceQueen(int row, int column)
{
int i;
for (i = 1; i <= row - 1; ++i)
{
if (Board[i] == column || abs(Board[i] - column) == abs(i - row))
{
return 0; // Conflict found
}
}
return 1; // No conflicts
}
void DisplaySolution(int n)
{
int i, j;
printf("\n\nSolution %d:\n\n", ++count);
printf("\t");
for (i = 1; i <= n; ++i)
{
printf("R%d ", i);
}
for (i = 1; i <= n; ++i)
{
6
printf("\n\nC%d", i);
for (j = 1; j <= n; ++j)
{
if (Board[i] == j)
{
printf("\tQ"); // Cell with queen
}
else
{
printf("\t."); // Cell without queen
}}}
printf("\n\n")}
Output:-
7
Program:-
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int inc[50], w[50], sum, n;
int promising(int i, int wt, int total) {
return ((wt + total) >= sum) && ((wt == sum) || (wt + w[i + 1] <= sum));}
void SumofSubSet(int i, int wt, int total);
int main(void) {
int i, j, temp, total = 0;
printf("\nEnter Number of Elements in Set: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter Number %d: ", i + 1);
scanf("%d", &w[i]);
total += w[i];}
printf("\nInput the SUM value to create subset: ");
scanf("%d", &sum);
// Sort the elements in ascending order
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (w[j] > w[j + 1]) {
temp = w[j];
w[j] = w[j + 1];
w[j + 1] = temp;}}}
printf("\nElements in Sorted Order:\n");
for (i = 0; i < n; i++) {
printf("%d\t", w[i]);}
if (total < sum) {
printf("\nSubset construction is not possible\n");
} else { for (i = 0; i < n; i++) {
inc[i] = FALSE;}
8
printf("\nPossible solutions using backtracking:\n");
SumofSubSet(-1, 0, total);}
return 0;}
void SumofSubSet(int i, int wt, int total) {
int j;
if (promising(i, wt, total)) {
if (wt == sum) {
printf("{\t");
for (j = 0; j <= i; j++) {
if (inc[j]) {
printf("%d\t", w[j]);}}
printf("}\n");
} else {
inc[i + 1] = TRUE;
SumofSubSet(i + 1, wt + w[i + 1], total - w[i + 1]);
inc[i + 1] = FALSE;
SumofSubSet(i + 1, wt, total - w[i + 1]);}}}
Output:-
9
Input:-
#include <stdio.h>
int G[50][50], x[50];
void GraphColor(int k, int n);
int main()
{
int n, e, i, j, k, l;
printf("Enter Number of Vertices: ");
scanf("%d", &n);
printf("Enter Number of Edges: ");
scanf("%d", &e);
// Initialize Adjacency Matrix
for (i = 0; i < n; i++)
{for (j = 0; j < n; j++)
{
G[i][j] = 0;}}
printf("Enter indexes where value is 1:\n");
for (i = 0; i < e; i++)
{
scanf("%d %d", &k, &l);
G[k][l] = 1;
G[l][k] = 1;}
GraphColor(0, n);
printf("Colors of vertices:\n");
for (i = 0; i < n; i++)
{
printf("Vertex[%d]: %d\n", i + 1, x[i]);}
return 0;}
void GraphColor(int k, int n)
{int i, j;
x[k] = 1; // Solution Vector, starting with color 1
for (i = 0; i < k; i++)
10
{
if (G[i][k] != 0 && x[k] == x[i]) // if connected and has the same color
{x[k] = x[i] + 1; // assign a higher color than x[i]}}
if (k < n - 1)
{
GraphColor(k + 1, n); // move to the next vertex}}}}
Output:-
11