Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int a[20][20], q[20], visited[20], n,u, i, j, f = 0, r = -1;
- void bfs(int v)
- {
- for(i = 1; i <= n; i++)
- if(a[v][i] && !visited[i])
- q[++r] = i;
- if(f <= r)
- {
- visited[q[f]] = 1;
- bfs(q[f++]);
- }
- }
- void isConnectedGraph(int v,int u)
- {
- for(i = 1; i <= n; i++)
- if(a[v][u] && !visited[i])
- q[++r] = i;
- if(f <= r)
- {
- visited[q[f]] = 1;
- bfs(q[f++]);
- }
- }
- void main()
- {
- int v;
- printf("\n Enter the number of vertices:");
- scanf("%d", &n);
- for(i=1; i <= n; i++)
- {
- q[i] = 0;
- visited[i] = 0;
- }
- printf("\n Enter graph data in matrix form:\n");
- for(i=1; i<=n; i++)
- {
- for(j=1; j<=n; j++)
- {
- scanf("%d", &a[i][j]);
- }
- }
- printf("\n Enter the starting vertex:");
- scanf("%d", &v);
- bfs(v);
- printf("\n Enter the ending vertex:");
- scanf("%d", &u);
- isConnectedGraph(v,u);
- printf("\n The node's traversal path:\n");
- for(i=1; i <= n; i++)
- {
- if (a[i][u] == 1 && visited[i] == 0)
- {
- for(j=1; j<=n; j++)
- visited[i] =1;
- printf("%d\t", a[i][j]);
- }
- }
- }
Add Comment
Please, Sign In to add comment