Sie sind auf Seite 1von 15

Advanced

Programming
Lab
File
N.S.I.T
Submitted By:
Jaideep Kumar
738/IT/13

INDEX
S.No
.

Problem

Date

Sign

Problem 1: Write a program to implement Floyd


Warshall Algorithm.
Source Code: #include<iostream>
using namespace std;
int main()
{int n;
cout<<"enter no. of nodes\n";
cin>>n;
int graph[n][n];
int i,j;
cout<<"enter distance\n ";
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{cin>>graph[i][j];}}
for(int k=0;k<n;k++)
{for(i=0;i<n;i++){for(j=0;j<n;j++)
{graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
}}}
cout<<"distance are:\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cout<<graph[i][j]<<" ";}
cout<<endl;}
return 0;}OUTPUT:

Problem 2: Write a Program to implement Dijsktras


Algorithm.
Source Code :
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> > adj[100];
#define inf 1000000
vector<int> dist( 101 , inf);
vector<int> parent(101);
void dijkstra(int s){
priority_queue<pair<int,int> > q;
q.push(make_pair(0,s));
dist[s]=0;
parent[s]=-1;
while(!q.empty()){
pair<int,int> node= q.top();
int d,v;
d=node.first; v=node.second;

//NODE

q.pop();
if(dist[v]<d) continue;
for(int i=0 ;i<adj[v].size();i++)
{int a=adj[v][i].first ;
int b=adj[v][i].second;
if( dist[a] > dist[v] + b ){

//

NODE
//

DIST

dist[a] = dist[v] +b;


parent[a]=v;
q.push( make_pair( dist[a] , a ));}}}}
int main(){
int u,v,n,w,m;
cout<<"ENTER the number of nodes and edges \n";
cin>>n;
cin>>m;
cout<<"enter the edges"<<endl;
for(int i=0;i<m;i++){
cin>>u>>v>>w;
adj[u].push_back( make_pair(v,w) );
adj[v].push_back( make_pair(u,w) );
}cout<<" enter the source "<<endl;
cin>>m;
dijkstra(m);
for(int i =1;i<=n;i++)
cout<<dist[i]<<" ";
cout<<endl;
cout<<"enter the target"<<endl;
cin>>m;
cout<<m;
for(int i=parent[m];i!=-1;i=parent[i])
cout<<"<----"<<i;}

OUTPUT:

Problem 3: Write a Program to implement Magic Matrix.


Source Code :
#include<iostream>
#include<cstring>
using namespace std;
void generateMat(int n)
{ int magicMat[n][n];
memset(magicMat, 0, sizeof(magicMat));
int i = 0;
int j = n/2;
for (int num=1; num <= n*n; )
{ if (i==-1 && j==n)
{ j = 0;
i = n-1;}
else
{if (j == -1)
j = n-1;
if (i < 0)
i=n-1;}

if (magicMat[i][j])
{ i--;

continue; }
else
magicMat[i][j] = num++;
i--;
j--;
}cout<<"The Magic Matrix for n="<<n<<":\nSum of each row or column
"<<(n*(n*n+1)/2)<<endl;
for(i=0;i<n; i++)
{ for(j=0; j<n; j++)
cout<<magicMat[i][j]<<" ";
cout<<endl;
}}int main()
{int n ;
cin>>n;
generateMat (n);
return 0;
}

OUTPUT:

Problem 4: Write a program to implement Matrix Chain


Multiplication Algorithm.
Source Code:
#include<iostream>
#include<climits>
using namespace std;
#define maxn 100
int m[100][100];
int ord[maxn][maxn];
int MatrixChainOrder(int p[], int n)
{ int i, j, k, L, q;
for (i = 1; i < n; i++)
m[i][i] = 0,ord[i][i]=i;
for (L=2; L<n; L++)
{ for (i=1; i<=n-L+1; i++)
{ j = i+L-1;
m[i][j] = INT_MAX;
for (k=i; k<=j-1; k++)
{q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j]){
m[i][j] = q;

ord[i][j]=k;
}}}}return m[1][n-1];
}void print(int i,int j){
if(i==j){
cout<<"m"<<1;
return;}
cout<<"(";
print(i,ord[i][j]);
print(ord[i][j]+1,j);
cout<<")";
}int main()
{int arr[100],n;
cout<<"enter no. of matrices";
cin>>n;
for(int i=0;i<=n;i++)
{cin>>arr[i];}
cout<<"Minimum number of multiplications is "<<MatrixChainOrder(arr, n);
getchar();
return 0;
}

OUTPUT:

Problem 5: Write a program to implement longest


common subsequence.
Source Code:
#include<iostream>
#include<cstring>
#include<cstdlib>
#define len 100
using namespace std;
int dp[len][len];
int max(int a,int b)
{return (a>b)?a:b;}
char *lcs(char *x,char *y)
{int i,j;
int n,m;
char a[50];
n=strlen(x);
m=strlen(y);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{dp[i][j]=0;
if(x[i-1]==y[j-1])
dp[i][j]=1+dp[i-1][j-1];
else

dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
} i=n;j=m;
char *str=(char *)malloc ((len+1)*sizeof(char));
int g=dp[n][m];
str[g--]='\0';
while(i && j)
{if(x[i-1]==y[j-1])
{str[g--]=x[i-1];
i--;j--;
}else if(dp[i-1][j]>dp[i][j-1])
i--;
else j--;}
return str;}
int main()
{char x[len],y[len];
int i,j,n;
cout<<"enter string \n";
cin>>n;
cin>>x>>y;
char *str=lcs(x,y);
for(i=3;i<=n;i++)
{cin>>y;
str=lcs(str,y);}
cout<<str;
return 0;
}

OUTPUT:

Problem 6: write a program to implement 0-1 Knapsack .


Source Code:#include<bits/stdc++.h>
using namespace std;
int main()
{int n,m;

//items, weight;

int weight[100],profit[100];
int dp[100][100];
cout<<"enter no. of items\n";
cin>>n;
cout<<"enter total capacity of knapsack\n";
cin>>m;
for(int i=1;i<=n;i++)
cin>>weight[i];
for(int i=1;i<=n;i++)
cin>>profit[i];
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){dp[i][j]=0;
if(weight[i]<=j)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+profit[i]);

}}cout<<"Profit is "<<dp[n][m];}

OUTPUT:

Problem 7: Write a program to implement fractional 0-1


knapsack .
Source Code:
#include<iostream>
#include<algorithm>
using namespace std;
void knapsack(int w[],int v[],int c,int n)
{int cur_w;
float tot_v;
int i, maxi;
int x[10];
for (i = 0; i < n; ++i)
x[i] = 0;
cur_w = c;
while (cur_w > 0) {
maxi = -1;
for (i = 0; i < n; ++i)
if ((x[i] == 0) &&((maxi == -1) || ((float)v[i]/w[i] > (float)v[maxi]/w[maxi])))
maxi = i;
x[maxi] = 1;
cur_w -= w[maxi];

tot_v += v[maxi];
if (cur_w < 0)
{ tot_v -= v[maxi];
tot_v += (1 + (float)cur_w/w[maxi]) * v[maxi];
}} cout<<"total profit is"<< tot_v;}
int main()
{int n,k;
cout<<"Enter no. of items\n";
cin>>n;
cout<<"Enter total capacity of Knapsack\n";
cin>>k;
int weight[100],value[100];
int i;
for(i=0;i<n;i++)
{cin>>weight[i];
}for(i=0;i<n;i++)
{cin>>value[i];
}knapsack(weight,value,k,n);
return 0;
}

OUTPUT: