Experiment 13: Algorithm

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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];

sort(mices, mices + n);


sort(holes, holes + n);

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;

int knapSackRec(int W, vector<int>& wt,vector<int>& val, int index, vector<vector<int>>&


dp)
{
if (index < 0)
return 0;
if (dp[index][W] != -1)
return dp[index][W];

if (wt[index] > W) {

dp[index][W] = knapSackRec(W, wt, val, index - 1, dp);


return dp[index][W];
}
else {
dp[index][W] = max(val[index] + knapSackRec(W - wt[index], wt, val,index -
1, dp),knapSackRec(W, wt, val, index - 1, dp));
return dp[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));

cout <<"Maximum sum possible: " <<knapSackRec(W, weight, profit, n-1,dp);


return 0;
}

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

#define INF 99999

void printSolution(int dist[][V])


{
cout << "The following matrix shows the shortest "
"distances"
" between every pair of vertices \n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF"
<< " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

void floydWarshall(int dist[][V])


{
int i, j, k;

for (k = 0; k < V; k++) {


for (i = 0; i < V; i++) {

for (j = 0; j < V; j++) {


if (dist[i][j] > (dist[i][k] + dist[k][j])
&& (dist[k][j] != INF
&& dist[i][k] != INF))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}

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:

X and Y be two given sequences


Initialize a table LCS of dimension X.length * Y.length
X.label = X
Y.label = Y
LCS[0][] = 0
LCS[][0] = 0
Start from LCS[1][1]
Compare X[i] and Y[j]
If X[i] = Y[j]
LCS[i][j] = 1 + LCS[i-1, j-1]
Point an arrow to LCS[i][j]
Else
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])
Point an arrow to max(LCS[i-1][j], LCS[i][j-1])

Code:

#include <bits/stdc++.h>
using namespace std;

int lcs(string X, string Y, int m, int n,vector<vector<int> >& dp)


{
if (m == 0 || n == 0)
return 0;
if (X[m - 1] == Y[n - 1])
return dp[m][n] = 1 + lcs(X, Y, m - 1, n - 1, dp);

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];

sort(mices, mices + n);


sort(holes, holes + n);

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy