Professional Documents
Culture Documents
Ada PDF
Ada PDF
4th Semester
BE/CSE(II)
UE173076
INDEX
TYPE OF ALGORITHM :
//Binary Search
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class arrays{
public:
void input(int *&arr,int num);
void show_i(int *&arr,int num);
void search_b(int *&arr,int num);
};
void arrays::input(int *&arr,int num)
{ int i;
srand(time(0));
for(i=0; i<num; i++)
{ int r = rand() % num;
arr[i]=r;
}
}
void arrays::search_b(int *&arr,int num){
int a;
int first,last,middle;
cout<<"Enter the number that you want to search:\t";
cin>>a;
first = 0;
last = num-1;
middle = (first+last)/2;
while (first <= last) {
if(arr[middle] < a) {
first = middle + 1; }
else if(arr[middle] == a)
{
cout<<a<<" found in the array at the location "<<middle+1<<"\n";
break; }
else {
last = middle - 1; }
middle = (first + last)/2; }
if(first > last){
cout<<a<<" not found in the array"<<endl;
}}
void arrays::show_i(int *&arr,int num){
for(int i=0;i<num;i++)
cout<<arr[i]<<"\t";
}
int main(){
arrays a;
int num;
cout<<"Enter Size of Array"<<endl;
cin>>num;
int *arr=new int[num];
int o;
int start,stop;
//merge sort
#include <iostream>
#include<stdlib.h>
using namespace std;
class merge_sort{
public:
void merge_sort1(int[],int, int);
void merge_array(int[],int, int, int, int);
};
void merge_sort::merge_sort1(int arr_sort[],int i, int j) {
int m;
if (i < j) {
m = (i + j) / 2;
merge_sort1(arr_sort,i, m);
merge_sort1(arr_sort,m + 1, j);
merge_array(arr_sort,i, m, m + 1, j);
}
}
void merge_sort::merge_array(int arr_sort[],int a, int b, int c, int d) {
int t[d+1];
int i = a, j = c, k = 0;
while (j <= d)
t[k++] = arr_sort[j++];
OUTPUT
//Quick Sort
#include <iostream>
using namespace std;
class quick_sort{
public:
void quick_sort1(int[],int,int);
int partition(int[],int,int);
};
void quick_sort::quick_sort1(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort1(a,l,j-1);
quick_sort1(a,j+1,u);
}
}
int quick_sort::partition(int a[],int p,int q){
int x,i,j,temp;
x=a[p];
i=p;
j=q+1;
do
{ do
i++;
while(a[i]<x&&i<=q);
do
j--;
while(x<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[p]=a[j];
a[j]=x;
return(j);
}
int main()
{ quick_sort q;
int n,i,start,stop,start1,stop1;
cout<<"Enter Size of array"<<endl;
cin>>n;
int *a=new int [n];
cout<<"Inserting "<<n<<" Random numbers"<<endl;
srand(time(0));
for(i=0; i<n; i++)
{
a[i]=rand()%n+1;
}
start = clock();
q.quick_sort1(a,0,n-1);
stop=clock();
cout<<"\nArray after sorting:"<<endl;
start1=clock();
for(i=0;i<n;i++){
cout<<a[i]<<" ";
cout<<"\t";}
stop1=clock();
cout<<"\nTime taken "<<(stop-start)/double(CLOCKS_PER_SEC)<<" Seconds"<<endl;
cout<<"Time taken "<<(stop1-start1)/double(CLOCKS_PER_SEC)<<" Seconds"<<endl;
return 0;
}
OUTPUT:
Write a program for Iterative Binary Search and Selection Sort in C/C++.
OUTPUT:
Write a program for Min and Max in C/C++.
//min_max
#include <iostream>
#include <climits>
using namespace std;
void findMinAndMax(int arr[], int low, int high, int& min, int& max)
{
if (low == high)
{
if (max < arr[low])
max = arr[low];
return;
}
if (high - low == 1)
{
if (arr[low] < arr[high])
{
if (min > arr[low])
min = arr[low];
int main()
{
int n;
cout<<"Enter Value of n"<<endl;
cin>>n;
int arr[n];
cout<<"Enter Data in Array"<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
cout << "The minimum element in the array is " << min << '\n';
cout << "The maximum element in the array is " << max;
return 0;
}
OUTPUT:
GREEDY ALGORITHM
Write a program for activity selection poroblem in C/C++.
//activity-selection
#include<iostream>
#include <algorithm>
using namespace std;
class activity
{
public:
float stime;
float ftime;
string name;
void insertinact(int t1,int t2,string n)
{
stime=t1;
ftime=t2;
name=n;
}
void display()
{
cout<<"Activity with name "<<name<<" is executed"<<endl;
cout<<endl;
}
};
bool gettime(activity a,activity b)
{
return (a.ftime<b.ftime);
}
}
else if(arr[i].ftime<=arr[j].stime)
{
arr[j].display();
i=j;
break;
}
}
}
}
OUTPUT:
Write a program for Knapsack poroblem in C/C++.
//knapsack
#include<iostream>
int main()
cin >> n;
cin >> w;
{
cin >> array[0][i] >> array[1][i];
}
{
used[i] = 0;
}
curw = w;
{
maxi = -1;
/ (float) array[0][maxi]))))
{
maxi = i;
}
}
used[maxi] = 1;
curw -= array[0][maxi];
totalprofit += array[1][maxi];
if (curw >= 0)
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< " completely in the bag, Space left: " << curw;
}
else
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
totalprofit -= array[1][maxi];
+ curw));
}
cout << "\nBags filled with objects worth: " << totalprofit;
return 0;
}
OUTPUT:
//dijkstra
#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
struct comp
{
bool operator()(const pii &a, const pii &b)
{
return a.second > b.second;
}
};
int D[MAX];
bool F[MAX];
int main()
cout << "Enter the edges with weigth: <source> <destination> <weigth>: \n";
{
cin >> u >> v >> w;
G[u].pb(pii(v, w));
G[v].pb(pii(u, w));
}
cout << "Enter the source node: ";
D[i] = INF;
D[starting] = 0;
Q.push(pii(starting, 0));
while (!Q.empty())
{
u = Q.top().first;
Q.pop();
if (F[u])
continue;
sz = G[u].size();
{
v = G[u][i].first;
w = G[u][i].second;
D[v] = D[u] + w;
Q.push(pii(v, D[v]));
}
}
F[u] = 1;
cout << "Node " << i << ", min weight = " << D[i] << endl;
return 0;
}
OUTPUT:
Write
a
#include <iostream>
using namespace std;
int i,j,k,a,b,u,v,n,ne=1;
int min_,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
int main()
{
cout<<"\n\tImplementation of Kruskal's algorithm\n";
cout<<"\nEnter the no. of vertices: ";
cin>>n;
cout<<"\nEnter the cost adjacency matrix:\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
}
cout<<"The edges of Minimum Cost Spanning Tree are\n";
while(ne < n)
{
for(i=1,min_=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min_)
{
min_=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
cout<<ne++ <<" edge "<<a<<","<<b<<" = "<<min_<<endl;
mincost +=min_;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n\tMinimum cost = "<<mincost<<endl;
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
OUTPUT:
Write a
program
for
mimimum spanning tree (PRISM) in C/C++.
#include<iostream>
int weight[20][20],visited[20],d[20],p[20];
int v,e;
void creategraph()
{
int i,j,a,b,w;
cout<<"\nEnter number of vertices ";
cin>>v;
cout<<"\nEnter number of edges ";
cin>>e;
for(i=1;i<=v;i++)
for(j=1;j<=v;j++)
weight[i][j]=0;
for(i=1;i<=v;i++)
{
p[i]=visited[i]=0;
d[i]=32767;
}
for(i=1;i<=e;i++)
{
cout<<"\nEnter edge a,b and weight w:";
cin>>a>>b>>w;
weight[a][b]=weight[b][a]=w;
}
}
void prim()
{
int current,totalvisited,mincost,i;
current=1;d[current]=0;
totalvisited=1;
visited[current]=1;
while(totalvisited!=v)
{
for(i=1;i<=v;i++)
{
if(weight[current][i]!=0)
if(visited[i]==0)
if(d[i]>weight[current][i])
{
d[i]=weight[current][i];
p[i]=current;
}
}
mincost=32767;
for(i=1;i<=v;i++)
{
if(visited[i]==0)
if(d[i]<mincost)
{
mincost=d[i];
current=i;
}
}
visited[current]=1;
totalvisited++;
}
mincost=0;
for(i=1;i<=v;i++)
mincost+=d[i];
cout<<"\nMinimum cost="<<mincost;
cout<<"\nMinimum span tree is";
for(i=1;i<=v;i++)
cout<<"\nVertex "<<i<<" is connected to"<<p[i];
}
int main()
{
creategraph();
prim();
}
OUTPUT:
DYNAMIC PROGRAMMING
#include<iostream>
#include<vector>
int i;
dp[0][0]=entry[0]+processing[0][0];
dp[1][0]=entry[1]+processing[1][0];
for(i=1;i<n;i++)
dp[0][i]=min(dp[0][i-1],dp[1][i-1]+transfer[1][i-1])+processing[0][i];
dp[1][i]=min(dp[1][i-1],dp[0][i-1]+transfer[0][i-1])+processing[1][i];
dp[0][n]=dp[0][n-1]+exit[0];
dp[1][n]=dp[1][n-1]+exit[1];
return min(dp[0][n],dp[1][n]);
int main()
{
int i,n;
cin>>entry[0]>>entry[1];
cin>>exit[0]>>exit[1];
for(i=0;i<n;i++)
cin>>processing[0][i];
for(i=0;i<n;i++)
cin>>processing[1][i];
cout<<"Enter the transfer time from each station of assembly line 1 to next station of assembly
line 2"<<endl;
for(i=0;i<n-1;i++)
cin>>transfer[0][i];
cout<<"Enter the transfer time from each station of assembly line 2 to next station of assembly
line 1"<<endl;
for(i=0;i<n-1;i++)
cin>>transfer[1][i];
cout<<"The minimum time required to get all the jobs done is "<<endl;
cout<<endl;
return 0;
}
OUTPUT
Write
a
#include<iostream>
using namespace std;
struct fwd{
int l;
int a[20];
};
int main(){
int k,i,j,n,p[10],m,z,cost[50],v,c[20][20];
cin>>k;
n=0;
for(i=1;i<=k;i++){
cout<<"Enter no. of vertices in stage "<<i<<" :";
cin>>s1[i].l;
n+=s1[i].l;
for(j=1;j<=s1[i].l;j++){
cout<<"Enter the value of vertex "<<j<<" :";
cin>>s1[i].a[j];
}
}
for(i=1;i<k;i++)
{
for(j=s1[i].a[1];j<=s1[i].a[s1[i].l];j++)
{
for(z=s1[i+1].a[1];z<=s1[i+1].a[s1[i+1].l];z++)
{
}
}
}
cost[n]=0;
int min,d[50],t;
for(i=k-1;i>=1;i--)
{
for(j=s1[i].a[1];j<=s1[i].a[s1[i].l];j++)
{
min=999;
for(z=s1[i+1].a[1];z<=s1[i+1].a[s1[i].l];z++){
if(cost[z]+c[j][z]<min)
min=cost[z]+c[j][z];
t=z;
}
cost[j]=min;
d[j]=t;
p[1]=1;
p[k]=n;
for(i=2;i<k;i++)
p[i]=d[p[i-1]];
for(i=1;i<k;i++)
cout<<p[i]<<"-->";
cout<<p[k];
OUTPUT
Write a program for All Pair Shortest Path in C/C++.
#include<iostream>
#define inf 99999
using namespace std;
int main()
{
int cost[10][10],a[10][10];
int n,e,f,s;
int i,j,k,c;
cout<<"enter no. of nodes in graph \n";
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
cost[i][j]=0;
else
cost[i][j]=inf;
}
}
cout<<"enter no. of edges \n";
cin>>e;
for(k=1;k<=e;k++)
{
cout<<"for edge "<<k<<endl;
cout<<"enter first vertex : ";
cin>>f;
cout<<"enter second vertex :";
cin>>s;
cout<<"enter the cost : ";
cin>>c;
cost[f][s]=c;
cost[s][f]=c;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=cost[i][j];
}
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=min(a[i][j], a[i][k]+a[k][j]);
}
}
}
cout<<"shortest distance matrix \n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
} cout<<endl;
}
}
OUTPUT
Write a program for Longest Common Subsequence in C/C++.
#include<iostream>
#include<string.h>
using namespace std;
int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];
void lcs()
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='c';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='u';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='l';
}
}
}
int main()
{
cout<<"Enter 1st sequence:";
cin>>x;
cout<<"Enter 2nd sequence:";
cin>>y;
cout<<"\nThe Longest Common Subsequence is ";
lcs();
print(m,n);
return 0;
}
OUTPUT
Write a program for 0/1 Knapsack in C/C++.
#include<iostream>
using namespace std;
return T[n][W];
}
int main()
{ int n;
cout<<"Total Number of items"<<endl;
cin>>n;
int w[n];
int V[n];
cout<<"Enter weight of Item"<<endl;
for(int i=0;i<n;i++)
cin>>w[i];
cout<<"Enter value of Item"<<endl;
for(int i=0;i<n;i++)
cin>>V[i];
//int w[n] = {5, 4, 6, 3};
//int V[n] = {10, 40, 30, 50};
int W = 10;
int Max;
cout<<endl;
Max = knapsack(W, w, V, n);
cout<<"Maximum profit we can obtain = "<<Max;
return 0;
}
OUTPUT
BACKTRACKING
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define N 5
cout<<board[i][j]<<" ";
cout<<endl;
int i, j;
if (board[row][i])
return false;
}
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
if (board[i][j])
return false;
return true;
if (col >= N)
return true;
if ( isSafe(board, i, col) )
board[i][col] = 1;
return true;
board[i][col] = 0;
}
return false;
bool solveNQ()
if (solveNQUtil(board, 0) == false)
return false;
printSolution(board);
return true;
int main()
solveNQ();
return 0;
OUTPUT
Write a program for graph coloring in C/C++.
#include<iostream>
using namespace std;
int G[10][10],m,edges,color_tab[10],v1,v2,i,j,n,a,b;
void Gen_Col_Value(int,int);
void Gr_coloring(int,int);
int main()
{
cout<<"\nEnter the number of nodes & edges\n";
cin>>n>>edges;
m=n-1;
cout<<"\nEnter the edges of the graph\n\n";
for (i=1;i<=edges; i++)
{
cout<<"Enter value of x,y\n";
cin>>v1>>v2;
G[v1][v2] = G[v2][v1] = 1;
cout<<endl;
}
Gr_coloring(1,n);
cout<<"\n The Vertices To be Coloured As...\n";
for(i=1;i<=n;i++)
cout<<"\n V"<<i<<":= "<<color_tab[i];
return 0;
}
void Gen_Col_Value(int k,int n)
{
while(1)
{
a=color_tab[k]+1;
b=m+1;
color_tab[k] = a%b;
if(color_tab[k]==0) return;
for(j=1;j<=n;j++)
{
if(G[k][j] && color_tab[k]==color_tab[j])
break;
}
if(j==n+1) return;
}
}
void Gr_coloring(int k,int n)
{
Gen_Col_Value(k,n);
if(color_tab[k]==0) return;
if(k==n) return;
else Gr_coloring(k+1,n);
}
OUTPUT