You are on page 1of 18

Digital Assignment-4

Name: Pratishtha Gaur


Reg no:19BCE0104
Que1) Menu driven C program to implement depth first search and
breadth first search graph traversal algorithms
Pseudocode:
bfs(starting vertex, number of vertex)
{add(starting vertex)
Change visited to 1
temp=delete()
if(temp!=0) print temp
while(temp!=0)
{for i=1 to n
{if((arr[temp][i]!=0)and(visited[i]==0))// if vertex not visited
{add(i)
Change to visited}}
temp=delete()
if(temp!=0) print temp}
for i=1 to n
{if(i not visited) bfs(i,n)//repeat}}

dfs(starting vertex ,number of vertices)


{push(stating vertex)
visited[s]=1
temp=pop()
if temp!=0 print temp
while(temp not equal to 0)
{for i=1 to n
{if((arr[temp][i]!=0) and (visited[i]==0))// if not visited
{push(i)
Change to visited }
temp=pop()
if(temp!=0) print temp}
for i=1 to n
{if(visited[i]=0) dfs(i,n)}}
Code:
#include<stdio.h>
int top=-1,q[20],stack[20],front=-1,rear=-1,arr[20][20],visited[20]={0};
int main()
{int i,j,n,ch,s;

printf("Enter the Number of Vertices : ");

scanf("%d",&n);

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

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

{ printf("Enter 1 if %d has a node with %d else 0 : ",i,j)


scanf("%d",&arr[i][j]); } }

printf("Menu\n1.BFS \n2.DFS \n3.Exit\n");

while(1)

{ printf("Enter choice : ");

scanf("%d",&ch);

If(ch==1||ch==2)

{ printf("Enter stating vertex : ");

scanf("%d",&s);}

else
break;

switch(ch)

{case 1:bfs(s,n); break;

case 2:dfs(s,n); break;

case 3:exit(0); break;}}

for(i=0;i<=n;i++){visited[i]=0;}}

void add(int item)

{ if(rear==19)

printf("Queue full");

else

{ if(rear==-1)

{ q[++rear] = item;

front++;}

else

q[++rear]=item;}}

int delete()

{ int k;

if ((front>rear)||(front==-1))

return (0);

else

{ k=q[front++];

return(k);}}

void push( int item )

{ if ( top == 19 )

printf( "Stack overflow " );


else

stack[ ++top ] = item;

int pop()

{ int k;

if ( top == -1 )

return ( 0 );

else

{ k = stack[ top-- ];

return ( k );}}

bfs(int s,int n)

{int i,p;

add(s);

visited[s]=1;

p=delete();

if(p!=0) printf("%d\n",p);

while(p!=0)

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

{ if((arr[p][i]!=0)&&(visited[i]==0))

{ add(i);

visited[i]=1; } }

p=delete();

if(p!=0)

printf("%d\n",p); }

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

if(visited[i]==0) bfs(i,n); }
dfs(int s,int n)

{int k,i;

push(s);

visited[s]=1;

k=pop();

if(k!=0)

printf("%d\n",k);

while(k!=0)

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

{ if((arr[k][i]!=0)&&(visited[i]==0))

{ push(i);

visited[i]=1; }

k=pop();

if(k!=0)

printf("%d\n",&k); }

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

if(visited[i]==0) dfs(i,n); }

Output:
Que 2) Implement Dijikstra’s algorithm to find shortest path from source
node to all other nodes
Pseudocode:
dijkstra(G,S)
{for V[i] in G
distance[V]<-infinite
previous[V]<-NULL
if V[i]!=S
push v[i] to q
distance[S]=0
while Q!=NULL
U=min(Q)
for visited=0 of U
tempDistance=distance[U]+edge_weight(U,V)
if tempDistance=distance[V]
distance[V]=tempDistance
previous[V]=U
return distance[],previous[]
}
Code:
#include<stdio.h>
#include<conio.h>
#define infinity 1000
#define max 10
void dijkstra(int G[max][max],int n,int startnode)
{ int cost[max][max],distance[max],pred[max];
int visited[max],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{ distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0; }
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{ mindistance=infinity;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{ mindistance=distance[i];
nextnode=i;}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{ distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;}
count++;}
for(i=0;i<n;i++)
if(i!=startnode)
{ printf("\nDistance of node %d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do {
j=pred[j];
printf("<-%d",j);}
while(j!=startnode);}}
int main()
{ int G[max][max], i, j, n, u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
Output:

Que3) Menu driven C program to implement insertion, selection and


bubble sort
Pseudocode:
insertion sort(array,n)
{ for i=1 to n
min=arr[i]
j=i-1;
while(min<arr[j] and j>=0)
arr[j+1]=arr[j]
j=j-1
arr[j+1]=min
display(array,n)
}
bubble sort(array n)
{for i=0 to n
for j=0 to n-1
if(arr[j]>arr[j+1])
temp=arr[j] //swap
arr[j]=arr[j+1]
arr[j+1]=temp
display(array,n)
}
selection sort(array ,n)
{for i=0 to n-1
for j=i+1 to n
if(arr[i]>arr[j])
temp=arr[i] //swap
arr[i]=arr[j]
arr[j]=temp
display(arr,n)
}

Code:
#include<stdio.h>
#include<stdlib.h>
void display(int arr[],int n)
{ for(int i=0;i<n;i++)
printf(" %d ",arr[i]);
}
void bubble_sort(int arr[],int n)
{ int i,j,temp;
for(i=0;i<n;i++)
{ for(j=0;j<n-i-1;j++)
{ if(arr[j]>arr[j+1])
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;} } }
printf("After Bubble sort Elements are : ");
display(arr,n);
}
void selection_sort(int arr[],int n)
{ int i,j,temp;
for(i=0;i<n-1;i++)
{ for(j=i+1;j<n;j++)
{ if(arr[i]>arr[j])
{ temp=arr[i];
arr[i]=arr[j];
arr[j]=temp; } } }
printf("After Selection sort Elements are : ");
display(arr,n);}
void insertion_sort(int arr[],int n)
{ int i,j,min;
for(i=1;i<n;i++)
{ min=arr[i];
j=i-1;
while(min<arr[j] && j>=0)
{ arr[j+1]=arr[j];
j=j-1;}
arr[j+1]=min;
}
printf("After Insertion sort Elements are : ");
display(arr,n);
}
int main()
{ int n, ch, i;
char ch[20];
printf("Enter no. of elements u want to sort : ");
scanf("%d",&n);
int arr[n];
printf("Enter Element :);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Please select any option Given Below for Sorting : \n");
printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion
Sort\n4.Exit\n");
while(1)
{ printf("\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{ case 1:bubble_sort(arr,n);
break;
case 2:selection_sort(arr,n);
break;
case 3:insertion_sort(arr,n);
break;
case 4:exit(0);
return 0;
default: printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}
Output:

Que4) Menu driven C program to implement quick sort and merge sort
using divide and conquer method.
Pseudocode:
quicksort(a[ ],low,high)
{if(low<high)
pivot=a[low];
i=low+1
j=high;
while(1)
while(pivot>a[i] andi<=high)
i++
while(pivot<a[j]&&j>=low)
j--;
if(i<j)
temp=a[i] //swap
a[i]=a[j]
a[j]=temp
else break
a[low]=a[j]
a[j]=pivot
quicksort(a,low,j-1)
quicksort(a,j+1,high)
}
mergesort(a[],low,high)
{if a[high]>a[low]
mid=high+low/2
mergesort(a[],low,mid)
mergesort(a[],mid+1,high)
merge(a[],low,mid,high)
}
merge(a[],low,mid,high)
{i=low
j=mid+1
k=low
while((i<=mid) and (j<=high))
if(a[i]>=a[j])
temp[k++]=a[j++]
else
temp[k++]=a[i++]
while(i<=mid)
temp[k++]=a[i++]
while(j<=high)
temp[k++]=a[j++]
for i=low to high
a[i]=t[i]
}
Code:
#include<stdio.h>
void quicksort(int a[ ],int low,int high)
{ int pivot,t,i,j;
if(low<high)
{ pivot=a[low];
i=low+1;
j=high;
while(1)
{ while(pivot>a[i]&&i<=high)
i++;
while(pivot<a[j]&&j>=low)
j--;
if(i<j)
{ t=a[i];
a[i]=a[j];
a[j]=t;}
else
break; }
a[low]=a[j];
a[j]=pivot;
quicksort(a,low,j-1);
quicksort(a,j+1,high); } }
void mergesort(int a[],int low,int mid,int high)
{ int t[50],i,j,k;
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{ if(a[i]>=a[j])
t[k++]=a[j++];
else
t[k++]=a[i++]; }
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=t[i]; }
void msortdiv(int a[],int low,int high)
{ int mid;
if(low!=high)
{ mid=((low+high)/2);
msortdiv(a,low,mid);
msortdiv(a,mid+1,high);
mergesort(a,low,mid,high); } }
void main( )
{ int low, high, pivot, t, n, i, j, a[10],ch;
printf("How many elements you want to sort ? ");
scanf("%d",&n);
printf("Enter elements for an array:");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Menu\n1.Using quick sort\n2.Using Merge sort\n3.Exit");
while(1)
{ printf("\nSelect option from 1-3:");
scanf("%d",&ch);
switch(ch)
{ case 1:low=0;
high=n-1;
quicksort(a,low,high);
printf("\After Sorting the elements are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
break;
case 2:msortdiv(a,0,n-1);
printf("\nAfter Sorting the elements are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
break;
case 3:exit(0);
break; } } }
Output:

You might also like