Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void DFS(int);
  4. int G[10][10],visited[10],n;
  5.  
  6. void main()
  7. {
  8.     int i,j,v;
  9.     printf("Enter number of total vertices:-");
  10.  
  11.     scanf("%d",&n);
  12.  
  13.     printf("\nEnter adjecency matrix of the graph:-\n");
  14.  
  15.     for(i=0;i<n;i++)
  16.        for(j=0;j<n;j++)
  17.             scanf("%d",&G[i][j]);
  18.  
  19.    for(i=0;i<n;i++)
  20.         visited[i]=0;
  21.     printf("Enter the started node:- ");
  22.     scanf("%d",&v);
  23.  
  24.     DFS(v);
  25. }
  26.  
  27. void DFS(int v)
  28. {
  29.     int k;
  30.     printf("\n%d",v);
  31.     visited[v]=1;
  32.  
  33.     for(k=0;k<n;k++)
  34.        if(!visited[k]&&G[v][k]==1)
  35.             DFS(k);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement