0% found this document useful (0 votes)
67 views14 pages

Wa0002

The document contains code snippets for 10 different algorithms: 1) Selection sort 2) Bubble sort 3) Brute force string matching 4) Insertion sort 5) DFS connectivity 6) DFS reachability 7) BFS traversal 8) Binomial coefficients 9) Warshall's algorithm 10) Floyd's algorithm for finding shortest paths in a weighted graph. For each algorithm, it provides the code to implement the algorithm and test it on sample input data.

Uploaded by

Kishan Bhat K
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)
67 views14 pages

Wa0002

The document contains code snippets for 10 different algorithms: 1) Selection sort 2) Bubble sort 3) Brute force string matching 4) Insertion sort 5) DFS connectivity 6) DFS reachability 7) BFS traversal 8) Binomial coefficients 9) Warshall's algorithm 10) Floyd's algorithm for finding shortest paths in a weighted graph. For each algorithm, it provides the code to implement the algorithm and test it on sample input data.

Uploaded by

Kishan Bhat K
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/ 14

Part - A

1.Selection Sort
#include <stdio.h>
#include <stdlib.h>
#include<sys/time.h>
void selectionsort(int a[],int n)
{
int i,j,min,temp;
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int a[100],n,i;
struct timeval tim;
double dtime1,dtime2;
printf("Enter size : ");
scanf("%d",&n);
printf("Enter %d elements :\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
gettimeofday(&tim,NULL);
dtime1=tim.tv_sec+(tim.tv_usec/1000000.0);
selectionsort(a,n);
gettimeofday(&tim,NULL);
dtime2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("After sorting :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("Time of sorting : %lf seconds\n",dtime2-dtime1);
return 0;
}
2.Bubble Sort
#include <stdio.h>
#include <stdlib.h>
#include<sys/time.h>
void bubblesort(int a[],int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

}
}
int main()
{
int a[100],n,i;
struct timeval time;
double dtime1,dtime2;
printf("Enter size : ");
scanf("%d",&n);
printf("Enter elements :\n");
for(i=0;i<n;i++)
a[i]=rand()%100;
gettimeofday(&time,NULL);
dtime1=time.tv_sec+(time.tv_usec/1000000.0);
bubblesort(a,n);
gettimeofday(&time,NULL);
dtime2=time.tv_sec+(time.tv_usec/1000000.0);
printf("After sorting :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("Time of sorting : %lf seconds\n",dtime2-dtime1);
return 0;
}
3.Bruteforce Stringmatching
#include<stdio.h>
#include<string.h>
int bruteforce(char t[100],char p[50],int n,int m)
{
int i,j;
for(i=0;i<=n-m;i++)
{
j=0;
while(j<m && p[j]==t[i+j])
{
j=j+1;
}
if(j==m)
return i;
}
return -1;
}
int main()
{
char t[100],p[50];
int m,n,pos;
printf("Enter the text :\n");
gets(t);
n=strlen(t);
printf("Enter the pattern : ");
gets(p);
m=strlen(p);
pos=bruteforce(t,p,n,m);
if(pos==-1)
{
printf("The string matching is unsuccessful\n");
}
else
{
printf("String found in position %d\n",pos+1);
}
return 0;
}
4.Insertion Sort
#include<stdio.h>
#include<stdlib.h>
void insertionsort(int a[],int n)
{
int i,j,v;
for(i=1;i<n;i++)
{
v=a[i];
j=i-1;
while(j>=0 && a[j]>v)
{
a[j+1]=a[j];
j--;
}
a[j+1]=v;
}
}
int main()
{
int i,n,a[100];
printf("Enter size : ");
scanf("%d",&n);
printf("Enter %d elements :\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertionsort(a,n);
printf("After sorting :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
5.DFS Connectivity
#include<stdio.h>
#include<conio.h>
int A[20][20],visited[20],con=0,n;
void dfs(int v)
{
int w,count=0;
count++;
visited[v]=count;
for(w=1;w<=n;w++)
{
if(A[v][w] && visited[w]==0)
{
dfs(w);
}
}
}
int main()
{
int i,j,k,e,v1,v2,flag;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
A[i][j]=0;
}
}
printf("Enter the edges of the graph one by one :\n");
for(i=1;i<=e;i++)
{
scanf("%d %d",&v1,&v2);
A[v1][v2]=1;
}
printf("Adjacency matrix :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
for(k=1;k<=n;k++)
{
flag=0;
for(i=1;i<=n;i++)
{
visited[i]=0;
}
dfs(k);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
{
flag=1;
break;
}
if(flag==0)
con=con+1;
}
}
if(con==n)
printf("The graph is strongly connected\n");
else if(con==0)
printf("The graph is not connected\n");
else
printf("The graph is weakly connected\n");
return 0;
}
6.DFS Reachability
#include<stdio.h>
#include<conio.h>
int A[20][20],visited[20],con=0,n;
void dfs(int v)
{
int w,count=0;
count++;
visited[v]=count;
printf("Reached vertex %d\n",v);
for(w=1;w<=n;w++)
{
if(A[v][w] && visited[w]==0)
{
dfs(w);
}
}
}
int main()
{
int i,j,v,flag=0,e,v1,v2;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
A[i][j]=0;
}
}
printf("Enter the edges of the graph one by one :\n");
for(i=1;i<=e;i++)
{
scanf("%d %d",&v1,&v2);
A[v1][v2]=A[v2][v1]=1;
}
printf("Adjacency matrix :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
printf("Enter a vertex : ");
scanf("%d",&v);
dfs(v);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
{
printf("%d not visited\n",i);
flag=1;
}
}
if(flag==0)
printf("All nodes are visited\n");
else
printf("All nodes are not visited\n");
return 0;
}
7.BFS Traversal
#include<stdio.h>
#include<stdlib.h>
int q[50],front=1,rear=0,n, count=0,i,j,A[20][20],visited[20];
void bfs(int v)
{
int q[50],front=1,rear=0,w;
count=count+1;
visited[v]=count;
printf("%d is visited\n",v);
q[++rear]=v;
while(front<=rear)
{
v=q[front];
for(w=1;w<=n;w++)
{
if(visited[w]==0&&A[v][w]==1)
{
count++;
visited[w]=count;
printf("%d is visited\n",w);
q[++rear]=w;
}
}
front++;
}
}
int main()
{
int v1,v2,e,k;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
A[i][j]=0;
}
}
printf("Enter the edges of the graph one by one:\n");
for(i=1;i<=e;i++)
{
scanf("%d %d",&v1,&v2);
A[v1][v2]=A[v2][v1]=1;
}
printf("Adjacency matrix :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
for(k=1;k<=n;k++)
{
if(visited[k]==0)
bfs(k);
}
return 0;
}
8.Binomial Co-efficients
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int c[20][20];
int min(int i,int k)
{
if (i<k )
return i;
else
return k ;
}
void binomial(int n,int r)
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=min(i,r);j++)
{
if(j==0||j==i)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
}
int main()
{
int n,r,i,j;
printf("Enter n : ");
scanf("%d",&n);
printf("Enter r : ");
scanf("%d",&r);
if(n<r)
printf("Invalid input: n cannot be less than r\n");
else if(r<0)
printf("Invalid input: r cannot be less than 0\n");
else
{
binomial(n,r);
printf("Computed matrix :\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=min(i,r);j++)
printf("%d\t",c[i][j]);
printf("\n");
}
printf("Binomial coefficient c[%d,%d] = %d\n",n,r,c[n][r]);
}
return 0;
}
9.Warshall’s Algorithm
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int c[20][20];
int min(int i,int k)
{
if (i<k )
return i;
else
return k ;
}
void binomial(int n,int r)
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=min(i,r);j++)
{
if(j==0||j==i)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
}
int main()
{
int n,r,i,j;
printf("Enter n : ");
scanf("%d",&n);
printf("Enter r : ");
scanf("%d",&r);
if(n<r)
printf("Invalid input: n cannot be less than r\n");
else if(r<0)
printf("Invalid input: r cannot be less than 0\n");
else
{
binomial(n,r);
printf("Computed matrix :\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=min(i,r);j++)
printf("%d\t",c[i][j]);
printf("\n");
}
printf("Binomial coefficient c[%d,%d] = %d\n",n,r,c[n][r]);
}
return 0;
}
10.Floyd’s Algorithm
#include<stdio.h>
#include<stdlib.h>
int W[10][10],n,e,D[10][10];
int min(int i,int k)
{
if (i<k)
return i;
else
return k ;
}
void Floyd_AllPair()
{
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
D[i][j]=W[i][j];
for(k=1;k<=n;k++)
{
printf("D(%d) = \n",k);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
D[i][j]=min(D[i][j], D[i][k]+D[k][j]);
printf("%d\t",D[i][j]);
}
printf("\n");
}
}
}
int main()
{
int v1,v2,i,j,w;
printf("Enter no. of vertices : ");
scanf("%d",&n);
printf("Enter no. of edges : ");
scanf("%d",&e);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
W[i][j]=0;
else
W[i][j]=9999;
}
printf("Enter the Edges with distance one by one\n");
for(i=1;i<=e;i++)
{
printf("Edge %d: ",i);
scanf("%d%d%d",&v1,&v2,&w);
W[v1][v2]=w;
}
printf("The given digraph D(0) =\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",W[i][j]);
printf("\n");
}
Floyd_AllPair();
printf("The All Pair Shortest Path for the given digraph is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",D[i][j]);
printf("\n");
}
return 0;
}

You might also like