This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Algorithm Analysis And Design Complete File as PDF for free.
University School of Information Technology, G.G.S.I.PU.
ALGORITHM ANALYSIS AND DESIGN FILE
Submitted By : ANUP SINGH KUSHWAHA
Enrol
no: 0111645308
// Program to implement Breadth-First-Search (BFS) #include<stdio.h> #include #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; }
} 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)
{ 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; }
//PROGRAM FOR COUNTING INVERSION WITH O(n SQUARE) COMPLEXITY #include #include 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>a[i]; } c=count_inver(a,n); cout<<"\nThe total number of counting inverse is "<x[j]) { cout<<"\n"<<x[i]<<x[j]; c++; } return c; }
//PROGRAM FOR COUNTING INVERSION #include #include 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>a[i]; low=0; high=n-1; sortandcount(low,high); cout<<"\nThe total number of inversions is "<
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; }
// Program to implement Depth-First-Search (DFS) #include<stdio.h> #include #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; } }
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; }
//PROGRAM FOR DIJKSTRA'S ALGORITHM #include #include 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"<>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) {