Skip to content

Commit

Permalink
add: LPS9
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindXD committed Nov 17, 2024
1 parent 8986e20 commit 1e0ac81
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 0 deletions.
16 changes: 16 additions & 0 deletions LPS-9/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 style="color:red;text-align:right">Aravind N 22BRS1099</h1>

# Lab 9
## [Q1. Move Diagonal Hate 3 Queen Game](q1/soln.cpp)
[Question.](q1/README.md)

<div style="display: flex; justify-content: space-between;">
<img src="q1.png" alt="Complexity Plot for Q1" style="width: 100%;"/>
</div>

## [Q2. Mutual-distinct-digit subset](q2/soln.cpp)
[Question.](q2/README.md)

<div style="display: flex; justify-content: space-between;">
<img src="q2.png" alt="Complexity Plot for Q2" style="width: 100%;"/>
</div>
Binary file added LPS-9/q1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions LPS-9/q1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [Move Diagonal Hate 3 Queen Game](soln.cpp)
Move Diagonal Hate 3 Queen Game is a board game in which we have to place min(m,n) queens on a mXn rectangular board. The rows and columns are numbered from 1 to m and 1 to n respectively. We cannot place a queen on a cell if sum of their row and column indices is divisible by 3 (i.e) queens cannot be placed on cells with indices like (1,2), (5,1), ... In this game a queen is attacked by another queen if they are on the same row or on the same column. For example, if the dimension of the rectangular board is 4X5 then one of the solution is:

1 0 0 0 0
0 1 0 0 0
0 0 0 1 0
0 0 1 0 0

There are 6 such possible solutions for this board dimension. Given the dimension of the rectangular board develop a recursive algorithm using backtracking technique to find the number of different solutions

- Input Format
```
First line contains number of rows in the rectangular board, m
Next line contains number of rows in the rectangular board, n
```
- Output Format
```
Print the number of solutions
```
65 changes: 65 additions & 0 deletions LPS-9/q1/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <fstream>

using namespace std;
using namespace std::chrono;

bool is_safe_position(vector<vector<int>>& grid, int row, int col, int rows) {
if ((row + col) % 3 == 0) {
return false;
}
for (int i = 0; i < rows; i++) {
if (grid[i][col] == 1) {
return false;
}
}
return true;
}

void solve_nqueens(vector<vector<int>>& grid, int &solutions, int queens, int rows, int cols, int current_row) {
if (current_row == queens) {
solutions++;
return;
}
for (int col = 0; col < cols; col++) {
if (is_safe_position(grid, current_row, col, rows)) {
grid[current_row][col] = 1;
solve_nqueens(grid, solutions, queens, rows, cols, current_row + 1);
grid[current_row][col] = 0;
}
}
}

int main() {
ofstream file("nqueens_times.csv");
file << "Board Size,Time (microseconds)\n";

// Test different board sizes
for (int size = 4; size <= 12; size++) { // Reduced maximum size due to exponential growth
vector<vector<int>> board(size, vector<int>(size, 0));
int total_solutions = 0;

// Multiple runs for stable measurements
vector<long long> times;
const int runs = 3; // Reduced runs due to complexity

for (int run = 0; run < runs; run++) {
auto start = high_resolution_clock::now();
solve_nqueens(board, total_solutions, size, size, size, 0);
auto stop = high_resolution_clock::now();
times.push_back(duration_cast<microseconds>(stop - start).count());
}

// Use median time
sort(times.begin(), times.end());
long long median_time = times[runs/2];

file << size << "," << median_time << "\n";
}

file.close();
return 0;
}
100 changes: 100 additions & 0 deletions LPS-9/q1/graph.ipynb

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions LPS-9/q1/soln.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool is_safe_position(vector<vector<int>>& grid, int row, int col, int rows) {
int i;
if ((row + col) % 3 == 0) {
return false;
}
for (i = 0; i < rows; i++) {
if (grid[i][col] == 1) {
return false;
}
}
return true;
}

void solve_nqueens(vector<vector<int>>& grid, int &solutions, int queens, int rows, int cols, int current_row) {
int col;
if (current_row == queens) {
solutions++;
return;
}
for (col = 0; col < cols; col++) {
if (is_safe_position(grid, current_row, col, rows)) {
grid[current_row][col] = 1;
solve_nqueens(grid, solutions, queens, rows, cols, current_row + 1);
grid[current_row][col] = 0;
}
}
}

