0% found this document useful (0 votes)
14 views

Ada Lab Programs

The document describes two programs to search for an element in an array. The first uses linear search and records the time taken. It searches through the array sequentially. The second uses binary search, which first sorts the array and then searches by checking the middle element and recursively searching either the upper or lower half. It also records the time taken to find the element. Both output the position of the found element or indicate if it is not found along with the execution time.

Uploaded by

Rahul Ramesh
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)
14 views

Ada Lab Programs

The document describes two programs to search for an element in an array. The first uses linear search and records the time taken. It searches through the array sequentially. The second uses binary search, which first sorts the array and then searches by checking the middle element and recursively searching either the upper or lower half. It also records the time taken to find the element. Both output the position of the found element or indicate if it is not found along with the execution time.

Uploaded by

Rahul Ramesh
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/ 20

Desiq DesiqwANd ANalysis of Agonitks :', =

Write a program to search an element in an array using linear Soarch mothod. Find thetim
Program
required to search an element. Oulput
Enter the number
#includesstdio.h>
5
#include<time.h»
Enter the elemen
#include sstdlib.h> 10 20 30 40 50
int linearSearch (int a].int n,int key) Enter the key ele
50
int i; Thesearch elein=
The Execution Ti
for(i-0;isn;i++)
if (key == a[]) Program 2:,
return i;
#include<stdio.h:
return-1; //Unsuccessful Search - It returns -1 if key is not found #includectime.ha
finclude<stdlib.h

void main0 int binarySearchs


char ch; int mid, i
int a100], n, key, i, res;
clock t st,et; if (last<fi
retur
printf("Enter the number of elements in the array: \n):
scanf"%d",&n); forfi-0;i-
printf("Enter the elements of the array:\n"); fori
fori=0;i<n;i++)
scanf(%d",&a[lil);
printf("Enter the key element to search: \n");
scanf"%d",&key);
st=clockO; //Record Start Time
res=linearSearch(a,n,key);
et clock: //Record End time
double time_taken =
(((double) (et -st)) / CLOCKS_PER_SEc)*1000;
while(f
if(res-1)
printf("The seafch element is not found\n"J if(k
print ("The Execution Time is %.0f Milli Seconds",time_taken);
=

exit(0); else
else
elsE
printf("The search element is found at position %d\n",res+1);
printf("The Execution Time is %.0f Milli Seconds",time.taken);
=

return
Lab PnoçnaMs As
-thod. Find the time
Output
Enter the number of elements in the array:

Enter the elements ofthe array


10 20 30 40 50
Enter the key element to search:
50
The search elenent is found at position 5
The Execution Time is =0 Milli Seconds

Write a program to search an eloment in an orray using Binary Search mothod. Find the time
Program required to search an oloment.

#includeestdio.h>
#includectime.h>
#include<stdlib.h>

int binarySearch(int al],intkeyint n,int firstint last)


int hid, 1, j, temp;

if (lastfirst)
return-1;

for(i=0;ic=n-2;i++)
for(j-0;j<=n-2-ij++),
if(ati+1]«ali)
temp=aj]:
a[il=alj+1];
ali+1]=temp;

while (first=last)
mid-(firstlast)/2;
if(tkey=afmid])
return mid+1;
else if(keysa[mid])
last=mid-1;
else
first=mid+1;
return 1;
Resiqw Ad Aálysis of Alcorirkiws E

void main0 Write a


Program 3
the tim
char ch;
int a[100].n, key, i, res, first, last; #include<stdio.h>
#includectime.h>
clock_t st,et #include<stdlib.h>
printf("Enter the number of elements in the array: \n"); int RBinarySearch[int a
scanf("%d",&n);
int midij.temp;
printf("Enter the elements of the array in \n"):
for(i=0i<n;i+ +) iflastefirst)
scaní("%d"&a[i]); return-1;

printf("Enter the key element to search: \n"); fori-0i=n-2+-


scanf("%d",&key);
forj-0;j=n-
first0;
last=n-1; ifalj+1]
st=clockO: //Record Start Time temp
apl-
res=binarySearch(a,key.n,first,last); aj+1
printf("The sorted array is: ");
for(i=0;i<n;i++)
printf(" 6d al]);
mid=(first+last
et=clock0; I/ Record End time
double time_taken (double) (et st)) /
=
CLOCKS_ PER SEC) 1000;
-

i key-=a[mid]
return mid;

