Created
August 4, 2017 18:27
-
-
Save sudikrt/d4553b9cb5c8b8402d74eb06dbea4586 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
int n,a[10][10],visited[10],q[10],f=0,r=-1,x[10]; | |
void bfs(int v) | |
{ | |
int i; | |
for(i=0;i<n;i++) | |
{ | |
if(a[v][i]&&!visited[i]){ | |
visited[i]=1; | |
q[++r]=i; | |
} | |
} | |
if(f<=r) | |
{ | |
printf("%d",q[f]); | |
bfs(q[f++]); | |
} | |
} | |
main() | |
{ | |
int i,j; | |
printf("Enter the number of vertices\n"); | |
scanf("%d",&n); | |
printf("Enter the adjacency matrix\n"); | |
for(i=0;i<n;i++) | |
visited[i]=0; | |
for(i=0;i<n;i++) | |
{ | |
for(j=0;j<n;j++){ | |
scanf("%d",&a[i][j]); | |
} | |
} | |
visited[0]=1; | |
printf("%d",0); | |
bfs(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment