Advertisement
Waliullah8328

DFS

Aug 4th, 2021
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include<stdio.h>
  2. void DFS(int);
  3.  
  4. int G[10][10],visited[10],n;    //n is no of vertices and graph is sorted in array G[10][10]
  5. int main()
  6. {
  7.     int i,j;
  8.    //Enter number of vertices
  9.     scanf("%d",&n);
  10.     //Enter adjecency matrix of the graph
  11.     for(i=1;i<=n;i++)
  12.        for(j=1;j<=n;j++)
  13.             scanf("%d",&G[i][j]);
  14.     //visited is initialized to zero
  15.     for(i=1;i<=n;i++)
  16.         visited[i]=0;
  17.     int v;
  18.     scanf("%d",&v); // starting vertice
  19.     DFS(v);
  20.     return 0;
  21. }
  22. void DFS(int i)
  23. {
  24.     int j;
  25.     printf("%d\t",i);
  26.     visited[i]=1;
  27.     for(j=1;j<=n;j++)
  28.        if(!visited[j]&&G[i][j]==1)
  29.             DFS(j);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement