algorithm analysis and design complete file

60
University School of Information Technology, G.G.S.I.PU. ALGORITHM ANALYSIS AND DESIGN FILE Submitted By : ANUP SINGH KUSHWAHA Enrol no: 0111645308

Upload: rajeev-ranjan

Post on 11-Apr-2015

283 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: ALGORITHM ANALYSIS AND DESIGN complete file

University School of Information Technology, G.G.S.I.PU.

ALGORITHM ANALYSIS AND DESIGN

FILE

Submitted By : ANUP SINGH KUSHWAHA Enrol no: 0111645308

Page 2: ALGORITHM ANALYSIS AND DESIGN complete file

// Program to implement Breadth-First-Search (BFS)

#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 100

void bfs(int n,int s);void enqueue(int q[],int m);int dequeue(int q[]);

int color[MAX];int d[MAX];int pi[MAX];

typedef struct vert* ver;struct vert{

int ident;ver next;

} cor;ver adj_list[MAX];

void main(){

int n,i,j,k,s,u;ver temp,prev;clrscr();printf("Enter no of nodes : ");scanf("%d",&n);for(i=0;i<=n;i++)

adj_list[i]='\0';for(i=1;i<=n;i++){

prev=NULL;printf("Enter No of vertex connected to %d :",i);scanf("%d",&j);for(k=0;k<j;k++){

temp=(ver)malloc(sizeof(cor));temp->next=NULL;printf("Enter Vertex No. connected to vertex %d : ",i);scanf("%d",&temp->ident);if(prev==NULL)

adj_list[i]=temp;else

prev->next=temp;prev=temp;

}

Page 3: ALGORITHM ANALYSIS AND DESIGN complete file

}printf("\nEnter the starting point of graph : ");scanf("%d",&s);bfs(n,s);for(i=1;i<=n;i++){

printf("\nThe Distance from source(%d) to %d is %d",s,i,d[i]);printf("\nThe previous vertex of %d is %d",i,pi[i]);

}getch();

}

void bfs(int n,int s){

int i,v,q[MAX],u;ver temp;for(i=1;i<=n;i++){

if(i==s)continue;

color[i]=0;d[i]=32767;pi[i]=NULL;

}color[s]=1;d[s]=0;pi[s]=NULL;for(i=0;i<=n;i++)

q[i]='\0';enqueue(q,s);while(q[0]!='\0'){

u=dequeue(q);temp=adj_list[u];while(temp!=NULL){ v=temp->ident;

if(color[v]==0){

color[v]=1;d[v]=d[u]+1;pi[v]=u;enqueue(q,v);

}temp=temp->next;

}color[u]=2;

}}

void enqueue(int q[MAX],int m)

Page 4: ALGORITHM ANALYSIS AND DESIGN complete file

{int i=0;while(q[i]!='\0')

{i++;

}q[i]=m;

}int dequeue(int q[MAX]){

int i=0,ret;ret=q[i];while(q[i]!='\0'){

q[i]=q[i+1];i++;

}return ret;

}

Page 5: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR COUNTING INVERSION WITH O(n SQUARE) COMPLEXITY

#include<iostream.h>#include<conio.h>int count_inver(int [],int);void main(){

int a[100],n,i,c;clrscr();cout<<"Enter the total number of elements in the array:";cin>>n;cout<<"Enter the elements in the array\n";for(i=0;i<n;i++){

cin>>a[i];}c=count_inver(a,n);cout<<"\nThe total number of counting inverse is "<<c;getch();

}int count_inver(int x[],int n){

int i,j,c=0;for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)if(x[i]>x[j]){

cout<<"\n"<<x[i]<<x[j];c++;

}return c;

}

Page 6: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR COUNTING INVERSION

#include<iostream.h>#include<conio.h>int a[100],b[100],d,n,low,high;void sortandcount(int,int);void mergeandcount(int,int,int);void main(){ int i; clrscr(); cout<<"\n Enter the lengh of the series"; cin>>n; cout<<"\n Enter the series"; for(i=0;i<n;i++) cin>>a[i]; low=0; high=n-1; sortandcount(low,high); cout<<"\nThe total number of inversions is "<<d; getch(); }void sortandcount(int low,int high){ int mid; if(low<high) { mid=(low+high)/2; sortandcount(low,mid); sortandcount(mid+1,high); mergeandcount(low,mid,high); }}void mergeandcount(int low,int mid,int high){ int l,h,k,m,i;static int counter; l=low,h=low,m=mid+1; while(l<=mid&&m<=high) { if(a[l]<=a[m])

{ b[h]=a[l]; l++; } else { b[h]=a[m]; counter=counter+(mid-l+1); m++; } h++;

}

Page 7: ALGORITHM ANALYSIS AND DESIGN complete file

if(l>mid) {

for(k=m;k<=high;k++) { b[h]=a[k]; h++; }

} else {

for(k=l;k<=mid;k++) { b[h]=a[k]; h++; }

} for(i=low;i<=high;i++)

a[i]=b[i];d=counter;

}

Page 8: ALGORITHM ANALYSIS AND DESIGN complete file

// Program to implement Depth-First-Search (DFS)

#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 100

void dfs(int n);void dfs_visit(int u);

int color[MAX];int d[MAX];int f[MAX];int time;

typedef struct vert* ver;struct vert{

int ident;ver next;

} cor;ver adj_list[MAX];

void main(){

int n,i,j,k;ver temp,prev;clrscr();printf("Enter no of nodes : ");scanf("%d",&n);for(i=0;i<=n;i++)

adj_list[i]='\0';for(i=1;i<=n;i++){

prev=NULL;printf("Enter No of vertex connected to %d :",i);scanf("%d",&j);for(k=0;k<j;k++){

temp=(ver)malloc(sizeof(cor));temp->next=NULL;printf("Enter Vertex No. connected to vertex %d : ",i);scanf("%d",&temp->ident);if(prev==NULL)

adj_list[i]=temp;else

prev->next=temp;prev=temp;

}}

Page 9: ALGORITHM ANALYSIS AND DESIGN complete file

dfs(n);for(i=1;i<=n;i++){

printf("\nStarting/Ending time of Vertex %d is %d/%d",i,d[i],f[i]);}getch();

}

void dfs(int n){

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

color[i]=0;time=0;for(i=1;i<=n;i++){

if(color[i]==0)dfs_visit(i);

}}void dfs_visit(int u){

ver temp;color[u]=1;time++;d[u]=time;temp=adj_list[u];while(temp!=NULL){

if(color[temp->ident]==0)dfs_visit(temp->ident);

temp=temp->next;}color[u]=2;f[u]=++time;

}

Page 10: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR DIJKSTRA'S ALGORITHM

#include<iostream.h>#include<conio.h>int cost[50][50],b[50],dist[50],n;int find(int*,int*,int);void main(){ clrscr(); int i,j,num,v,u; cout<<"enter the no of nodes in the graph : "<<endl; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cost[i][j]=0; } cout<<"\nenter the weights of the graph : (Enter 1000 if no edge exists between 2 nodes) "; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cin>>cost[i][j]; } //print the cost matrix in matrix form for(i=1;i<=n;i++) { cout<<endl; for(j=1;j<=n;j++)

cout<<"\t"<<cost[i][j]; }

for(i=1;i<=n;i++) b[i]=0; //initialise b[] matrix to 0 cout<<"\ninput the source vertex : "; cin>>v; for(i=1;i<=n;i++) { dist[i]=cost[v][i]; b[v]=1; dist[v]=0;

} for(num=2;num<=n-1;num++) { u=find(dist,b,n); b[u]=1;

for(int w=1;w<=n;w++) { if((cost[u][w]!=1000 )&& b[w]==0) {

Page 11: ALGORITHM ANALYSIS AND DESIGN complete file

if(dist[w]> dist[u]+cost[u][w]) { dist[w]=dist[u]+cost[u][w];

} }

} }

cout<<"the distance matrix is :\n "; for(i=1;i<=n;i++) cout<<" "<<dist[i];

getch(); }

int find(int dist[],int s[],int n) { int u,i; int min=1000; for(i=1;i<=n;i++) { if(s[i]==0 && dist[i]<min) { min=dist[i]; u=i; } } return(u); }

Page 12: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR EUCLID'S ALGORITHM

#include<iostream.h>#include<conio.h>int euclid(int a,int b){if(b==0)return a;elsereturn euclid(b,a%b);}void main(){ int a,b,gcd;clrscr();cout<<"Enter 2 numbers whose gcd is to be found";cin>>a>>b;gcd=euclid(a,b);cout<<gcd;getch();}

Page 13: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR EXTENDED EUCLID'S ALGORITHM

#include<iostream.h>#include<conio.h>int * exteuclid(int a,int b){ int *z;int d,x,y;if(b==0){z[0]=a;z[1]=1;z[2]=0;cout<<"\n";cout<<a<<"\t";cout<<b<<"\t";cout<<z[0]<<"\t";cout<<z[1]<<"\t";cout<<z[2];return z;}z=exteuclid(b,a % b);d=z[0];x=z[2];y=z[1]-((a/b)*z[2]);z[0]=d;z[1]=x;z[2]=y;cout<<"\n";cout<<a<<"\t";cout<<b<<"\t";cout<<z[0]<<"\t";cout<<z[1]<<"\t";cout<<z[2];return z;}void main(){ int a,b;int * gcd;clrscr();cout<<"Enter 2 numbers whose gcd is to be found";cin>>a>>b;gcd=exteuclid(a,b);cout<<"\n";cout<< "GCD is";cout<<gcd[0];getch();}

Page 14: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR KRUSHKAL'S ALGORITHM

#include<iostream.h>#include <conio.h>#define infinity 1000int num_of_nodes;int cost[10][10];int parent[10];int solution[10][2];int min_edge(int *, int *);int find(int);void unite(int, int);void main(){int i = 0;int j = 0;int k = 0;int u = 0;int v = 0;int min_cost = 0;int cost_of_uv = 0;clrscr();cout<<"\nEnter the number of nodes in the graph: ";cin>>num_of_nodes;cout<<"\n!!!!!!!! Enter 1000 for a not connected link!!!!!!\n";for(i=0;i<num_of_nodes;i++){cost[i][i]=0;parent[i]= -1;for(j=(i+1);j<num_of_nodes;j++){cout<<"\nEnter the cost from node "<<i+1<<" to node "<<j+1<<" ";cin>>cost[i][j];cost[j][i]=cost[i][j];if(cost[i][j]>=infinity)cost[i][j]=infinity;}}i=0;while((i<num_of_nodes-1) && (cost_of_uv!=infinity)){cost_of_uv=min_edge(&u,&v);j=find(u);k=find(v);if(j!=k){solution[i][0]=u;solution[i][1]=v;min_cost=min_cost+cost_of_uv;unite(j,k);

Page 15: ALGORITHM ANALYSIS AND DESIGN complete file

i=i+1;}}if(i!=num_of_nodes-1)cout<<"\n No spanning tree ";else{cout<<"\nSpanning tree is: ";for(i=0;i<num_of_nodes-1;i++)cout<<"\n"<<solution[i][0]+1<<" "<<solution[i][1]+1;cout<<"\nCost of spanning tree: "<<min_cost;}getch();}int min_edge(int *x,int *y){int i = 0;int j = 0;int min = 1000;int u = 0;int v = 0;for(i=0;i<num_of_nodes;i++){for(j=(i+1);j<num_of_nodes;j++){if((cost[i][j] != -1)){if(min>cost[i][j]){min=cost[i][j];u=i;v=j;}}}}cost[u][v] = -1;*x=u;*y=v;return min;}int find(int i){while(parent[i]>=0)i = parent[i];return i;}void unite(int i,int j){parent[i]=j;}

Page 16: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR LONGEST COMMON SUBSEQUENCE

#include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>#define MAX 12

void LCS_Length(char[],char[],int,int);void print_LCS(char[MAX][MAX],char[],int,int);

void main(){

char X[10],Y[10],ans;int i,j;clrscr();

cout<<"\nEnter the first sequence(less then or equal to 10 charaters):";gets(X);i=strlen(X);cout<<"\nEnter the second sequence:";gets(Y);j=strlen(Y);

LCS_Length(X,Y,i,j);getch();

}void LCS_Length(char X[10],char Y[10],int m,int n){

int i,j;int C[MAX][MAX];char B[MAX][MAX];for(i=0;i<MAX;i++)

for(j=0;j<MAX;j++)B[i][j]='0';

for(i=1;i<=m;i++)C[i][0]=0;

for(j=0;j<=n;j++)C[0][j]=0;

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

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

if(X[i]==Y[j]){

C[i+1][j+1]=C[i][j]+1;B[i+1][j+1]='\\';

Page 17: ALGORITHM ANALYSIS AND DESIGN complete file

}else if(C[i][j+1]>=C[i+1][j]){

C[i+1][j+1]=C[i][j+1];B[i+1][j+1]='|';

}else{

C[i+1][j+1]=C[i+1][j];B[i+1][j+1]='-';

}}

}cout<<"The C matrix is:\n";for(i=0;i<=m;i++){

for(j=0;j<=n;j++)cout<<"\t"<<C[i][j];

cout<<"\n";}cout<<"The B matrix is:\n";for(i=1;i<=m;i++){

for(j=1;j<=n;j++)cout<<"\t"<<B[i][j];

cout<<"\n";}cout<<"\nThe combined Matrix C & B is \n";for(i=0;i<=m;i++){

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

if((i==0)||(j==0))cout<<C[i][j];

elsecout<<B[i][j]<<C[i][j];

}cout<<"\n";

}cout<<"\nThe length of the Longest Common Subsequence is \n"<<C[m][n];cout<<"\nThe Longest Sequence is : ";print_LCS(B,X,m,n);

}void print_LCS(char B[MAX][MAX],char X[10],int i,int j){

if(i==0||j==0)return;

if(B[i][j]=='\\'){

Page 18: ALGORITHM ANALYSIS AND DESIGN complete file

print_LCS(B,X,i-1,j-1);cout<<X[i-1];

}else if(B[i][j]=='|')

print_LCS(B,X,i-1,j);else

print_LCS(B,X,i,j-1);}

Page 19: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR MATRIX CHAIN MULTIPLICATION ALGORITHM

#include<iostream.h>#include<conio.h>long int M[50][50], P[50], s[50][50];void MatrixChain(int n){long int i, j, k, l, q;for(i=1;i<=n;i++)M[i][i]=0;for(l=2;l<=n;l++){for(i=1;i<=n-l+1;i++){j=i+l-1;M[i][j]=20000;for(k=i;k<j;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;s[i][j]=k;}}}}}void display(long int s[50][50],int i,int j){if(i==j)cout<<"A"<<i;else{cout<<"(";display(s,i,s[i][j]);display(s,s[i][j]+1,j);cout<<")";}}void main(){long int n, i, j;clrscr();cout << "Enter the no of Matrices : ";cin >> n;cout << "Enter the Order Array ";for(i=0;i<=n;i++){cin >> P[i];

Page 20: ALGORITHM ANALYSIS AND DESIGN complete file

}MatrixChain(n);for(i=1;i<=n;i++){for(j=1;j<=n;j++){cout << M[i][j]<<" ";}cout<<"\n";}cout<<"\n";for(i=1;i<=n;i++){for(j=1;j<=n;j++){cout << s[i][j]<<" ";}cout<<"\n";}display(s,1,n);getch();}

Page 21: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR MERGE SORT

#include<iostream.h>#include<conio.h>void mergesort(int[],int,int);void merge(int[],int,int,int);void main(){int a[10],p,q,r,i,n;clrscr();cout<<"Enter the number of elements";cin>>n;p=0;r=n-1;cout<<"Enter the array";for(i=0;i<n;i++){cin>>a[i];}mergesort(a,p,r);cout<<"The sorted array is:";for(i=0;i<n;i++){cout<<"\n"<<a[i];}getch();}

void mergesort(int a[],int p,int r){if( p < r){int q=(p+r)/2;mergesort(a,p,q);mergesort(a,q+1,r) ;merge(a,p,q,r);}}void merge(int a[],int p,int q,int r){int c[10];int i=p;int j=q+1;int k=p;while((i<=q)&&(j<=r)){if(a[i]<a[j]){c[k]=a[i];i=i+1;

Page 22: ALGORITHM ANALYSIS AND DESIGN complete file

k=k+1;}else{c[k]=a[j];j=j+1;k=k+1;}}while(i<=q){c[k] =a[i];i=i+1;k=k+1;}while(j<=r){c[k]=a[j];j=j+1;k=k+1;}int l=p;while(l<=r){a[l]=c[l];l=l+1;}}

Page 23: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR OPTIMAL BINARY SEARCH TREE

#include<iostream.h>#include<conio.h>float e[50][50], p[50], q[50],w[50][50];int root[50][50];void obst(int n,float p[],float q[]){int i, j,l,r;float t;for(i=1;i<=n+1;i++){e[i][i-1]=q[i-1];w[i][i-1]=q[i-1];}for(l=1;l<=n;l++){for(i=1;i<=n-l+1;i++){j=i+l-1;e[i][j]=10.0;w[i][j]=w[i][j-1]+p[j]+q[j];for(r=i;r<=j;r++){t=e[i][r-1] + e[r+1][j] + w[i][j];if(t<e[i][j]){e[i][j]=t;root[i][j]=r;}}}}}void main(){int n, i, j;clrscr();cout << "Enter the no of keys : ";cin >> n;cout << "Enter the array p ";for(i=1;i<=n;i++){cin >> p[i];}cout << "Enter the array q ";for(i=0;i<=n;i++){cin >> q[i];}

Page 24: ALGORITHM ANALYSIS AND DESIGN complete file

obst(n,p,q);for(i=1;i<=n+1;i++){for(j=0;j<=n;j++){cout << e[i][j]<<" ";}cout<<"\n";}cout<<"\n";for(i=1;i<=n;i++){for(j=1;j<=n;j++){cout << root[i][j]<<" ";}cout<<"\n";}getch();}

Page 25: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR PRIM'S ALGORITHM

#include<iostream.h>#include<conio.h>#define infinity 1000int num_of_nodes;int nodes[10][10][2];int near_edges[10];int temp[50];int solution[10][2];void min_edge(int *, int *);int srch_min_near_edge(void);void main(){int i = 0;int j = 0;int k = 0;int u = 0;int v = 0;int min_cost = 0;clrscr();cout<<"\nEnter the number of nodes in the graph: ";cin>>num_of_nodes;cout<<"\n!!!!!!!! Enter 1000 for a not connected link!!!!!!\n";for(i=0;i<num_of_nodes;i++){nodes[i][i][0]=0;nodes[i][i][1]=1;for(j=(i+1);j<num_of_nodes;j++){cout<<"\nEnter the cost from node "<<i+1<<" to node "<<j+1<<" ";cin>>nodes[i][j][0];nodes[j][i][0]=nodes[i][j][0];if(nodes[i][j][0]>=infinity){nodes[i][j][1]=1;nodes[j][i][1]=1;}else{nodes[i][j][1]=0;nodes[j][i][1]=1;}}}min_edge(&u, &v);min_cost=nodes[u][v][0];solution[0][0]=u;solution[0][1]=v;for(i=0;i<num_of_nodes;i++)

Page 26: ALGORITHM ANALYSIS AND DESIGN complete file

{if(nodes[i][u][0] < nodes[i][v][0])near_edges[i] = u;else{if((nodes[i][u][0]==infinity) && (nodes[i][v][0]==infinity))near_edges[i]=infinity;elsenear_edges[i]=v;}}near_edges[u] = -1;near_edges[v] = -1;for(i = 1; i < num_of_nodes-1; i++){j=srch_min_near_edge();solution[i][0]=j;solution[i][1]=near_edges[j];min_cost=min_cost+nodes[j][near_edges[j]][0];near_edges[j]=-1;for(k=0;k<num_of_nodes;k++){if((near_edges[k]!=infinity)){if((near_edges[k] != -1) && (nodes[k][near_edges[k]][0]>nodes[k][j][0]))near_edges[k]=j;}elsenear_edges[k] = j;}}cout<<"\nMinimum cost spanning tree is: \n";for(i=0;i<num_of_nodes-1;i++)cout<<solution[i][0]+1<<" "<<solution[i][1]+1<<"\n";cout<<"\nCost of spanning tree: "<<min_cost;getch();}void min_edge(int *x, int *y){int i = 0;int j = 0;int min = 1000;int u = 0;int v = 0;for(i=0;i<num_of_nodes;i++){for(j=(i+1);j<num_of_nodes;j++){if((nodes[i][j][1]==0) && (nodes[i][j][0]!=infinity)){

Page 27: ALGORITHM ANALYSIS AND DESIGN complete file

if(min>nodes[i][j][0]){min=nodes[i][j][0];u=i;v=j;}}}}nodes[u][v][1]=1;*x=u;*y=v;}int srch_min_near_edge(void){int edge;int min=1000;int i=0;for(i=0;i<num_of_nodes;i++){if((near_edges[i] != -1) && (near_edges[i] != infinity)){if((min>nodes[i][near_edges[i]][0]) && (nodes[i][near_edges[i]][0]!=infinity)){min=nodes[i][near_edges[i]][0];edge=i;}}}return edge;}

Page 28: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR STRASSEN'S MATRIX MULTIPLICATION

#include<iostream.h>#include<conio.h>void main(){int a[2][2],b[2][2],c[2][2],i,j;int p1,p2,p3,p4,p5,p6,p7;clrscr();cout << "Enter the first matrix : ";for(i=1;i<=2;i++)for(j=1;j<=2;j++)cin >> a[i][j];cout << "Enter the second matrix : ";for(i=1;i<=2;i++)for(j=1;j<=2;j++)cin >> b[i][j];p1=a[1][1]*(b[1][2]-b[2][2]);p2=(a[1][1]+a[1][2])*b[2][2];p3=(a[2][1]+a[2][2])*b[1][1];p4=a[2][2]*(b[2][1]-b[1][1]);p5=(a[1][1]+a[2][2])*(b[1][1]+b[2][2]);p6=(a[1][2]-a[2][2])*(b[2][1]+b[2][2]);p7=(a[1][1]-a[2][1])*(b[1][1]+b[1][2]);c[1][1]=p5+p4-p2+p6;c[1][2]=p1+p2;c[2][1]=p3+p4;c[2][2]=p5+p1-p3-p7;for(i=1;i<=2;i++){for(j=1;j<=2;j++){cout<< c[i][j]<<" ";}cout<<"\n";}getch();}

Page 29: ALGORITHM ANALYSIS AND DESIGN complete file

//PROGRAM FOR TASK SCHEDULING PROBLEM

#include<iostream.h>#include<conio.h>struct task{ int deadl; int cost;}task[20],temp;void main(){ int i,j,n,p[20],count=0,c=0,ans[20],ex[10],penalty=0,temp1=1,x=0; clrscr(); cout<<"Enter the number of tasks"; cin>>n; cout<<"\nEnter dead line and cost of each task\n"

" dead line cost\n";

for(i=0;i<n;i++) { cin>>task[i].deadl>>task[i].cost; p[i]=-1; } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(task[i].cost<task[j].cost)

{ temp=task[i]; task[i]=task[j]; task[j]=temp;}

} } for(i=0;i<n;i++) { if(p[task[i].deadl]==-1)

{ p[task[i].deadl]=0;ans[count++]=i;}

else{ c=task[i].deadl; temp1=1; while(c>0) { if(p[c]==-1) {temp1=0; p[c]=0; ans[count++]=i; break; } c--;

Page 30: ALGORITHM ANALYSIS AND DESIGN complete file

} if(temp1==1) { ex[x++]=i; penalty=penalty+task[i].cost; }}

}