ifres-1) else iffkeysa[m


return RBir
printf(" nThe search element is not found\n" else
printf("The Execution Time is = %.0f Milli Seconds",time_taken); return RBir
exit(0);
else voidmain0
printf("nThe search element is found at position %d\n",res);
printf("The Execution Time is = %.0f Milli Seconds",time_taken); char ch
int a[100].nke
clock tstet;
printf("Entert
Enter the number df elements in the array: scanf %d"&n
6 printf Enter t
Enter the elements of the array in:
60 40 30 20 1050 for(i=0;<n;i+
Enter the key element to search,: Scanf(
10
The sorted array is : 10 20 30 40 50 60 printf(Enter
The search element is found at position 1 scanf"%d"&H
The Execution Time is = 2 Milli Seconds first-0
last=n-1;
Lab PivGgáis

Write a program to search an element in an array using Rocursiva Binary Search mothod. Find
Program 3' tho timo roqvired to soarch an element.

#includesstdio.h>
#includectime.h>
#includestdlib.h>

int RBinarySearch(int aint keyint nint first,int last)

int midi,.temp;

if(last first)
return-1;

for(i-0;ie=n-2;i++)
forj-0;j<=n-2-i:j++)
ifaj+1]<a[i])
temp=a[j];
a]=aj+1]:
aj+1]=temp;

mid-(first+ast)/2;
if(key==a[mid])
return mid;
else if(key<a{mid])
returnRBinarySearch(a,keyn,first,mid-1);
else
return RBinarySearch(a,keyn,mid+1,last)

void main0

char ch
int af100],n,key,l,res, first, last;
clock t st,et;
printf(Enter the number ofelements In the array: \n");
scanf("%d"&n);
printf("Enter the elements of the array:\n");
fori=0;i<n;i++)
scanf("%d"&a[i];
printf("Enter the key element to search: \n
scanf("%d",&key); .
first-0;
last=n-1;
Desiqw ANd ANalysis of Algonirkms

st=clock): //Record Start Time int partition(int A[10].in


res=-RBinarySearch(a,key.n,first.last); int pivot, j, temp.
printf("The sorted arrayis: "); pivot = low;
forfi=0;i<n;i++) i = low;
printf" %d"a[i]): j= high;
while i« j)
et-clock0: // Record End time
double time_taken =
((double) (et st))/ CLOCKS PER_SEC1000; whilei<high
-

it+
iftres=-1) while (A[i]->=
printf"\nThe search element is not found \n");
printf("The Execution Time is =
%.0f Milli Seconds", time_taken);
if(i«)
exit(0); temp =
A[i] = A
else
printf(" nThe search element is found at position %d\n', res+1): AG] te
printf"The Execution Time is =%.0f Milli Seconds",time_taken);

Output temp A[pivot


A[plvot) = AGI
Enter the number of elements in the array:
6 Al=temp:
return j:
Enter the elements of the array
90 70 50 80 40 30
Enter the key elemernt to searcih:
40
void main0
The sorted array is : 30 40 50 70 80 90
The search element is found t position 2
The Execution Time is = 1 Milli Seconds
inti. n, A[10];
clocktst, et;

Write d program to sort an elements in an array using Quick Sort. Find the time required printf Entert
Progrom 4 sort the elemenls.
to scanf("%d",&n
#include <stdio.h> printf Enter t
#include <conio.h> for ( 0;i< n;
#include <time.h>
SCanf%d
void quicksort(int A[10], int low, int high) st= clock0:
quicksort(A, 0
int j;
if (low < high)
et clock0:
double time_ta
jpartition(,low.high);
quicksort(A, low, j -1); printf("Sorted
quicksort[A, j + 1, high);
for(i 0;l<n
printf("
printf("The E=
getch0:
int partition(int A[10]. int low, int high)

int pivot, j, temp, i;


pivot= low;
i=low;
j=high;
while (i« j)

whileiehigh && Ai)<=A[plivot|)


i++;
whlle (A[i)>A[pivot])
if(t)
):
temp = A[i};
Ai] = A[j]:
AJ = temp;

temp A[pivot];
A[pivot) A0]:
Ali] = temp;
return j;

void main0

inti, n, A[10];
clockt st, et;
printf("Enter the number of elements of array: \n");
Quick Sort. Find tho time required to scanf("%d, &n);
printf("Enter the elements of the array: \n");
for (i = 0; i< n; i++)

scanf("%d", &A[):
st clock0:
quicksort(A, 0, n-1):
et clock0
double time taken = (((double) (et - st))/ CLOCKS_PER_SEC)*1000;

printf("Sorted list of elements ");


for (i 0; i< n; i++)
printf(" %d ", A[]):

printf("The Execution Time is = %.0f MillK Seconds",time_taken);


getch0:
A.8 Desiqw ANd ANalysis ofAlgonirhms

Output
Enter the number of elements of
array:
void Merge Sort(int A[10
5
Enter the elements int mid;
30 20 10 50 40
of the array:
Sorted list of elements : 10 20 30 40 50 if(lowehigh)
The Execution Time is = 0 in milli seconds
mid=(low+hig
Program 5
Write a
program
to sort an elements in an array using Merge Sort. Find the time
MergeSort(A
sort the elements. required to MergeSort(A
#include <stdio.h> Merge(Alow
#include <conio.h>
#include <time.h>
void Merge(int A[10],int lowint mid,int high)
void main00
int ij.k.c[10]:
{
i=low inti, n, A[10]:
k=low;
j=mid+1; clockt st, ct
while(i=mid)&&(j«=high)) printf"Enter the
if(A]<«=A[i}) scanf("%d", &n);

clkl-A[i; printf("Enter the


it+; for (i=0;i< n; in
k++; Scanf( %d".
else
st clockO:
MergeSort(A, 0,
c[kl-A[Jl; et= clock0:
jt+; double time_tak
k+t
printf('Sorted la
for (i 0;i< n;i-
while(lk=mid) printf" %d

elkl=A;
kt+;
printf(The Exe
getch0
whiielj=high)
Output:
c[k=A[j]: Enter the number ofe
ktt;
++;
Enter the elements of
28 95 44 12
for(i=low; i<=k-1; i++) Sorted list of element
A[J=c[il; The Executton Timei=
HMs
Lab ProqRAMS
A.9
void MergcSortint A[10],int lowjnt high)

intmid;
if[lowehigh)
mid=(low+high)/2;
MergeSort(A,low.mid);
g Merge Sort. Find the lime
required to MergeSort{A,mid+1,high):
Merge(A,low.mid.high);
.

voidmain0
inti, n, A[10];
clockt st, et;

printf("Enter the number of elements of array: \n");


scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i< n; it+)

scanf("%d", &A[1]);
st clock0:
MergeSort(A, 0, n-1);
et =clock0:
double time_taken = (((double) (et- st))/ CLOCKS_PER_SEC)*1000;

printf("Sorted list of elements:");


for (i 0; i< n; i++)
printfC" hd ", A[i]);

printf(The Execution Time is = %.0f Milli Seconds",time_laken);


getchO:

Output
Enter the number ofelements of array
5
Enter the elements of the array:
289544 12
Sorted list of clements:59 12 28 44
The Execution Time is =0 in Milli Seconds
A Desiaw ANd Analysis.of Algooirhus
From a given verlex in a weighted connected graph fMnd shortost paths to othor vortices printf Enter the
Program 6 t usin
Dijikstras Algorilhm. scanf"%d",2v);
/* Shortest path using dljikstras algorithm"/
#include<stdio.h> Dijikstra(n,Mcost
void Dijikstrafint n,int v, int cost[10][10], int dist[]) printf"\n Shorte
for(j-1;j<=n; jt-
int countu,,wflag[10].min;
ifj=v)
for(i=1;ie=n;i+*) printf
{ getch0:
lagil=0;
dist[ij=cost[v][{];
Output
flagfv]=1; Enter the no of nodes
distv]=1;
count=2; Enter the cost matrix
0 60 100
whilefcount<=n)
min=9999:
for(w=1; w«=n; w++)
20
if(dist/w]« min && !fag[w])
min=dist[w], u=w;
Enter the source vert
flaglu=1;
countt+ Shortest path from

for(w=1; w<=n; W++) 1-2 60


if[(distful+cost[u]/w] < dist{w]) && !faglw]) 1-+3-35
distfw] = dist[u]+ cost[u]lw): 1->4=15
1 - 5 10

Progran 7 Fin
void main()
/ Minimum cost spa
int n,v.cost[10][10].dlst[10],1J; #indudestdio.h>
printf("\n Enter the no of nodes:");
int parent[10), min, n
scanf("%d",&n);.
int ab,ij.u,yn;
printf("\n Enter the cost matrix: \");
for(l=i;j«=n;i++) main0
forlj=1;je=n; j++) printf"\n En
scanf( %d",
scanf"%d",&cost[i][)]l):
if(cost[i]==0) printf"n Er
costfij]=9999; forfi=1;i=nz
forj=1-
. Lab PhoqnaMS,

est paths to other vertices


using
printf("Enter the source vertex: ");
scanf"%d",&v);

Dijikstra(n,v.costdist);
printf"n Shortest path from \n");
for(j=1; j<=n; jt+)
if(jl=v)
printf\p%d->%d =%d"vj,dist[j]);
getch0:

Output
Enter the no of nodes:5

Enter the cost matrix


0 60 100 0 10

0 50 0

0 0 0 20
20 0 0

0 0 5 0

Enter the source vertex: 1.

Shortest path from

1->2 60
1->3 35
1->4 15
1->5 10

Find minimum cost spanning tree of a givon undirected graph using Kruskal's algorithm.
Progrom
|/* Minimum cost spanning tree uslng Kruskal's algorithm /
#include<stdio.h»
int parent10), min, no of.edges=1,mincost=0,cost[10][10];
int a,b,ij.u,y.n;
mainO
printf("\n Enter the no ofvertices:):
scanf("%d", &n);
printf"\n Enter the adjacency matrix: ");
for(i=1;i<=n;i++)
forj=1;j<=n;j++)
AJ2 Desicw adAnalysi of Alçogitns

Program 8 Find mi
scanf("%d"&cost[i)[i);
if(costill==0) /Minimum cost spannin
costfilf]=999; #include stdio.h>
while no ofedges < n) int a,b,u,v.n,ij.no_of.edge
intvisited[10), min, minc

for(i-t; min=999; i<=n; i++) void main()


for(j=1;j<=n; j+*)
if(cost[i]0]<min) printf"n Enter t
scanf("%d",&n):
min=cost[JU}: printf("\n Entert
a=u=i;
b=y=; for(i=1;l<=n;i++
forlj-1j=n;
while(parent[u])
u=parent[; Scanf"
while(parent[v)) iffcost[
V=parent[v): cOst
if(ul=v) forfi=2;ic=n;i+-
visited[]=0o
printf'n od \t Edge \t(%d,%d) = %d", no.of_edges,a,b.min);
mincost+= min; printft"\n The
parent[v) = u;
visited[1]=1;
no_ofedges++;
whiie(no_ofe
costla][b] = cost[b]la] = 999;
for(i=1, m-
printf("n\t Minimum cost= %d", mincost); forj=?
if
Output
Enterthe no ofvertices:5

Enter the adjacency matrix :

0 11 9 7

11 0 15 14 13
9 15 0 12 14
14 12 0 6

8.13 14 6

Edge(4,5) =6
COSt
2 Edge(1,4)=7
3 Edge(13)=9
4 Edge(1,2)= 11 printf("\n
Minímum cost= 33
getch():
Lab PaóqnaMS
lGogithns

Find minimum cost spanning tree of a given undirected graph using Prims algorithm
Program8
/* Minimum cost spanning
tree using prims algorithm */
¥includesstdio.h>
int a,b,u,vn,i,f.no_of_edges=1;
visited[10], min, mincost=0, cost[10][10];
int
void main)
printf("\n Enter the no of vertices: ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix ");

for(i=1;1k=n;i++)
for(j-1;j<=n;j+*)
scanf("%d",&cost[ij0]};
if(cost[i]0]==0)
cost[ []=999;

for(i=2;ic=n;i++)
visited[i]=0;
gesa,bmin); printf("\n The edges of spanning tree are :\n");
visited[1]=1;
while(no_of edges < n)

for(i=1, min=999; i*=n; i++)


for (j=1; j=n ;j++)
iffcost[i]0i] < min)
iffvisited[]==0)
continue;
else

min = costf]]:
a=u=i;
b=vj
iffvisited(u]==0 || visited [v]==0)

printf"\n%d\t Edge \t(%d,%d)=%d",no_of edges,a,b,min);


mincost += min;
visited(b]=1;
no_of edges++;
costfa)[b]=cost[bjfaj=999;

printf(\n\t Minimum cost = %d\n",mincost);

getch()
A4 DesiqN ANd Analysis of Algoeirkins

floyd0:
Output
Enter the no ofvertices and graph data: 4 forfi=1;i<=n;i++)
0 1 c]-0;
printf("\n The all pair
11
for(i=1;i<=n; i-+)
22
11 for(j-1:jc=n; j++)
printf"%6d',c
The edges of spanning tree are:
printf"\n)
Edge(1,4)=1 getch0
2 Edge(1,2)= 2

Edge(2,3) = 1
Output
Minimum cost = 4
Enter the number of verticie

Implement all pairs shortest paths problem using Floyd's algorithm. Enter the adjacency E
Program 93
/ All pair shortest paths problem using Floyd's algorithm"/ 10
#include<stdio.h> 0

int c[5]5), n,i, j, k 50 'o


0 0 60
void floyd0

for(k=1;k<=n;k++) The ail pair shor-


for(i-1i=n;i++) 10
for(j=1;j<=n;j++)
ifCc[iT[k] + c[k]il) < c[jG) 130 0
c[G]= c[i][k) + c[k][il: 50 60
110 120
void main0

printf("Enter the number of the verticies: \n")


scanf("%d"&n); Program 10:.Implement
/*0/1 knapsack problem usir
printf("\n Enter the adjacency matrix: \n"); #includestdio.h>
for(i=1; i<=n; i++)
for(j=1; j=n; j++) int ijin.capacity.w[501,p[50}
scanf(%d"&clij0)): int maxinmum(int x,int y)
iffci)li)==0)
cllil-999; if(x>y)
return x;
else
rerurn y
floyd0:

for(i=1;i<=n;i++)
0-0;
printf\n The all pair shortest path: \):
for(i=1;i«=n; 1++)

for(j=1; je=n: j++)


printf("%6d'c[Ji]);
printf("\n')
getch);

Output
Enter the number ofverticies: 4

Enter the adjacency matrix:


10 40

0 20

50 0

0 60 0

The all pair shortest path:


0 10 90 30
130 80 20
50 60 80
110 120 60 0

Progam 10 Implement 0/1 knapsack problem using dynamic programming.


/0/1 knapsack probleni using dynamic progranming "/
#include<stdio.h>
intiji.n.capacity,w[50J,p[50).maxprofity
int maximumint xinty)
if(xoy)
return x;
else
return y;
***Disigw andAnalyssöf ÄlGoni
Program 11s Check whe
knapsack(int i,int c)
Check graph for connecte
ifl==n) #includeestdio.h>
return((cew[n])? 0: p[n]);

if( c w[il) intiJa[20]/20],visited[20].


return knapsack(i+1,¢);
return maximumknapsack{i+1,c), knapsackfi+1,c-w[il)+p[i}): void initiateQ
for(i = 1; i<=n; i++)

void main0 visited[i]=0,J[iJ=0


printf" n Enter thè no of objects "); for(i 1 ; i <= n; i++)
scanf("%d",&n); for(j=1;j<=n;i
ali]0)=0;
printf"\n Enter the weights: ");
for(i=0;i<n;i++)
scanf("%d",&w[i]): void dfs(int v)
printf"n Enterthe profits associated: ");
for(i=0;i«n;i++) visited[v]=1;
scanf %d",&p[}); IvJ=label;
for(i=1; i<=n; i++)
printf"\n Enter the capacity: "); ifalv]] &&visi
scanf("%d",&capacity);: dís():
maxprofit=knapsack{0, ca pacity);
printf("\n Maximum profit = %d", maxprofit);
void component_label0
getchO
int i;
fori=1; ic=n; i++)
Output
Enter the number of objects:3 ifE== 0)

Enter the weights label++


100 for(k=1;k=
14 visited []=0;
10 dfs(i):
Enter the profits associated:
20
18
if(label> 1)
printf("n Discor
15
else
printfn Conne
Enter the capacity
116
printf"\n No of con

Maximum profit=38 forfi=1;i=n;i++)


printr"n Vertez
ANd Awalysts of Algogin LAb PaogRÁMS, 4, t

Program 11 Check whether a given graph is connectod or not connected using DFS method.

/Check graph for connected using DFS */


#includestdio.h>
int ija[20][20].visited[20].n,label=-0,kI[10]Vw
apsack(i+1,c-w[il)+p[ll);
void initiate0
for(i =1;i<=n;i++)
visited[i]=0,1[i]=0;
for(i 1;i <a n; i++)
fori 1 ; j <= n, j++)
alibj=0;

void dfs(int v)

visited[v]=1;
Iv]=label;
for(i=1; ic=n; i++)
if(alv][i] && visited[il==0)
dfs (i);

profit): void component_label0

nt:
fori=1; ie=n; i++)

if([ == 0)

label++
for(k=1;k<=n; k++)
visited [i]=0;
dís ():

if(label> 1)
printf"n Disconnected Graph";
else
printf("\n Connected Graph");

printf("n No of connected elements: %d",label);


for(i-1;ic=n;i++)
printf("\n Vertex %d helongs to component %d",i,|);
A.1e DtsiqN ANd Aalysisaf.Algonitlkms

void main0 Enter the a djacent vertices


Doyou want to continue: M
char ch;
printf("\n Enter the no vertices: ");
The adjacency matrix is:
scanf( 6d"&n);
0 0
initiate0: 0 0
do
0
printf("\n Enter the adjacent vertices: ");
scanf"%d %d", &v&w):
if(ve0 || v>n || we0 || w>n) Connected Graph
printfC"n Invalid vertex."); No of connected elements:
else Vertex 1 belongs to compor
Vertex 2 belongs to compor
alvl[w]=1; Vertex 3 belongs to compor
a w][v]=1; Vertex 4 belongs to compor

Print all th
printf("\n Do you want to continue :"); Program 12: search
fMush(stdin):
scanf("%c",&ch); #include<stdio.h>
#include<conio.h»
while ch=='y'||ch=='y);
intq[10-1r-1;
printf("The adjacency natrix is: \n");
void qinsintx)
forfi 1;i <<n; i++)
{ if(f-1&&r=-1)
for(j 1;j «= n; j++)
printf( 63d"a[li]G)); f+*;
printf("\n"); g++r=x
else
componentlabel(); q+*r]=*
getch0;

int qdel0
Oulput
int x=q[f;
Enterthe no vertices:4
if=r)
Enter the adjacent vertices:1 3 fsr-1;
Doyou want to continue: Y else

Enterthe adjacentvertices:34 return x;


Doyou wantto continue:Y
LAb PaogRAMS

Enterthe adjacent vertices:42


Do you want to continue: N

The adjacency matrix is:


0

0 0
0

Connected Graph
Noof connected elements:1
Vertex 1 belongs to component 1
Vertex 2 belongs to component 1
Vertex 3 belongs to component 1
Vertex 4 belongs to component

Print all the nodes reaeheble from a given starting node in a given digraph using breadth flrst
Program 12
search
#include<stdio.h>
#include<conio.h>

intq[10]=1,r-1;
voidqins(intx)
if(f=-1&8&r=-1)
f++;
9++r]=x;

else
gl++r]=x;

intqdel0
int x=q[f;
(f==r)
far-1;
else
f+
return x;
A Desicie aid AudraisofAlkonithns
void main0
Output
int u.s[i0]=[0).srcna[10}[10J,AJ; Enter no of verti

printf("n Enter nopfvertices: \"); Enter the adjacer


scanf("od",&n); 1

printf("n Enter the adjacency matrix: \n)"); 0


for(i-1; i<=n;i++) 1
for(j-1;j<=n;j++)
scanf("%d",&a[10]);
0
printf("\n Enter the sourcg vertex "); Enter the source

scanf"%d",&src); 1
The nodes that
s[srcj=1;

qins(src);
whileft=-1 && rl=-1)

u=qdel0:
printf(" %d"u);
Program 13:
fori-1;<=n;i++)
ifalul-=1 &ks|ij==0) / Checkfor node:
#includeestdio.h>
s[il=1; #incdudecconio.h=
qins()
ints{10]={0};
void dfs(int n,int a.
printf"\n The nodes that are reachable from %d are:\n',src);
for(i=1;i<=n;it+) intv
ifsl) sfu=1;
printf(" %0
printf("%d \n",i);
for(v=1;v
getch0: if(a[u]
dist

void main(0

int a[10][1
print("\n E
scanf("%cd"
printf\n I
of Alkonith Lab Progaams

Outpu
Enter no of vertices:

Enter the adjacency matrix:

1 0 1
0 0 1
1 0 0
0 0
0
Enter the source vertex: 1
1 2 4
The nodes that are reachable from 1 are:

Print all the nodes reachable


Program. 13: irom a given starting node in a given digraph using depth first
search method.
/* Check for nodes reachable
using DFS */
#include<stdio.h>
#includecconio.h>

int si10]={0}

void dfsint n,int a[10][10],int u)


l are:\n",sTc;
int v;
ssuj=1;
printf(" %d ",u);
for(v=1;ve=n;v++)
if(a[uly]==1 && siy]==0)
dfs(na,v};

void inain(0

int a[10][10],n,src,j;
printf("\n Enter the nuniber of vertices(n) \un"
scanf"%l",&en);
print"\n Enter the adjacency matrlx\n");

You might also like