0% found this document useful (0 votes)
17 views

Lab 10 Daa

The document describes a laboratory assignment on algorithms. It includes code to solve the travelling salesman problem using dynamic programming and the N queens problem using backtracking. The code includes functions for solving each problem and outputting the results.

Uploaded by

Alok Kumar
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)
17 views

Lab 10 Daa

The document describes a laboratory assignment on algorithms. It includes code to solve the travelling salesman problem using dynamic programming and the N queens problem using backtracking. The code includes functions for solving each problem and outputting the results.

Uploaded by

Alok Kumar
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/ 6

Laboratory Assignment File

For

Design And Analysis of Algorithms Laboratory


(CSPC-206)

B.Tech (Computer Science and Engineering)


IV Semester Sec-A

Department of Computer Science and Engineering


DR B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
Jalandhar -144011,Punjab-India

Prepared during (Jan-June 2024) By


Name: ALOK KUMAR
Roll No: 22103010
Assignment-10
Q1)Travelling Salesman Problem

CODE-

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

const int INF = numeric_limits<int>::max();

// Function to solve the TSP using dynamic programming and bitmasking


int tsp(int mask, int pos, const vector<vector<int>>& dist,
vector<vector<int>>& dp, int n) {
// If all cities have been visited, return to the starting city
if (mask == (1 << n) - 1) {
return dist[pos][0];
}

// If this state has already been computed, return the stored result
if (dp[mask][pos] != -1) {
return dp[mask][pos];
}

// Initialize the minimum cost to a large value


int min_cost = INF;

// Try to visit each unvisited city


for (int city = 0; city < n; ++city) {
if ((mask & (1 << city)) == 0) {
// Calculate the cost of visiting the city and update the mask
int new_mask = mask | (1 << city);
int cost = dist[pos][city] + tsp(new_mask, city, dist, dp, n);
// Update the minimum cost if a better route is found
min_cost = min(min_cost, cost);
}
}

// Store the result and return the minimum cost


return dp[mask][pos] = min_cost;
}

int main() {
// Define the number of cities
int n;
cout << "Enter the number of cities: ";
cin >> n;

// Define the distance matrix


vector<vector<int>> dist(n, vector<int>(n));
cout << "Enter the distance matrix:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> dist[i][j];
}
}

// Initialize the DP table with -1 (not computed)


vector<vector<int>> dp(1 << n, vector<int>(n, -1));

// Solve the TSP starting from the first city


int result = tsp(1, 0, dist, dp, n);

// Print the result


cout << "Minimum cost to visit all cities and return to the starting city:
" << result << endl;

return 0;
}

OUTPUT-

Q2) N Queens
CODE-

#include <iostream>
#include <vector>
#include <algorithm> // Include the <algorithm> header for std::find

using namespace std;

class NQueens {
public:
NQueens(int n) : n(n), board(n, vector<char>(n, '.')) {}
// Function to solve the N-Queens problem
void solve() {
backtrack(0);
}

private:
int n;
vector<vector<char>> board;
vector<int> cols; // Keep track of columns used
vector<int> diag1; // Keep track of major diagonals used
vector<int> diag2; // Keep track of minor diagonals used

// Function to print the current state of the board


void printBoard() {
for (const auto& row : board) {
for (const auto& cell : row) {
cout << cell << ' ';
}
cout << endl;
}
cout << endl;
}

// Backtracking function to find solutions


void backtrack(int row) {
// If all rows are filled, print the board
if (row == n) {
printBoard();
return;
}

// Try to place a queen in each column of the current row


for (int col = 0; col < n; ++col) {
if (isSafe(row, col)) {
// Place the queen
board[row][col] = 'Q';
cols.push_back(col);
diag1.push_back(row + col);
diag2.push_back(row - col);

// Recursively solve the next row


backtrack(row + 1);

// Backtrack and remove the queen


board[row][col] = '.';
cols.pop_back();
diag1.pop_back();
diag2.pop_back();
}
}
}

// Function to check if placing a queen at the given position is safe


bool isSafe(int row, int col) {
// Check if the column is already in use
if (find(cols.begin(), cols.end(), col) != cols.end()) {
return false;
}

// Check if the major diagonal is already in use


if (find(diag1.begin(), diag1.end(), row + col) != diag1.end()) {
return false;
}

// Check if the minor diagonal is already in use


if (find(diag2.begin(), diag2.end(), row - col) != diag2.end()) {
return false;
}

return true;
}
};

int main() {
int n;
cout << "Enter the value of N (number of queens): ";
cin >> n;

NQueens solver(n);
cout << "Solutions:" << endl;
solver.solve();

return 0;
}

OUTPUT-

You might also like