fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* ADJACENCY MATRIX */
  4. int source,V,E,time,visited[20],G[20][20];
  5. void DFS(int i)
  6. {
  7. int j;
  8. visited[i]=1;
  9. printf(" %d->",i+1);
  10. for(j=0;j<V;j++)
  11. {
  12. if(G[i][j]==1&&visited[j]==0)
  13. DFS(j);
  14. }
  15. }
  16. int main()
  17. {
  18. int i,j,v1,v2;
  19. printf("\t\t\tGraphs\n");
  20. printf("Enter the no of edges:");
  21. scanf("%d",&E);
  22. printf("Enter the no of vertices:");
  23. scanf("%d",&V);
  24. for(i=0;i<V;i++)
  25. {
  26. for(j=0;j<V;j++)
  27. G[i][j]=0;
  28. }
  29. /* creating edges :P */
  30. for(i=0;i<E;i++)
  31. {
  32. printf("Enter the edges (format: V1 V2) : ");
  33. scanf("%d%d",&v1,&v2);
  34. G[v1-1][v2-1]=1;
  35.  
  36. }
  37.  
  38. for(i=0;i<V;i++)
  39. {
  40. for(j=0;j<V;j++)
  41. printf(" %d ",G[i][j]);
  42. printf("\n");
  43. }
  44. printf("Enter the source: ");
  45. scanf("%d",&source);
  46. DFS(source-1);
  47. return 0;
  48. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
			Graphs
Enter the no of edges:Enter the no of vertices:Enter the source:  0->