int main() {
int rows, cols;
cin >> rows >> cols;
vector<vector<int>> board(rows, vector<int>(cols, 0));
int total_solutions = 0;
int queens = min(rows, cols);
solve_nqueens(board, total_solutions, queens, rows, cols, 0);
cout << total_solutions << endl;

return 0;
}
Binary file added LPS-9/q2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions LPS-9/q2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## [Mutual-distinct-digit subset](soln.cpp)
Let D = {0,1,2,3,...9}. We call 1 is the successor of 0, 2 is a successor of 1, ... and 9 is a successor of 8. A number (formed with the digits of D) in which all the digits from the left are in successive order, is called as an LR-successive number (LRS-number). 789 is an LR-successive number where as 798 is not an LR-successive number. Similarly, a number in which all the digits from the right are in successive order, is called as an RL-Successive number (RLS- number). Two numbers are said to be digit-distinct number (dd-number) if there are no digits common between the numbers. 123 and 789 are dd-numbers. A set S is said to be mutual-dd set if all the pairs of S are dd-numbers. Given a set S that consists of either RLS-numbers or LRS-numbers, design a greedy-based pseudocode to compute the maximum mutual-dd subset of S. The elements of the maximum mutual-dd subset shoul be returned in an increasing order. There may be more than one maximum-mutual-dd subsets for S. If S= {123, 234, 789, 6789, 89} then some maximum mutual-dd subsets of S are : X1= {89,234}, X2= {89,123}, X3= {123, 789}. In case, if there are more than one maximum mutual-dd subset of S, your code should output the set which has the smallest number among the elements of X1,X2,X3. In case, if the smallest number occurs in more than one mutual-dd subset, your code should output the set which has both the smallest and the immediate bigger element to the smallest element among the elements of X1,X2,X3. In fact, Your code should output the elements of X in an ascending order. Your code should output the cardinality also. So, for the set S= {123, 234, 789, 6789, 89}, your code should output, 2, {89,123}. In case, mutual-dd-subset of the given set is empty, your code should output -1.

Input format:

Enter the number of elements of S: n (say)

Enter the first element of S:

Enter the second element of S:

....

....

Enter the n-th element of S :



Output format :

Cardinality of the set X:

First element of X :

Second element of X:

....

last element of X:
118 changes: 118 additions & 0 deletions LPS-9/q2/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <fstream>
#include <random>

using namespace std;
using namespace std::chrono;

int count_digits(int number) {
int digit_count = 0;
while (number > 0) {
digit_count++;
number /= 10;
}
return digit_count;
}

bool are_digits_unique(int x, int y) {
int x_size = count_digits(x);
int y_size = count_digits(y);
int digits_x[x_size];
int digits_y[y_size];
int remainder, idx = 0;

while (x > 0) {
remainder = x % 10;
digits_x[idx++] = remainder;
x /= 10;
}
idx = 0;
while (y > 0) {
remainder = y % 10;
digits_y[idx++] = remainder;
y /= 10;
}

for (int i = 0; i < x_size; i++) {
for (int j = 0; j < y_size; j++) {
if (digits_x[i] == digits_y[j]) {
return false;
}
}
}
return true;
}



void find_largest_unique_set(vector<int>& numbers) {
int size = numbers.size();
int max_count = 0;
vector<int> largest_set;

for (int i = 0; i < size; i++) {
vector<int> current_set = {numbers[i]};

for (int j = i + 1; j < size; j++) {
bool is_unique = true;
for (int k = 0; k < current_set.size(); k++) {
if (!are_digits_unique(current_set[k], numbers[j])) {
is_unique = false;
break;
}
}
if (is_unique) {
current_set.push_back(numbers[j]);
}
}

if (current_set.size() > max_count) {
max_count = current_set.size();
largest_set = current_set;
}
}
}

int main() {
ofstream file("unique_digits_times.csv");
file << "Input Size,Time (microseconds)\n";

// Random number generation
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 9999);

// Test different input sizes
for (int size = 10; size <= 200; size += 10) {
vector<int> numbers;

// Generate random numbers
for (int i = 0; i < size; i++) {
numbers.push_back(dis(gen));
}
sort(numbers.begin(), numbers.end());

// Multiple runs for stable measurements
vector<long long> times;
const int runs = 5;

for (int run = 0; run < runs; run++) {
auto start = high_resolution_clock::now();
find_largest_unique_set(numbers);
auto stop = high_resolution_clock::now();
times.push_back(duration_cast<microseconds>(stop - start).count());
}

// Use median time
sort(times.begin(), times.end());
long long median_time = times[runs/2];

file << size << "," << median_time << "\n";
}

file.close();
return 0;
}
101 changes: 101 additions & 0 deletions LPS-9/q2/graph.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 1e0ac81

Please sign in to comment.