0% found this document useful (0 votes)
37 views11 pages

Experiment No 5 AOA

The document discusses solving combinatorial optimization problems using backtracking and branch and bound techniques. It provides algorithms and analysis for the N-queen problem, sum of subsets problem, and graph coloring problem. Code implementations are also included for the N-queen and sum of subsets problems.
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)
37 views11 pages

Experiment No 5 AOA

The document discusses solving combinatorial optimization problems using backtracking and branch and bound techniques. It provides algorithms and analysis for the N-queen problem, sum of subsets problem, and graph coloring problem. Code implementations are also included for the N-queen and sum of subsets problems.
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/ 11

Experiment No.

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.

B] Sum of Subset:- The Sum of Subsets problem is another classic combinatorial


optimization problem that can be solved using backtracking and branch and bound
techniques. Here’s a brief explanation:
Backtracking: In the Sum of Subsets problem, we are given a set of numbers and a target
sum. The goal is to determine if there is a subset of the numbers that adds up to the target
sum. Using backtracking, we explore all possible subsets by including or excluding each
element. If the current subset exceeds the target sum, we backtrack and try a different
combination.
Branch and Bound: The branch and bound method improves upon backtracking by adding
a bounding function. This function is used to avoid exploring subsets that cannot possibly
satisfy the condition (i.e., their sum already exceeds the target sum or even with the inclusion
of all remaining elements, cannot reach the target sum). This way, we prune the search space
and potentially find the solution more efficiently.

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.

Branch and Bound:

• Similar to backtracking, but with an additional bounding function to avoid


unnecessary searches.
• The bounding function determines if it’s possible to color the remaining vertices with
the current partial coloring.
• If it’s not possible, the algorithm backtracks and tries a different path.

Procedure:-

A] Algorithm for N-queen Problem:-

Algorithm N_QUEEN (k, n)

Input: n- Number of queen

k- Number of the queen being processed currently, initially set to 1.

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

B] Algorithm for Sum of Subsets:-

Algorithm SUB_SET_PROBLEM(i, sum, W, remSum)

Input: W: Number of which subset to be computed


2
i: Item index

sum: Sum of integers selected so far

remSum: Size of remaining problem i.e. (W- sum)

if FEASIBLE_SUB_SET(i) == 1 then

if(sum == W) then

printX[1…i]

end

else

X[i+1] ← 1

SUB_SET_PROBLEM(i+1, sum+w[i]+1, W, remSum-w[i]+1)

X[i+1] ← 0

SUB_SET_PROBLEM(i+1, sum, W, remSum-w[i]+1)

end

Function FEASIBLE_SUB_SET(i)

if(sum+remSum ≥ W) AND (sum == W)

or (sum+ w[i]+ 1≤ W) then

return 0

end

return 1

C] Algorithm for Graph Coloring :-

Algorithm GRAPH_COLORING(G, COLOR, i)

Input: Graph g with n vertices, list of colors, initial vertex i

//COLOR[1…N] is the array of n different colors

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

Function CHECK_VERTEX (i)

for j ← 1 to i-1 do

if Adjacent(i, j) then

if COLOR(i) == COLOR(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

You might also like