fork(3) download
  1. #include<cstdio>
  2. #include<list>
  3. #include<queue>
  4.  
  5. using namespace std;
  6.  
  7. int V;
  8. list<int>* point;
  9.  
  10.  
  11.  
  12. void Graph_Make(int V)
  13. {
  14. point=new list<int>[V];
  15. }
  16.  
  17.  
  18. void Edge_Create(int u,int v)
  19. {
  20. point[u].push_back(v);
  21. }
  22.  
  23. void Show_Graph()
  24. {
  25. int i;
  26. for(i=0;i<V;i++)
  27. {
  28. printf("\n");
  29. printf("%d :",i);
  30. for(auto it=point[i].begin();it!=point[i].end();it++)
  31. printf("%d ",*it);
  32. }
  33. }
  34.  
  35. void DFS_Recursive(int v,bool array1[])
  36. {
  37. printf("%d ",v);
  38. array1[v]=true;
  39.  
  40. for(auto it=point[v].begin();it!=point[v].end();it++)
  41. if(array1[*it]==false)
  42. DFS_Recursive(*it,array1);
  43. }
  44.  
  45.  
  46.  
  47. void DFS(int s)
  48. {
  49. bool array1[V];
  50. for(int i=0;i<V;i++)
  51. array1[i]=false;
  52.  
  53. DFS_Recursive(s,array1);
  54.  
  55. for(int i=0;i<V;i++)
  56. if(array1[i]==false)
  57. DFS_Recursive(i,array1);
  58. }
  59.  
  60.  
  61.  
  62.  
  63. int main(void)
  64. {
  65. int s;
  66. printf("\n Enter the vertices in a graph");
  67. scanf("%d",&V);
  68. Graph_Make(V);
  69. Show_Graph();
  70. printf("\n Enter the node from where u want to start your DFS");
  71. scanf("%d",&s);
  72. printf("\n\n DFT of above tree is : ");
  73. DFS(s);
  74. return(0);
  75. }
  76.  
Success #stdin #stdout 0s 3476KB
stdin
4
3
stdout
 Enter the vertices in a graph
0 :
1 :
2 :
3 :
 Enter the node from where u want to start your DFS

 DFT of above tree is : 3 0 1 2