Ilovepdf Merged
Ilovepdf Merged
EXP.NO: 1
IMPLEMENT ALGORITHMS TO SOLVE THE GEOMETRIC PROBLEMS
DATE:
AIM:
PSEUDOCODE 1:
return true;
}else{
return false;
}}
CODE 1:
#include <stdio.h>
#include<stdbool.h>
return true;
}else{
return false;
}}
int main(){
int rec1[4],rec2[4];
for(int i=0;i<4;i++){
scanf("%d",&rec1[i]);
for(int i=0;i<4;i++){
scanf("%d",&rec2[i]);
717823F229
23CSR405 – Advanced Algorithms
if(isRectangleOverlap(rec1,rec2)){
} else{
OUTPUT 1:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 2:
Coordinate* c1 = (Coordinate*)a;
Coordinate* c2 = (Coordinate*)b;
int index = 0;
coordinates[index].row = r;
coordinates[index].col = c;
index++;
printf("\n");
CODE 2:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row;
int col;
717823F229
23CSR405 – Advanced Algorithms
} Coordinate;
Coordinate* c1 = (Coordinate*)a;
Coordinate* c2 = (Coordinate*)b;
int index = 0;
coordinates[index].row = r;
coordinates[index].col = c;
index++;
printf("\n");
int main() {
int rows,cols,rCenter,cCenter;
scanf("%d",&rows);
scanf("%d",&cols);
717823F229
23CSR405 – Advanced Algorithms
scanf("%d",&rCenter);
scanf("%d",&cCenter);
return 0;
OUTPUT 2:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 3:
return a < b ? a : b;
return a > b ? a : b;
int maxArea = 0;
maxArea = sideLength;
CODE 3:
#include <stdio.h>
#include <limits.h>
return a < b ? a : b;
717823F229
23CSR405 – Advanced Algorithms
return a > b ? a : b;
int maxArea = 0;
maxArea = sideLength;
int main() {
int n;
scanf("%d", &n);
717823F229
23CSR405 – Advanced Algorithms
printf("Maximum area of a square that can fit inside the intersecting region of at least two rectangles:
%d\n", result);
return 0;
OUTPUT 3:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 4:
typedef struct {
int x;
int y;
double distance;
} Point;
Point pointArray[n];
pointArray[i].x = points[i][0];
pointArray[i].y = points[i][1];
result[i][0] = pointArray[i].x;
result[i][1] = pointArray[i].y;
CODE 4:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int x;
717823F229
23CSR405 – Advanced Algorithms
int y;
double distance;
} Point;
Point pointArray[n];
pointArray[i].x = points[i][0];
pointArray[i].y = points[i][1];
result[i][0] = pointArray[i].x;
result[i][1] = pointArray[i].y;
int main() {
int n, k;
scanf("%d", &n);
int points[n][2];
717823F229
23CSR405 – Advanced Algorithms
scanf("%d", &k);
int result[k][2];
kClosestPoints(points, k, n, result);
return 0;
OUTPUT 4:
Marks
Criterion Max Marks
Awarded
Preparation 30
Lab
30
Performance
Viva voce and
40
performance
Total 100
Faculty Signature
RESULT:
Thus, the program to implement algorithms to solve the geometric problems was successfully
executed and the output is verified.
717823F229
23CSR405 – Advanced Algorithms
EXP.NO: 2
DEVELOP SOLUTIONS USING DYNAMIC PROGRAMMING
DATE:
AIM:
PSEUDOCODE 1:
int n = strlen(sequence);
int k = strlen(word);
int dp[n+1];
memset(dp,0,sizeof(dp));
int max_k = 0;
for(int i=k;i<=n;i++){
if(strncmp(sequence + i-k,word,k)==0){
if(dp[i]> max_k){
max_k = dp[i];
return max_k;
CODE 1:
#include <stdio.h>
#include <string.h>
int n = strlen(sequence);
int k = strlen(word);
717823F229
23CSR405 – Advanced Algorithms
int dp[n+1];
memset(dp,0,sizeof(dp));
int max_k = 0;
for(int i=k;i<=n;i++){
if(strncmp(sequence + i-k,word,k)==0){
if(dp[i]> max_k){
max_k = dp[i];
return max_k;
int main() {
scanf("%s", sequence);
scanf("%s", word);
return 0;
OUTPUT 1:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 2:
printf("[");
printf("[");
if(j!=i)
printf(",");
printf("]");
if(i!=numRows-1)
printf(",");
printf("]");
triangle[i][0] = 1;
triangle[i][i] = 1;
}}
return triangle;
717823F229
23CSR405 – Advanced Algorithms
CODE 2:
#include <stdio.h>
#include <stdlib.h>
printf("[");
printf("[");
if(j!=i)
printf(",");
printf("]");
if(i!=numRows-1)
printf(",");
printf("]");
triangle[i][0] = 1;
triangle[i][i] = 1;
} }
return triangle;
int main() {
int numRows;
717823F229
23CSR405 – Advanced Algorithms
scanf("%d", &numRows);
printPascal(triangle, numRows);
free(triangle[i]);
free(triangle);
return 0;
OUTPUT 2:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 3:
int i = 0, j = 0;
if (s[i] == t[j]) {
i++;
j++;
CODE 3:
#include <stdio.h>
#include <stdbool.h>
int i = 0, j = 0;
if (s[i] == t[j]) {
i++;
j++;
int main() {
scanf("%s", s);
scanf("%s", t);
if (isSubsequence(s, t)) {
717823F229
23CSR405 – Advanced Algorithms
printf("true\n");
} else {
printf("false\n");
return 0;
OUTPUT 3:
717823F229
23CSR405 – Advanced Algorithms
PSEUDOCODE 4:
ans[0] = 0;
*returnSize = n + 1;
return ans;
CODE 4:
#include <stdio.h>
#include <stdlib.h>
ans[0] = 0;
*returnSize = n + 1;
return ans;
int main() {
int n;
scanf("%d", &n);
int returnSize;
printf("Output: [");
717823F229
23CSR405 – Advanced Algorithms
printf("%d", result[i]);
if (i < returnSize - 1) {
printf(",");
printf("]\n");
free(result);
return 0;
OUTPUT 4:
Marks
Criterion Max Marks
Awarded
Preparation 30
Lab
30
Performance
Viva voce and
40
performance
Total 100
Faculty Signature
RESULT:
Thus, the program to develop solutions using dynamic programming was successfully executed and
the output was verified.
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
Ex.No: 03
Implement algorithms using backtracking methods
Date:
AIM:
To Implement algorithms using backtracking methods.
QUESTION 1:
PSEUDO CODE:
int backTrack(vector<int>& nums, int idx, int size, int XOR) {
if (idx == size)
return XOR;
return backTrack(nums, idx + 1, size, XOR ^ nums[idx]) + backTrack(nums, idx + 1, size, XOR);
}
int subsetXORSum(vector<int>& nums) {
int n = nums.size();
return backTrack(nums, 0, n, 0);
}
SOURCE CODE:
#include <iostream>
#include <vector>
using namespace std;
int backTrack(vector<int>& nums, int idx, int size, int XOR) {
if (idx == size)
return XOR;
return backTrack(nums, idx + 1, size, XOR ^ nums[idx]) + backTrack(nums, idx + 1, size, XOR);
}
int subsetXORSum(vector<int>& nums) {
int n = nums.size();
return backTrack(nums, 0, n, 0);
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
vector<int> nums(n);
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
cout << "Sum of all subset : " << subsetXORSum(nums) << endl;
return 0;
}
Time Complexity: O(2^n)
OUTPUT:
QUESTION 2:
PSEUDO CODE:
int solve(vector<int>&nums, vector<int>&vis, int index, int n){
if(index==n+1) return 1;
int res=0;
for(int i=0;i<nums.size();i++){
if(vis[nums[i]]==0 && (nums[i]%index==0 || index%nums[i]==0)){
vis[nums[i]]=1;
res+=solve(nums,vis,index+1,n);
vis[nums[i]]=0;
}
}
return res;
}
int countArrangement(int n) {
vector<int>nums, vis(n+1,0);
for(int i=1;i<=n;i++){
nums.push_back(i);
}
int ans=solve(nums,vis,1,n);
return ans;
SOURCE CODE:
#include<bits/stdc++.h>
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 3:
PSEUDO CODE:
vector<vector<int>>ans;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>ans;
void backtrack(vector<int>&arr,int start,int rem,vector<int>&temp){
if(rem<0)return;
if(rem==0)ans.push_back(temp);
for(int i=start;i<arr.size();i++){
temp.push_back(arr[i]);
backtrack(arr,i,rem-arr[i],temp);
temp.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int>temp;
backtrack(candidates,0,target,temp);
return ans;
}
int main() {
int n, target;
cout << "Enter the number of candidates: ";
cin >> n;
vector<int> candidates(n);
cout << "Enter the candidates: ";
for (int i = 0; i < n; i++) {
cin >> candidates[i];
}
cout << "Enter the target: ";
cin >> target;
vector<vector<int>> result = combinationSum(candidates, target);
cout << "Combinations: ";
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 4:
PSEUDO CODE:
void solve(unordered_set<string>& S, string& result, string& curr, int& n, bool& found) {
if (found) return;
if (curr.size() == n) {
if (S.find(curr) == S.end())
result = curr;
return;
}
curr += '0';
solve(S, result, curr, n, found);
curr.pop_back();
curr += '1';
solve(S, result, curr, n, found);
curr.pop_back();
}
string findDifferentBinaryString(vector<string>& nums) {
string result = "";
string curr = "";
bool found = false;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
int n = nums[0].size();
unordered_set<string> S(nums.begin(), nums.end());
solve(S, result, curr, n, found);
return result;
}
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
void solve(unordered_set<string>& S, string& result, string& curr, int& n, bool& found) {
if (found) return;
if (curr.size() == n) {
if (S.find(curr) == S.end())
result = curr;
return;
}
curr += '0';
solve(S, result, curr, n, found);
curr.pop_back();
curr += '1';
solve(S, result, curr, n, found);
curr.pop_back();
}
string findDifferentBinaryString(vector<string>& nums) {
string result = "";
string curr = "";
bool found = false;
int n = nums[0].size();
unordered_set<string> S(nums.begin(), nums.end());
solve(S, result, curr, n, found);
return result;
}
int main() {
int n;
cout << "Enter the number strings: ";
cin >> n;
vector<string> nums(n);
cout << "Enter binary strings :\n";
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
string result = findDifferentBinaryString(nums);
cout << "A different binary string is: " << result << endl;
return 0;
}
Time Complexity: O(2^n)
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
RESULT:
Thus Implement algorithms using backtracking methods was executed successfully and output was
verified.
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
Ex.No: 04
Detect cycles in a graph using appropriate algorithms.
Date:
AIM:
To Detect cycles in a graph using appropriate algorithms.
QUESTION 1:
PSEUDOCODE:
void dfs(vector<vector<char>>& grid, vector<vector<int>>& visited, int x, int y, int lastX, int lastY, char
cur, int color) {
visited[x][y] = color;
for (int i = 0; i < 4; i++) {
int a = x + dir[i];
int b = y + dir[i + 1];
if (a < 0 || b < 0 || a >= grid.size() || b >= grid[0].size()) continue;
if (!(a == lastX && b == lastY) && grid[a][b] == cur && visited[a][b] == color) {
ans = true;
}
if (visited[a][b] == 0 && !(a == lastX && b == lastY) && grid[a][b] == cur) {
dfs(grid, visited, a, b, x, y, cur, color);
}
}
}
bool containsCycle(vector<vector<char>>& grid) {
ans = false;
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> visited(m, vector<int>(n));
int color = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == 0) {
dfs(grid, visited, i, j, -1, -1, grid[i][j], color);
color++;
}
}
}
return ans;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
vector<int> dir{1, 0, -1, 0, 1};
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
bool ans=false;
void dfs(vector<vector<char>>& grid,vector<vector<int>>& visited,int x,int y,int lastX,int lastY,char
cur,int color) {
visited[x][y]=color;
for(int i=0;i<4;i++) {
int a=x+dir[i];
int b=y+dir[i + 1];
if(a<0 || b<0 || a>=grid.size() || b>=grid[0].size()) continue;
if(!(a==lastX && b==lastY) && grid[a][b]==cur && visited[a][b]==color) {
ans = true;
}
if(visited[a][b] == 0 && !(a==lastX && b==lastY) && grid[a][b]==cur) {
dfs(grid,visited,a,b,x,y,cur,color);
}
}
}
bool containsCycle(vector<vector<char>>& grid) {
ans = false;
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> visited(m, vector<int>(n));
int color = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == 0) {
dfs(grid, visited, i, j, -1, -1, grid[i][j], color);
color++;
}
}
}
return ans;
}
int main() {
int m, n;
cout << "Enter number of rows: ";
cin >> m;
cout << "Enter number of columns: ";
cin >> n;
vector<vector<char>> grid(m, vector<char>(n));
cout << "Enter the grid elements row-wise:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j]; } }
bool result = containsCycle(grid);
cout << "Output: " << (result ? "true" : "false") << endl;
return 0;
}
Time Complexity: O(m × n)
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 2:
PSEUDOCODE:
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool detectCapitalUse(string word) {
if(word.size() == 1)
return true;
int upper = 0, lower = 0;
for(char ch : word){
if (islower(ch)) lower++;
else upper++;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
}
if(lower==word.size() || upper==word.size()){
return true;
}
else if(upper==1 && isupper(word[0])){
return true;
}
return false;
}
int main() {
string word;
cout << "Enter a word: ";
cin >> word;
bool result = detectCapitalUse(word);
cout << "Output: " << (result ? "true" : "false") << endl;
return 0;
}
Time Complexity: O(1)
OUTPUT:
QUESTION 3:
PSEUDOCODE:
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool squareIsWhite(string cod) {
char c=cod[0];
int n=cod[1]-'0';
return (c-'a'+ 1+n)%2==1;
}
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
int main() {
string cod;
cout<<"Enter the chessboard coordinates: ";
cin>>cod;
bool res=squareIsWhite(cod);
cout<<"Output: "<<(res?"true":"false")<<endl;
return 0;
}
Time Complexity: O(1)
OUTPUT:
QUESTION 4:
PSEUDO CODE:
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
bool hascycle(ListNode* head){
ListNode* fast=head;
ListNode* slow=head;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
RESULT:
Thus, The algorithm to detect cycles in a graph was successfully executed and the output was
verified.
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
Ex.No: 05
Develop algorithms for topological sorting.
Date:
AIM:
To implement the algorithms for topological sorting.
QUESTION 1:
PSEUDOCODE:
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> in(N + 1), out(N + 1);
for (auto a : trust) {
++out[a[0]];
++in[a[1]];
}
for (int i = 1; i <= N; ++i) {
if (in[i] == N - 1 && out[i] == 0) return i;
}
return -1;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> in(N + 1), out(N + 1);
for (auto a : trust) {
++out[a[0]];
++in[a[1]];
}
for (int i = 1; i <= N; ++i) {
if (in[i] == N - 1 && out[i] == 0) return i;
}
return -1;
}
int main() {
int N, T;
cout << "Enter the number of people: ";
cin >> N;
cout << "Enter the number of trust relationships: ";
cin >> T;
vector<vector<int>> trust(T, vector<int>(2));
cout << "Enter the trust relationships:\n";
for (int i = 0; i < T; ++i) {
cout << "Trust " << i + 1 << ": ";
cin >> trust[i][0] >> trust[i][1];
}
int judge = findJudge(N, trust);
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
cout<<judge<<endl;
return 0;
}
Time Complexity: O(T+N)
OUTPUT:
QUESTION 2:
PSEUDOCODE:
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<int> vis(n, 0);
vector<vector<int>> adj(n);
for (auto it : edges) {
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
function<bool(int)> dfs = [&](int node) {
vis[node] = 1;
if (node == destination) return true;
for (auto it : adj[node]) {
if (!vis[it]) {
if (dfs(it)) return true;
}
}
return false;
};
return dfs(source);
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<int> vis(n, 0);
vector<vector<int>> adj(n);
for (auto it : edges) {
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
function<bool(int)> dfs = [&](int node) {
vis[node] = 1;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 3:
PSEUDOCODE:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root == p || root == q) {
return root;
}
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 4:
PSEUDO CODE:
vector<int> countFrequency(const string& str) {
vector<int> frequency(26, 0);
for (char c : str) {
frequency[c - 'a']++;
}
return frequency;
}
vector<int> intersection(const vector<int>& a, const vector<int>& b) {
vector<int> t(26, 0);
for (int i = 0; i < 26; i++) {
t[i] = min(a[i], b[i]);
}
return t;
}
vector<string> commonChars(vector<string>& words) {
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
vector<string> words(n);
cout << "Enter the words: " << endl;
for (int i = 0; i < n; i++) {
cin >> words[i];
}
vector<string> result = commonChars(words);
cout << "Common characters: ";
for (const string& s : result) {
cout << s << " ";
}
cout << endl;
return 0;
}
OUTPUT:
RESULT:
Thus, The algorithm to solve topological sorting was successfully executed and the output was
verified.
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
Ex.No: 06
Implement graph coloring algorithms.
Date:
AIM:
To implement the graph coloring algorithms.
QUESTION 1:
PSEUDOCODE:
bool canMakeSquare(vector<vector<char>>& grid) {
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
int w = 0, b = 0;
if(grid[i][j] == 'B') b++;
else w++;
if(grid[i][j+1] == 'B') b++;
else w++;
if(grid[i+1][j+1] == 'B') b++;
else w++;
if(grid[i+1][j] == 'B') b++;
else w++;
if(w >= 3 || b >= 3) return true;
}}
return false;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool canMakeSquare(vector<vector<char>>& grid) {
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
int w = 0, b = 0;
if(grid[i][j] == 'B') b++;
else w++;
if(grid[i][j+1] == 'B') b++;
else w++;
if(grid[i+1][j+1] == 'B') b++;
else w++;
if(grid[i+1][j] == 'B') b++;
else w++;
if(w >= 3 || b >= 3) return true;
}}
return false;
}
int main() {
vector<vector<char>> grid(3, vector<char>(3));
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
cout << "Enter the 3x3 grid (B for black and W for white):" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> grid[i][j];
}}
if (canMakeSquare(grid)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
Time Complexity: O(1)
OUTPUT:
QUESTION 2:
PSEUDOCODE:
bool checkTwoChessboards(string coordinate1, string coordinate2) {
int one = (coordinate1[0] - 'a') + (coordinate1[1] - '0');
int two = (coordinate2[0] - 'a') + (coordinate2[1] - '0');
return one % 2 == two % 2;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool checkTwoChessboards(string coordinate1, string coordinate2) {
int one = (coordinate1[0] - 'a') + (coordinate1[1] - '0');
int two = (coordinate2[0] - 'a') + (coordinate2[1] - '0');
return one % 2 == two % 2;
}
int main() {
string coordinate1, coordinate2;
cout << "Enter the first coordinate: ";
cin >> coordinate1;
cout << "Enter the second coordinate: ";
cin >> coordinate2;
if (checkTwoChessboards(coordinate1, coordinate2)) {
cout << "true" << endl;
} else {
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
QUESTION 3:
PSEUDOCODE:
int minimumOperations(vector<vector<int>>& grid) {
int ans = 0;
for(int c = 0; c < grid[0].size(); ++c){
for(int r = 1; r < grid.size(); ++r){
if(grid[r][c] > grid[r-1][c]) continue;
ans += grid[r-1][c] + 1 - grid[r][c];
grid[r][c] = grid[r-1][c] + 1;
}}
return ans;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
int minimumOperations(vector<vector<int>>& grid) {
int ans = 0;
for(int c = 0; c < grid[0].size(); ++c){
for(int r = 1; r < grid.size(); ++r){
if(grid[r][c] > grid[r-1][c]) continue;
ans += grid[r-1][c] + 1 - grid[r][c];
grid[r][c] = grid[r-1][c] + 1;
}}
return ans;
}
int main() {
int m, n;
cout << "Enter number of rows : ";
cin >> m;
cout << "Enter number of columns : ";
cin >> n;
vector<vector<int>> grid(m, vector<int>(n));
cout << "Enter the grid values:" << endl;
for(int i = 0; i < m; i++) {
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
QUESTION 4:
PSEUDOCODE:
bool checkValid(vector<vector<int>>& matrix) {
const int n = matrix.size();
bitset<101> col_hasX[101] = {0};
for(int i = 0; i < n; i++) {
bitset<101> row_hasX = 0;
for(int j = 0; j < n; j++) {
int x = matrix[i][j];
if(row_hasX[x]) return false;
else row_hasX[x] = 1;
if(col_hasX[j][x]) return false;
else col_hasX[j][x] = 1;
}}
return true;}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
bool checkValid(vector<vector<int>>& matrix) {
const int n = matrix.size();
bitset<101> col_hasX[101] = {0};
for(int i = 0; i < n; i++) {
bitset<101> row_hasX = 0;
for(int j = 0; j < n; j++) {
int x = matrix[i][j];
if(row_hasX[x]) return false;
else row_hasX[x] = 1;
if(col_hasX[j][x]) return false;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
else col_hasX[j][x] = 1;
}}
return true;
}
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
vector<vector<int>> matrix(n, vector<int>(n));
cout << "Enter the elements of the matrix:" << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> matrix[i][j];
}}
if(checkValid(matrix)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
Time Complexity: O(n^2)
OUTPUT:
RESULT:
Thus, The algorithm to solve graph coloring algorithms was successfully executed and the output
was verified.
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
Ex.No: 07
Implement pattern matching algorithms.
Date:
AIM:
To Implement the pattern matching algorithms.
QUESTION 1:
PSEUDOCODE:
bool repeatedSubstringPattern(string s) {
int n = s.size();
for (int i = n / 2; i >= 1; i--) {
if (n % i == 0) {
if (s.substr(0, n - i) == s.substr(i)) return true;
}
}
return false;
}
SOURCE CODE:
#include <iostream>
using namespace std;
bool repeatedSubstringPattern(string s) {
int n = s.size();
for (int i = n / 2; i >= 1; i--) {
if (n % i == 0) {
if (s.substr(0, n - i) == s.substr(i)) return true;
}}
return false;
}
int main() {
string s;
cout << "Enter a string: ";
cin >> s;
if (repeatedSubstringPattern(s))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Time Complexity: O(n^2)
OUTPUT:
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
QUESTION 2:
PSEUDOCODE:
bool hasMatch(string s, string p) {
int x = p.find("*");
string b = p.substr(0, x);
string e = p.substr(x + 1);
int i = s.find(b);
int j = s.find(e, i + b.size());
return (i != -1 && j != -1);
}
SOURCE CODE:
#include <iostream>
using namespace std;
bool hasMatch(string s, string p) {
int x = p.find("*");
string b = p.substr(0, x);
string e = p.substr(x + 1);
int i = s.find(b);
int j = s.find(e, i + b.size());
return (i != -1 && j != -1);
}
int main() {
string s, p;
cout << "Enter the string: ";
cin >> s;
cout << "Enter the pattern: ";
cin >> p;
if (hasMatch(s, p))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Time Complexity: O(n)
OUTPUT:
QUESTION 3:
PSEUDOCODE:
int numSpecial(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size(), res = 0;
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
OUTPUT:
QUESTION 4:
PSEUDOCODE:
vector<int> luckyNumbers(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
int maxCols[50] = {};
vector<int> res;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
maxCols[i] = max(maxCols[i], matrix[j][i]);
for (int i = 0; i < m; i++) {
int minRow = matrix[i][0], colIndex = 0;
for (int j = 1; j < n; j++) {
if (matrix[i][j] < minRow) {
minRow = matrix[i][j];
colIndex = j;
}}
if (minRow == maxCols[colIndex])
res.push_back(minRow);
}
return res;
}
SOURCE CODE:
#include <bits/stdc++.h>
using namespace std;
vector<int> luckyNumbers(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
int maxCols[50] = {};
vector<int> res;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
maxCols[i] = max(maxCols[i], matrix[j][i]);
for (int i = 0; i < m; i++) {
int minRow = matrix[i][0], colIndex = 0;
for (int j = 1; j < n; j++) {
if (matrix[i][j] < minRow) {
717823F229
INFORMATION TECHNOLOGY 23CSR405 & ADVANCED ALGORITHMS LABORATORY
minRow = matrix[i][j];
colIndex = j;
}}
if (minRow == maxCols[colIndex])
res.push_back(minRow);
}
return res;
}
int main() {
int m, n;
cout << "Enter number of rows: ";
cin >> m;
cout << "Enter number of columns: ";
cin >> n;
vector<vector<int>> matrix(m, vector<int>(n));
cout << "Enter the matrix values:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}}
vector<int> result = luckyNumbers(matrix);
cout << "Lucky Numbers: ";
for (int num : result)
cout << num << " ";
cout << endl;
return 0;
}
Time Complexity: O(m × n)
OUTPUT:
RESULT:
Thus, The algorithm to solve pattern matching algorithms was successfully executed and the output
was verified.
717823F229