Experiment 13: Algorithm
Experiment 13: Algorithm
Experiment 13: Algorithm
Q. There are N Mice and N holes are placed in a straight line. Each hole can accommodate
only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move
one step left from x to x -1. Any of these moves consumes 1 minute. Assign mice to holes so
that the time when the last mouse gets inside a hole is minimized.
Algorithm:
sort mice positions (in any order)
sort hole positions
Loop i = 1 to N:
update ans according to the value
of |mice(i) - hole(i)|. It should
be maximum of all differences.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of holes: ";
cin>>n;
int mices[n],holes[n];
cout<<"Enter position of mices: ";
for(int i=0;i<n;i++) cin>>mices[i];
cout<<"Enter position of holes: ";
for(int i=0;i<n;i++) cin>>holes[i];
int maxi = 0;
for(int i = 0; i < n; ++i)
{
maxi=max(maxi,abs(mices[i] - holes[i]));
}
cout << "The last mouse gets into the hole in time: "<< maxi
<< endl;
return 0;
}
Output:
Experiment 10
Q. We are given N items where each item has some weight and profit associated with it. We
are also given a bag with capacity W, [i.e., the bag can hold at most W weight in it]. The
target is to put the items into the bag such that the sum of profits associated with them is the
maximum possible. Write a program to implement 0/1 Knapsack problem.
Algorithm:
The maximum value obtained from ‘N’ items is the max of the following two values.
Case 1 (include the Nth item): Value of the Nth item plus maximum value obtained by
remaining N-1 items and remaining weight i.e. (W-weight of the Nth item).
Case 2 (exclude the Nth item): Maximum value obtained by N-1 items and W weight.
If the weight of the ‘Nth‘ item is greater than ‘W’, then the Nth item cannot be
included and Case 2 is the only possibility.
Code:
#include <bits/stdc++.h>
using namespace std;
if (wt[index] > W) {
int main()
{
int n,W;
cout<<"Enter number of items: ";
cin>>n;
cout<<"Enter weight of knapsack: ";
cin>>W;
vector<int> profit,weight;
cout<<"Enter weights of items: ";
for(int i=0;i<n;i++){
int temp;
cin>>temp;
weight.push_back(temp);
}
cout<<"Enter profits of items: ";
for(int i=0;i<n;i++){
int temp;
cin>>temp;
profit.push_back(temp);
}
vector<vector<int>> dp(n,vector<int>(W+1,-1));
Output:
Experiment 11
Q. The problem is to find the shortest distances between every pair of vertices in a given
edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n.
Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no
edge from i to j.
Do it in place.
Algorithm:
# define N 4
void floydwarshall()
{
int cost [N][N];
int i, j, k;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
cost [i][j]= cost Mat [i] [j];
for(k=0; k<N; k++)
{
for(i=0; i<N; i++)
for(j=0; j<N; j++)
if(cost [i][j]> cost [i] [k] + cost [k][j];
cost [i][j]=cost [i] [k]+'cost [k] [i]:
}
//display the matrix cost [N] [N]
}
Code:
#include <bits/stdc++.h>
using namespace std;
#define V 4
int main()
{
int graph[V][V] = {
{ 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 }
};
floydWarshall(graph);
return 0;
}
Output:
Given Graph:
Experiment 12
Q. Q. Given two strings, S1 and S2, the task is to find the length of the Longest Common
Subsequence, i.e. longest subsequence present in both of the strings. Write a program to
implement Longest Common Subsequence (LCS).
Algorithm:
Code:
#include <bits/stdc++.h>
using namespace std;
if (dp[m][n] != -1) {
return dp[m][n];
}
return dp[m][n] = max(lcs(X, Y, m, n - 1, dp),lcs(X, Y, m - 1, n, dp));
}
int main()
{
string X = "AGGTAB";
string Y = "GXTXAYB";
int m = X.size();
int n = Y.size();
vector<vector<int> > dp(m + 1, vector<int>(n + 1, -1));
cout<<"String 1: "<<X<<endl;
cout<<"String 2: "<<Y<<endl;
cout << "Length of LCS is " << lcs(X, Y, m, n, dp);
return 0;
}
Output:
Experiment 13
Q. There are N Mice and N holes are placed in a straight line. Each hole can accommodate
only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move
one step left from x to x -1. Any of these moves consumes 1 minute. Assign mice to holes so
that the time when the last mouse gets inside a hole is minimized.
Algorithm:
sort mice positions (in any order)
sort hole positions
Loop i = 1 to N:
update ans according to the value
of |mice(i) - hole(i)|. It should
be maximum of all differences.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of holes: ";
cin>>n;
int mices[n],holes[n];
cout<<"Enter position of mices: ";
for(int i=0;i<n;i++) cin>>mices[i];
cout<<"Enter position of holes: ";
for(int i=0;i<n;i++) cin>>holes[i];
int maxi = 0;
for(int i = 0; i < n; ++i)
{
maxi=max(maxi,abs(mices[i] - holes[i]));
}
cout << "The last mouse gets into the hole in time: "<< maxi << endl;
return 0;
}
Output: