Daa Lab 4
Daa Lab 4
1
N-QUEENS
Date :
AIM:
To find the number of distinct ways to place n queens on an n x n chessboard such that no two queens
can attack each other.
Data Structure: Array
Data Type: Boolean
PSEUDOCODE:
bool isSafe(int row, int col, int n, bool cols[], bool diagonals1[], bool diagonals2[]) {
if (cols[col] || diagonals1[row - col + n - 1] || diagonals2[row + col]) {
return false;
}
return true;
}
void backtrack(int row, int n, bool cols[], bool diagonals1[], bool diagonals2[]) {
if (row == n) {
totalSolutions++;
return;
}
for (int col = 0; col < n; col++) {
if (isSafe(row, col, n, cols, diagonals1, diagonals2)) {
cols[col] = true;
diagonals1[row - col + n - 1] = true;
diagonals2[row + col] = true;
backtrack(row + 1, n, cols, diagonals1, diagonals2);
cols[col] = false;
diagonals1[row - col + n - 1] = false;
diagonals2[row + col] = false;
}
}}
int totalNQueens(int n) {
bool cols[n];
bool diagonals1[2 * n - 1];
bool diagonals2[2 * n - 1];
for (int i = 0; i < n; i++) cols[i] = false;
for (int i = 0; i < 2 * n - 1; i++) diagonals1[i] = diagonals2[i] = false;
totalSolutions = 0;
backtrack(0, n, cols, diagonals1, diagonals2);
return totalSolutions;
}
Time complexity: O(N!)
Space complexity: O(N)
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
int totalSolutions = 0;
bool isSafe(int row, int col, int n, bool cols[], bool diagonals1[], bool diagonals2[]) {
if (cols[col] || diagonals1[row - col + n - 1] || diagonals2[row + col]) {
return false;
}
717823F142
return true;
}
void backtrack(int row, int n, bool cols[], bool diagonals1[], bool diagonals2[]) {
if (row == n) {
totalSolutions++;
return;
}
for (int col = 0; col < n; col++) {
if (isSafe(row, col, n, cols, diagonals1, diagonals2)) {
cols[col] = true;
diagonals1[row - col + n - 1] = true;
diagonals2[row + col] = true;
backtrack(row + 1, n, cols, diagonals1, diagonals2);
cols[col] = false;
diagonals1[row - col + n - 1] = false;
diagonals2[row + col] = false;
}
}
}
int totalNQueens(int n) {
bool cols[n];
bool diagonals1[2 * n - 1];
bool diagonals2[2 * n - 1];
for (int i = 0; i < n; i++) cols[i] = false;
for (int i = 0; i < 2 * n - 1; i++) diagonals1[i] = diagonals2[i] = false;
totalSolutions = 0;
backtrack(0, n, cols, diagonals1, diagonals2);
return totalSolutions;
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int result = totalNQueens(n);
printf("The number of distinct solutions for %d-Queens is: %d\n", n, result);
return 0;
}
OUTPUT:
RESULT:
The program returns the number of distinct ways to place n queens on an n x n chessboard
without them attacking each other, with 92 solutions for 8 queens on an 8x8 board, successfully
executed.
717823F142
Exp.NO:4.2
PARTITION TO K EQUAL SUM SUBSETS
Date :
AIM:
To determine if it is possible to partition an array into k non-empty subsets such that the sum of
each subset is equal.
Data structure:Array
Data type:Integer
PSEUDOCODE:
bool canPartitionKSubsets(int* nums, int numsSize, int k) {
int sum = 0;
for (int i = 0; i < numsSize; i++) {
sum += nums[i];
}
if (sum % k != 0) return false;
int target = sum / k;
bool visited[numsSize];
memset(visited, false, sizeof(visited));
return backtrack(nums, visited, 0, k, target, 0, numsSize);
}
bool backtrack(int* nums, bool* visited, int startIndex, int k, int target, int currentSum, int count) {
if (k == 1) return true;
if (currentSum == target) {
return backtrack(nums, visited, 0, k - 1, target, 0, count);
}
for (int i = startIndex; i < count; i++) {
if (!visited[i] && currentSum + nums[i] <= target) {
visited[i] = true;
if (backtrack(nums, visited, i + 1, k, target, currentSum + nums[i], count)) {
return true;
}
visited[i] = false;
}
}
return false;
}
Time complexity: O(2^n)
Space complexity: O(n)
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool canPartitionKSubsets(int* nums, int numsSize, int k);
bool backtrack(int* nums, bool* visited, int startIndex, int k, int target, int currentSum, int count);
int main() {
int k, numsSize;
printf("Enter the number of elements in the array: ");
717823F142
scanf("%d", &numsSize);
int nums[numsSize];
printf("Enter the elements of the array: ");
for (int i = 0; i < numsSize; i++) {
scanf("%d", &nums[i]);
}
printf("Enter the value of k: ");
scanf("%d", &k);
bool backtrack(int* nums, bool* visited, int startIndex, int k, int target, int currentSum, int count) {
if (k == 1) return true;
if (currentSum == target) {
return backtrack(nums, visited, 0, k - 1, target, 0, count);
}
for (int i = startIndex; i < count; i++) {
if (!visited[i] && currentSum + nums[i] <= target) {
visited[i] = true;
if (backtrack(nums, visited, i + 1, k, target, currentSum + nums[i], count)) {
return true;
} visited[i] = false; }
}
return false;
}
717823F142
OUTPUT:
RESULT:
The program determines that it is possible to partition the array into `k` subsets with equal sum for
the given input, and the result was successfully executed.
Exp.NO:4.3
717823F142
SUBSET SUM PROBLEM USING BACKTRACKING
Date :
AIM:
To find and print all subsets of a given set of non-negative integers that sum up to a specified
value.
Data structure:Array
Data type:Integer
PSEUDOCODE:
SOURCE CODE:
#include <stdio.h>
void printSubsetsUtil(int* set, int n, int sum, int* subset, int subsetSize, int index);
void printSubsets(int* set, int n, int sum);
int main() {
int n, sum;
printf("Enter the number of elements in the set: ");
scanf("%d", &n);
int set[n];
printf("Enter the elements of the set: ");
for (int i = 0; i < n; i++) {
scanf("%d", &set[i]);
}
printf("Enter the target sum: ");
scanf("%d", &sum);
printSubsets(set, n, sum);
return 0;
}
717823F142
int subset[n];
printSubsetsUtil(set, n, sum, subset, 0, 0);
}
void printSubsetsUtil(int* set, int n, int sum, int* subset, int subsetSize, int index) {
if (sum == 0) {
printf("{ ");
for (int i = 0; i < subsetSize; i++) {
printf("%d ", subset[i]);
}
printf("}\n");
return;
}
if (index >= n || sum < 0) return;
subset[subsetSize] = set[index];
printSubsetsUtil(set, n, sum - set[index], subset, subsetSize + 1, index + 1);
printSubsetsUtil(set, n, sum, subset, subsetSize, index + 1);
}
OUTPUT:
RESULT:
The program finds and prints all subsets that sum to the given target value, and it has
successfully executed.
Exp.NO:4.4
TRAVELING SALESMAN PROBLEM USING BRANCH AND BOUND
717823F142
Date :
AIM:
To find the shortest possible tour that visits each city exactly once and returns to the starting city
using the Branch and Bound method for the Traveling Salesman Problem.
Data structure:Array
Data type:Integer
PSEUDOCODE:
void tsp(int graph[MAX_CITIES][MAX_CITIES], int currPos, int n, int visited[], int count, int cost,
int *ans, int path[], int minPath[]) {
if (count == n && graph[currPos][0]) {
if (cost + graph[currPos][0] < *ans) {
*ans = cost + graph[currPos][0];
for (int i = 0; i < n; i++) {
minPath[i] = path[i];
}
minPath[n] = 0;
}
return;
}
for (int i = 0; i < n; i++) {
if (!visited[i] && graph[currPos][i]) {
visited[i] = true;
path[count] = i;
tsp(graph, i, n, visited, count + 1, cost + graph[currPos][i], ans, path, minPath);
visited[i] = false;
}
}
}
Time complexity: O(n!)
Space complexity: O(n²)
SOURCE CODE:
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#define MAX_CITIES 100
void tsp(int graph[MAX_CITIES][MAX_CITIES], int currPos, int n, int visited[], int count, int cost,
int *ans, int path[], int minPath[]) {
if (count == n && graph[currPos][0]) {
if (cost + graph[currPos][0] < *ans) {
*ans = cost + graph[currPos][0];
for (int i = 0; i < n; i++) {
minPath[i] = path[i];
}
minPath[n] = 0;
}
return;
}
for (int i = 0; i < n; i++) {
return 0;
}
OUTPUT:
717823F142
RESULT:
The Branch and Bound method was successfully executed, calculating the minimum cost and
optimal path for the Traveling Salesman Problem, ensuring all cities are visited exactly once.
717823F142