for(i=0;i<count;i++) { for(j=i+1;j<count;j++) {

if(task[ans[i]].deadl>=task[ans[j]].deadl) {temp1=ans[i]; ans[i]=ans[j]; ans[j]=temp1; }

} } cout<<"\n The scheduled tasks are \n "; for(i=0;i<count;i++) cout<<" T"<<ans[i]+1; for(i=0;i<x;i++) cout<<" T"<<ex[i]+1;

cout<<"\n\nTotal penalty is"; cout<<penalty;

getch(); }

Page 31: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the number of activities11

Enter the start point and end points of the activities1435065738596108118122131214

The set of activities selected are A1 A4 A8 A11

Page 32: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the total number of elements in the array:5Enter the elements in the array24135

214143The total number of counting inverse is 3

Page 33: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

enter the no of nodes in the graph :6

enter the weights of the graph : (Enter 1000 if no edge exists between 2 nodes)0504510100010001000010151000100010001000010003010002010001000015100010002035100001000100010001000100030

0 50 45 10 1000 1000 1000 0 10 15 1000 1000 1000 1000 0 1000 30 1000 20 1000 1000 0 15 1000 1000 20 35 1000 0 1000 1000 1000 1000 1000 3 0

Page 34: ALGORITHM ANALYSIS AND DESIGN complete file

input the source vertex : 1the distance matrix is : 0 45 45 10 25 1000

Page 35: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter 2 numbers whose gcd is to be found2193

Page 36: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter 2 numbers whose gcd is to be found9978

3 0 3 1 06 3 3 0 115 6 3 1 -221 15 3 -2 378 21 3 3 -1199 78 3 -11 14GCD is3

Page 37: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the lengh of the series5

Enter the series24135

The total number of inversions is 3

Page 38: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the number of nodes in the graph: 5

!!!!!!!! Enter 1000 for a not connected link!!!!!!

Enter the cost from node 1 to node 2 1

Enter the cost from node 1 to node 3 2

Enter the cost from node 1 to node 4 2

Enter the cost from node 1 to node 5 1000

Enter the cost from node 2 to node 3 1

Enter the cost from node 2 to node 4 4

Enter the cost from node 2 to node 5 1000

Enter the cost from node 3 to node 4 6

Enter the cost from node 3 to node 5 3

Enter the cost from node 4 to node 5 5

Spanning tree is:1 22 31 43 5Cost of spanning tree: 7

Page 39: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the first sequence(less then or equal to 10 charaters):abcbdab

Enter the second sequence:bdcabaThe C matrix is: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 2 2 0 1 1 2 2 2 2 0 1 1 2 2 3 3 0 1 2 2 2 3 3 0 1 2 2 3 3 4 0 1 2 2 3 4 4The B matrix is: | | | \ - \ \ - - | \ - | | \ - | | \ | | | \ - | \ | | | | | | | \ | \ \ | | | \ |

The combined Matrix C & B is00000000|0|0|0\1-1\10\1-1-1|1\2-20|1|1\2-2|2|20\1|1|2|2\3-30|1\2|2|2|3|30|1|2|2\3|3\40\1|2|2|3\4|4

The length of the Longest Common Subsequence is4The Longest Sequence is : bcba

Page 40: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the no of Matrices : 6Enter the Order Array 30351551020250 15750 7875 9375 11875 151250 0 2625 4375 7125 105000 0 0 750 2500 53750 0 0 0 1000 35000 0 0 0 0 50000 0 0 0 0 0

0 1 1 3 3 30 0 2 3 3 30 0 0 3 3 30 0 0 0 4 50 0 0 0 0 50 0 0 0 0 0((A1(A2A3))((A4A5)A6))

Page 41: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the number of elements5Enter the array2112312The sorted array is:1231221

Page 42: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the no of keys : 5Enter the array p .15.10.05.10.20Enter the array q .05.10.05.05.05.100.05 0.45 0.9 1.25 1.75 2.750 0.1 0.4 0.7 1.2 20 0 0.05 0.25 0.6 1.30 0 0 0.05 0.3 0.90 0 0 0 0.05 0.50 0 0 0 0 0.1

1 1 2 2 20 2 2 2 40 0 3 4 50 0 0 4 50 0 0 0 5

Page 43: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the number of nodes in the graph: 5

!!!!!!!! Enter 1000 for a not connected link!!!!!!

Enter the cost from node 1 to node 2 1

Enter the cost from node 1 to node 3 2

Enter the cost from node 1 to node 4 6

Enter the cost from node 1 to node 5 3

Enter the cost from node 2 to node 3 1

Enter the cost from node 2 to node 4 4

Enter the cost from node 2 to node 5 1000

Enter the cost from node 3 to node 4 2

Enter the cost from node 3 to node 5 1000

Enter the cost from node 4 to node 5 5

Minimum cost spanning tree is:1 23 24 35 1

Cost of spanning tree: 7

Page 44: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the first matrix : 1251Enter the second matrix : 236714 1716 22

Page 45: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter the number of tasks7

Enter dead line and cost of each task dead line cost470260450340130420610

The scheduled tasks are T2 T4 T3 T1 T7 T5 T6

Total penalty is50

Page 46: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter no of nodes : 5Enter No of vertex connected to 1 :3Enter Vertex No. connected to vertex 1 : 2Enter Vertex No. connected to vertex 1 : 3Enter Vertex No. connected to vertex 1 : 4Enter No of vertex connected to 2 :3Enter Vertex No. connected to vertex 2 : 1Enter Vertex No. connected to vertex 2 : 3Enter Vertex No. connected to vertex 2 : 4Enter No of vertex connected to 3 :3Enter Vertex No. connected to vertex 3 : 2Enter Vertex No. connected to vertex 3 : 4Enter Vertex No. connected to vertex 3 : 5Enter No of vertex connected to 4 :4Enter Vertex No. connected to vertex 4 : 1Enter Vertex No. connected to vertex 4 : 2Enter Vertex No. connected to vertex 4 : 3Enter Vertex No. connected to vertex 4 : 5Enter No of vertex connected to 5 :2Enter Vertex No. connected to vertex 5 : 3Enter Vertex No. connected to vertex 5 : 4

Enter the starting point of graph : 1

The Distance from source(1) to 1 is 0The previous vertex of 1 is 0The Distance from source(1) to 2 is 1The previous vertex of 2 is 1The Distance from source(1) to 3 is 1The previous vertex of 3 is 1The Distance from source(1) to 4 is 1The previous vertex of 4 is 1The Distance from source(1) to 5 is 2The previous vertex of 5 is 3

Page 47: ALGORITHM ANALYSIS AND DESIGN complete file

OUTPUT

Enter no of nodes : 5Enter No of vertex connected to 1 :3Enter Vertex No. connected to vertex 1 : 2Enter Vertex No. connected to vertex 1 : 3Enter Vertex No. connected to vertex 1 : 4Enter No of vertex connected to 2 :3Enter Vertex No. connected to vertex 2 : 1Enter Vertex No. connected to vertex 2 : 3Enter Vertex No. connected to vertex 2 : 4Enter No of vertex connected to 3 :3Enter Vertex No. connected to vertex 3 : 1Enter Vertex No. connected to vertex 3 : 4Enter Vertex No. connected to vertex 3 : 5Enter No of vertex connected to 4 :4Enter Vertex No. connected to vertex 4 : 1Enter Vertex No. connected to vertex 4 : 2Enter Vertex No. connected to vertex 4 : 3Enter Vertex No. connected to vertex 4 : 5Enter No of vertex connected to 5 :2Enter Vertex No. connected to vertex 5 : 3Enter Vertex No. connected to vertex 5 : 4

Starting/Ending time of Vertex 1 is 1/10Starting/Ending time of Vertex 2 is 2/9Starting/Ending time of Vertex 3 is 3/8Starting/Ending time of Vertex 4 is 4/7Starting/Ending time of Vertex 5 is 5/6

Page 48: ALGORITHM ANALYSIS AND DESIGN complete file

INDEX

S.No. Program Date Signature

Page 49: ALGORITHM ANALYSIS AND DESIGN complete file
Page 50: ALGORITHM ANALYSIS AND DESIGN complete file