0% found this document useful (0 votes)
24 views20 pages

Jannat

1. The lab report discusses three algorithms: Greedy Algorithms, Fractional Knapsack, and Dynamic Programming. 2. For Greedy Algorithms, the task is to find the maximum number of activities that can be performed by a single person given start and end times of activities. 3. For Fractional Knapsack, the task is to fill a knapsack to maximum value by selecting items without exceeding capacity, allowing breaking items. For non-fractional knapsack, items cannot be broken. 4. For Dynamic Programming, tasks include the 0/1 knapsack problem to maximize profit within weight capacity, and finding the longest common subsequence between two strings.

Uploaded by

Md Tariful Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views20 pages

Jannat

1. The lab report discusses three algorithms: Greedy Algorithms, Fractional Knapsack, and Dynamic Programming. 2. For Greedy Algorithms, the task is to find the maximum number of activities that can be performed by a single person given start and end times of activities. 3. For Fractional Knapsack, the task is to fill a knapsack to maximum value by selecting items without exceeding capacity, allowing breaking items. For non-fractional knapsack, items cannot be broken. 4. For Dynamic Programming, tasks include the 0/1 knapsack problem to maximize profit within weight capacity, and finding the longest common subsequence between two strings.

Uploaded by

Md Tariful Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab report

5 and 6

Submitted by
Submitted by Submitted to
Md Jannatul Ferdous
Md
ID: tariful islam
21225103178
Faria Binte Kader
Lecturer
Intake
Intake 49 49
Section 10 Dep of C.S.E (BUBT)
Section 10
Lab 5 | Tasks | Topic: Greedy Algorithms
1. You are given n activities with their start and finish times. Your program should output
the maximum number of activities that can be performed by a single person, assuming
that a person can only work on a single activity at a time.

Source Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< pair<int,int> >v;
int n;
cin>>n;
int s[n+1],f[n+1];
for(int i=1; i<=n; i++)
{
cin>>s[i];
}
for(int i=1; i<=n; i++)
{
cin>>f[i];
}
for(int i=1; i<=n; i++)
{

v.push_back({f[i],s[i]});

}
sort(v.begin(),v.end());
for(int i=0; i<v.size(); i++)
{
f[i+1]=v[i].first;
s[i+1]=v[i].second;
}
int i=1,c=1;
int j=2;
vector<int>v1;
v1.push_back(1);
while(j<=n)
{
if(f[i]<=s[j])
{
c++;
i=j;

v1.push_back(i);
j++;
}
else
{

j++;
}
}
cout<<"Activity performed by a single person are "<<endl;
for(int i=0; i<v1.size(); i++)
{
cout<<v1[i]<<" ";
}

return 0;
}
INPUT:
6
130585
246799
OUTPUT:
Activity performed by a single person are
1246

Explanation: Activity 1, 2, 4 and 6 were selected as they do not overlap.

2. Given the weights and profits of N items, in the form of {profit, weight} put these items in a
knapsack of capacity W to get the maximum total profit in the knapsack. In Fractional
Knapsack, we can break items for maximizing the total value of the knapsack.
Source Code:
#include<bits/stdc++.h>
using namespace std;
bool compare(pair<int,int>p1,pair<int,int>p2)
{
double v1=(double) p1.first/p1.second;
double v2=(double) p2.first/p2.second;
return v1>v2;
}
int main()
{
int n;
cout<<"input :";
cin>>n;
vector< pair<int,int> >v;
int s2,s3;
for(int i=0; i<n; i++)
{ cin>>s2>>s3;
v.push_back({s2,s3});
}
int w;
cin>>w;
sort(v.begin(),v.end(), compare);
int ans=0;
for(int i=0; i<n; i++)
{
if(w>v[i].second)
{
ans+=v[i].first;
w-=v[i].second;
continue;
}
double vw=(double) v[i].first/v[i].second;
ans+=vw*w;
w=0;
break;
}
cout<<"Output :";
cout<<ans<<endl;
}
INPUT:
Input: 3

60 10

100 20

120 30

50

OUTPUT:

Output :240

Explanation: By taking items of weight 10 and 20 kg and 2/3 fraction of 30 kg.


Hence total price will be 60+100+(2/3)(120) = 240

3. Use the same greedy approach in the previous problem but this time the knapsack is not
fractional meaning we cannot break the items.
Source Code:
#include<bits/stdc++.h>
using namespace std;

bool compare(pair<int,int>p1,pair<int,int>p2)
{
double v1=(double) p1.first/p1.second;
double v2=(double) p2.first/p2.second;
return v1>v2;
}
int main()
{
int n;
cout<<"input :";
cin>>n;
vector< pair<int,int> >v;
int s2,s3;
for(int i=0; i<n; i++)
{ cin>>s2>>s3;
v.push_back({s2,s3});
}
int w;
cin>>w;
sort(v.begin(),v.end(), compare);
int ans=0;
for(int i=0; i<n; i++)
{
if(w>v[i].second)
{
ans+=v[i].first;
w-=v[i].second;
continue;
}
}
cout<<"Output :";
cout<<ans<<endl;

}
INPUT:
input :3

60 10

100 20

120 30

50

OUTPUT:

Output :160

Explanation: By taking items of weight 10 and 20 kg as the other item cannot be broken down.
Hence total price will be 60+100 = 160.
Lab 6 | Tasks | Topic: Dynamic Programming

1. You are given a set of items, each with a weight and a profit value, and a knapsack that
has a maximum weight capacity. Your task is to select a subset of the items to maximize
the total profit while ensuring that the total weight does not exceed the capacity of the
knapsack.
Source Code:
include <bits/stdc++.h>

using namespace std;

int wt[100000+3],val[100000+3];

int dp[10000][10000];

knapsak(int n, int w)
{

if(w<=0)

return 0;

if(n<=0)

return 0;

if(dp[n][w]!=-1)

return dp[n][w];

if(wt[n-1]>w)

return dp[n][w]=knapsak(n-1,w);

else{

dp[n][w]= max(knapsak(n-1,w),knapsak(n-1,w-wt[n-1])+val[n-1]);

return dp[n][w];

int main()

int n;

cin>>n;

int N=10000;

for(int i=0; i<N; i++)

{
for(int j=0; j<N; j++)

dp[i][j]=-1;

for(int i=0; i<n; i++)

cin>>wt[i];

for(int i=0; i<n; i++)

cin>>val[i];

int w;

cin>>w;

cout<<knapsak(n,w);

INPUT:

3
123
10 15 40

6
OUTPUT:
65
Explanation:

2. You are given a rod of length N and a list of prices corresponding to different lengths of
the rod that can be obtained by cutting it. Your goal is to determine the optimal way to cut
the rod to maximize the total price.
Source Code:
#include <bits/stdc++.h>

using namespace std;


int cutRodUtil(vector<int>& price, int ind, int N, vector<vector<int>>& dp){

if(ind == 0){
return N*price[0];
}

if(dp[ind][N]!=-1)
return dp[ind][N];

int notTaken = 0 + cutRodUtil(price,ind-1,N,dp);

int taken = INT_MIN;


int rodLength = ind+1;
if(rodLength <= N)
taken = price[ind] + cutRodUtil(price,ind,N-rodLength,dp);

return dp[ind][N] = max(notTaken,taken);


}

int cutRod(vector<int>& price,int N) {

vector<vector<int>> dp(N,vector<int>(N+1,-1));
return cutRodUtil(price,N-1,N,dp);
}

int main() {
int n;
cin>>n;
int p;
cin>>p;
int s;
vector<int>price;
for(int i=0; i<p; i++)
{
cin>>s;
price.push_back(s);
}

cout<<"The Maximum price generated is "<<cutRod(price,n);


}

INPUT:
5
4
2369
OUTPUT:
The Maximum price generated is 11
3. You are given two sequences of characters, “str1” and “str2”. Your task is to find the
length of the longest common subsequence (LCS) between these two sequences.
Source Code:
#include<bits/stdc++.h>
using namespace std;
int dp[10000][10000];
int lcs(string &s1, string &s2, int n, int m)
{
if(n==0 || m==0)
{
return 0;
}
if(dp[n][m]!=-1)
{
return dp[n][m];
}
if(s1[n-1]==s2[m-1])
{
dp[n][m]= 1+ lcs(s1,s2,n-1,m-1);
}
else
{
dp[n][m]=max(lcs(s1,s2,n,m-1), lcs(s1,s2,n-1,m));
}
return dp[n][m];
}
int main()
{
string s1,s2;
cin>>s1>>s2;
int N=10000;
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
dp[i][j]=-1;
}
}

int n=s1.size(),m=s2.size();
cout<<lcs(s1,s2,n,m);
}

INPUT:
AGGTAB

GTXAYB

OUTPUT:

EXPLANATION:

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