Assignment_2
Assignment_2
ASSIGNMENT
Submitted by
Raghav Kohli
Reg.no: 23BCI0219
Branch: Bachelor of Technology in Computer Science and Engineering(With specialization)
School: SCOPE
Prof. Shalini L
VIT, Vellore
DAA Lab assignment 2
(Note: The questions are not in the order of the question sheet)
Q1. Write a program to implement N-Queens problem
Approach:
Use backtracking to place queens one by one in each row, ensuring no two attack each other. If a valid position is
found, recursively place the next queen; otherwise, backtrack and try the next possibility.
Code:
#include <iostream>
#include <vector>
using namespace std;
return true;
}
int countSolutions = 0;
bool solveNQueens(vector<vector<int>> &board, int col, int N)
{
if (col >= N)
{
countSolutions++;
printBoard(board, N);
return true;
}
return res;
}
int main()
{
int N;
cout << "Enter the number of queens: ";
cin >> N;
solveNQueens(board, 0, N);
return 0;
}
Pseudocode:
Output:
Approach:
Use Dynamic Programming (DP) with Memoization to find the optimal way to parenthesize matrices to minimize
multiplication cost. Define dp[i][j] as the minimum cost to multiply matrices from index i to j, and compute it using
the recurrence:
Code:
#include <iostream>
#include <vector>
#include <limits.h>
int main()
{
int n;
cout << "Enter number of matrices: ";
cin >> n;
vector<int> p(n + 1);
cout << "Minimum multiplication cost: " << matrixChainOrder(p, n + 1) << endl;
return 0;
}
Pseudocode:
Output:
Approach:
Use Dynamic Programming (DP) with a 2D table to compute the Longest Common Subsequence (LCS) length. Define
dp[i][j] as the length of the LCS of prefixes X[0...i-1] and Y[0...j-1], using the recurrence:
Code:
#include <iostream>
#include <vector>
return lcsStr;
}
int main()
{
string X, Y;
cout << "Enter first string: ";
cin >> X;
cout << "Enter second string: ";
cin >> Y;
Pseudocode:
Output:
Test case 01- Normal case
Approach:
Use Dynamic Programming (DP) with a 2D table to solve the 0/1 Knapsack Problem. Define dp[i][w] as the
maximum value that can be obtained using the first i items with a weight limit w, using the recurrence:
Code:
#include <iostream>
#include <vector>
int main()
{
int n, W;
cout << "Enter number of items: ";
cin >> n;
vector<int> values(n), weights(n);
cout << "Maximum value in knapsack: " << knapsack(W, weights, values, n) <<
endl;
return 0;
}
Pseudocode:
Output:
Approach:
Use Dynamic Programming (DP) with two arrays to compute the minimum time to assemble a product. Define
T1[i] and T2[i] as the minimum time to reach station i on line 1 and line 2, respectively. Use the recurrence:
where a1[i] and a2[i] are processing times, and t1[i-1], t2[i-1] are transfer times. Compute results
iteratively and return min(T1[n-1] + x1, T2[n-1] + x2), where x1 and x2 are exit times.
Code:
Code:
#include <bits/stdc++.h>
int main()
{
int num_stations;
cout << "Enter the number of stations: ";
cin >> num_stations;
cout << "Minimum time to leave the assembly line: " << ALS(a, t, e, x,
num_stations) << endl;
return 0;
}
Pseudocode:
Output:
Test Case 01- Normal Case:
Approach:
The Job Sequencing Problem using Branch and Bound (B&B) aims to minimize penalties by optimally scheduling jobs
within their deadlines. The approach is:
2. Use a Priority Queue (Min-Heap) to explore solutions, where each node represents a partial job schedule.
3. At each step, either include a job in the sequence (if a slot is available) or exclude it (adding its penalty).
4. Branch to explore both possibilities and use Bounding to prune non-optimal solutions.
5. The minimum penalty is obtained when all jobs are processed optimally.
Code:
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
struct Job
{
int id;
int penalty;
int deadline;
};
struct Node
{
vector<bool> assigned;
int level;
int penalty;
};
while (!pq.empty())
{
Node current = pq.top();
pq.pop();
if (current.level == jobs.size())
{
minPenalty = min(minPenalty, current.penalty);
continue;
}
return minPenalty;
}
int main()
{
int N;
cout << "Enter number of jobs: ";
cin >> N;
vector<Job> jobs(N);
cout << "Enter penalty and deadline for each job: " << endl;
int maxDeadline = 0;
for (int i = 0; i < N; ++i)
{
jobs[i].id = i + 1;
cin >> jobs[i].penalty >> jobs[i].deadline;
maxDeadline = max(maxDeadline, jobs[i].deadline);
}
cout << "Min Penalty: " << jobSequencing(jobs, maxDeadline) << endl;
return 0;
}
Pseudocode:
Output:
Test Case 1:
End of Assignment