Saturday, May 8, 2021

50.7. C Programming code for Depth First Search (DFS) Algorithm.

#include<stdio.h>

void dfs(int source)
{
int curr,i = 1;
vis[source] = 1;
push(source);
printf("%d ",source); 
while(!empty())
{
curr = top();
while(i<=g.n)
{
if(vis[i]==0 && g.adj_mat[curr][i]==1)
{
curr = i; 
vis[curr] = 1;
push(curr);
printf("%d ",curr); 
i=1;
continue;
} 
i++;
} 
i = pop();
i++;
}
}

50.6. C Programming code for Breadth First Search(BFS) Algorithm.

#include<stdio.h>
void bfs()
{
int curr = 1,i = 2;
vis[curr] = 1;
init();
enqueue(curr);
while(!isEmpty())
{
curr = dequeue();
printf("%d ",curr);
i = 1;
while(i<=n)
{
if(vis[i]==0 && adj_mat[curr][i]==1)
{
vis[i] = 1;
enqueue(i);
}
i++;
}
}
}

50.5. C Programming code for Prim's Algorithm.

#include<stdio.h>
void prim()
{
int curr,prev,total_min_cost = 0,i,j,min_cost;
curr = 1;
vis[1] = 1;
for(j=2;j<=n;j++)                          
{
cost[j] = cost_mat[1][j];
from[j]=1;
vis[j]=0;
}
for(i=1;i<n;i++)
{
min_cost = 999;
for(j=2;j<=n;j++)                          
{
if(vis[j]==0 && cost[j]<min_cost)
{
min_cost = cost[j];
curr = j;
}
}
prev= from[curr];
printf("\nEdge %d---%d  Cost = %d",prev,curr,min_cost);
total_min_cost = total_min_cost + min_cost;
vis[curr]=1;
for(j=2;j<=n;j++)                          
{
if(vis[j]==0 && cost_mat[curr][j] < cost[j])
{
from[j] = curr;
cost[j] = cost_mat[curr][j];
}
}
}
printf("\n\nMin cost = %d",total_min_cost);
}

50.4. C Programming code for Djikstra Algorithm.

#include<stdio.h>

void djikstra()
{
int start,dest = n,min_cost,curr,i,j;
printf("\nEnter starting vertex ");
scanf("%d",&start);
vis[start]=1;
for(i=1;i<=n;i++)
{
if(i!=start)
{
vis[i]=0;
from[i]=start;
cost[i]=cost_mat[start][i];
}
}
for(i=1;i<n;i++)
{
min_cost = 999;
 
for(j=1;j<=n;j++)     
{
if(vis[j]==0 && cost[j]<min_cost)
{
min_cost = cost[j];
curr = j;
}

vis[curr]=1;
if(curr==dest)
break;
for(j=1;j<=n;j++)                          
{
if(vis[j]==0 && (cost_mat[curr][j]+min_cost) < cost[j])
{
from[j] = curr;
cost[j] = cost_mat[curr][j]+min_cost;
}
}
}
printpath(start,dest);
printf("\nMin cost = %d",cost[dest]);
}
void printpath(int start,int dest)
{
if(dest==start)
{
printf("%d  ",start);
return;
}
printpath(start,from[dest]);
printf("%d  ",dest);

}

50.3. C Programming code for Quick sort Algorithm.

#include <stdio.h>
void quicksort(int arr[],int low,int high)         
main()
{
int m;
if(low<high)
{
m=partition(arr,low,high);
quicksort(arr,low,m-1);
quicksort(arr,m+1,high);
}
}
int partition(int arr[],int low,int high)
{
int pivot=arr[low],i=low,j=high;
while(i<j)
{
while((arr[i]<=pivot)&&(i<=high))
i++;
while(arr[j]>pivot)
j--;
if(i<j)
swap(arr,i,j);               
}
swap(arr,low,j);       
return j;
}

50.2. C Programming code for Binary Search Algorithm.

#include<stdio.h>
void binsearch(int[],int);
int main()
{
int a[15],pos,n,i;
printf("\n Enter no of elements ");
scanf("%d",&n); 
printf("\n Enter elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
binsearch(a,n);
return 0;
}
void binsearch(int a[],int n)
{
int low = 0 , high = n-1 , key , mid = (low + high)/2 ,comp = 1;
printf("\n Enter key to be searched ");
fflush(stdin);
scanf("%d",&key);
while(low<=high && key != a[mid])
{
comp++;
if(key<a[mid])
high = mid - 1;
else
low = mid + 1; 
mid = (low + high)/2;

if(a[mid]==key)
{
printf("\n Element found at position = %d",mid + 1);
printf("\n Comparisons = %d",comp);
}
else
printf("\n Element not found");

50.1. C Programming code for Linear Search Algorithm.

#include<stdio.h>
void linsearch(int[],int);
int main()
{
int a[15],n,i;
printf("\n Enter no of elements ");
scanf("%d",&n);
printf("\n Enter elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]); 
linsearch(a,n);
return 0;
}
void linsearch(int a[],int n)
{
int i,key;
printf("\n Enter key to be searched ");
fflush(stdin);
scanf("%d",&key); 
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\n Element found at position = %d",i + 1);
break;
}
}
if(i==n)
printf("\n Element not found");
}