0% found this document useful (0 votes)
16 views10 pages

Daa Exp7

Uploaded by

aniketpethe007
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)
16 views10 pages

Daa Exp7

Uploaded by

aniketpethe007
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/ 10

Name Aniket Shailesh Pethe

UID no. 2022300081

Experiment No. 7

AIM: To implement n-Queens Problem and Sum of subsets using backtracking

ALGORITHM:
N-Queen:
It involves placing N chess queens on an N×N chessboard in such a
way that no two queens attack each other. This means that no two
queens share the same row, column, or diagonal.

Sum Of Subset:
Given a set of positive integers and a target sum, the task is to find a
subset of the given set such that the sum of its elements is equal to
the given target sum.

PSEUDOCODE
N-QUEEN:
nQueen(board, col):
if col >= N:
printBoard(board)
return true
for each row in board:
if isSafe(row, col):
board[row][col] = 1
if nQueen(board, col + 1):
return true
board[row][col] = 0
return false

isSafe(row, col):
for each queen in the same row:
if queen is placed:
return false
for each (i, j) in upper diagonal:
if queen is placed at (i, j):
return false
for each (i, j) in lower diagonal:
if queen is placed at (i, j):
return false
return true

printBoard(board):
for each row in board:
for each cell in row:
if cell is queen:
print "Q "
else:
print ". "
print new line

SUM OF SUBSETS
procedure subsetSum(arr, targetSum):
subsetSumtill(arr, [], 0, 0, targetSum)

subsetSumtill(arr, subset, subsetSize, total, targetSum, currentIndex):


if total == targetSum:
printSubset
return
if currentIndex == len(arr) or total > targetSum:
return
subset.append(arr[currentIndex])
subsetSumUtil(arr, subset, subsetSize + 1, total + arr[currentIndex],
targetSum, currentIndex + 1)
subset.pop()
subsetSumUtil(arr, subset, subsetSize, total, targetSum, currentIndex + 1)

The time complexity of the backtracking algorithm for the N Queens


problem is typically 𝑂(𝑁!) where N is the size of the chessboard.
The time complexity of the backtracking solution for Sum of Subsets
problem is 𝑂(2^𝑛), where n is the number of elements in the input set.
PROGRAM: #include <stdbool.h>
#include <stdio.h>

void printSubset(int subset[], int size) {


printf("{ ");
for (int i = 0; i < size; i++) {
printf("%d ", subset[i]);
}
printf("}\n");
}

void sumOfSubsets(int arr[], int subset[], int n, int subsetSize, int total, int
targetSum, int currentIndex) {
// Check if the subset size exceeds the array size
if (subsetSize > n) {
printf("Error: Subset size exceeds the array size.\n");
return;
}

// Check if the subset size is negative (shouldn't happen, but for


robustness)
if (subsetSize < 0) {
printf("Error: Subset size is negative.\n");
return;
}

// Check if the current index is out of bounds


if (currentIndex < 0 || currentIndex >= n) {
return;
}

// Check if the total exceeds the target sum


if (total > targetSum) {
return;
}

// Base case: If the total equals the target sum, print the subset
if (total == targetSum) {
printSubset(subset, subsetSize);
return;
}

// Base case: If the current index equals the array size, return
if (currentIndex == n) {
return;
}

// Include the current element in the subset and recursively call


sumOfSubsets
subset[subsetSize] = arr[currentIndex];
sumOfSubsets(arr, subset, n, subsetSize + 1, total + arr[currentIndex],
targetSum, currentIndex + 1);

// Exclude the current element from the subset and recursively call
sumOfSubsets
sumOfSubsets(arr, subset, n, subsetSize, total, targetSum,
currentIndex + 1);
}

void findSubsets(int arr[], int n, int targetSum) {


int subset[n];
sumOfSubsets(arr, subset, n, 0, 0, targetSum, 0);
}
void fig(int N, int board[N][N]) {
printf("Possible solution: \n");

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


for (int j = 0; j < N; j++) {
if (board[i][j])
printf("%d ",j+1);}}
printf("\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j])
printf("Q ");
else
printf(". ");
}
printf("\n");
}
printf("\n");
}

bool isSafe(int N, int board[N][N], int row, int col) {


int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}

bool solveNQUtil(int N, int board[N][N], int col) {


if (col >= N) {
fig(N, board);
return true;
}
bool res = false;
for (int i = 0; i < N; i++) {
if (isSafe(N, board, i, col)) {
board[i][col] = 1;
res = solveNQUtil(N, board, col + 1) || res;
board[i][col] = 0;
}
}
return res;
}
bool solveNQ(int N) {
int board[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = 0;
}
}
if (!solveNQUtil(N, board, 0)) {
printf("Solution does not exist");
return false;
}
return true;
}

int main() {
printf("1.N-QUEEN'S PROBLEM\n");

int N;
printf("Number of queens:");
scanf("%d",&N);
printf("\n");
solveNQ(N);

printf("2.SUM OF SUBSETS PROBLEM\n");


int n;
printf("Number of elements?:");
scanf("%d",&n);
int arr[n];

for(int i=0;i<n;i++){
printf("Element no. %d:",i+1);
scanf("%d",&arr[i]);
}
int targetSum;
printf("Target sum?:");
scanf("%d",&targetSum);

printf("Subsets with sum equal to %d are:\n", targetSum);


findSubsets(arr, n, targetSum);

return 0;
}

RESULT:
DRY RUN: N-Queen:
Sum of Subset:
CONCLUSION:
Using the backtracking method, I have solved the n-Queens and Sum of Subsets
problem.

You might also like