File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h> 
2+ using  namespace  std ; 
3+ 
4+ void  dfsUtil (int  currentNode, vector< vector<int > > &adj, vector<bool > &visited) {
5+  visited[currentNode] = true ;
6+  cout << currentNode << "  " 
7+  for (int  i = 0 ; i < adj[currentNode].size (); i++) {
8+  int  neighbour = adj[currentNode][i];
9+  if (!visited[neighbour])
10+  dfsUtil (neighbour, adj, visited);
11+  }
12+  return ;
13+ }
14+ 
15+ void  dfs (int  nodes, vector< vector<int > > &adj) {
16+  vector<bool > visited (nodes+1 , false );
17+  for (int  i = 1 ; i <= nodes; i++) {
18+  if (!visited[i])
19+  dfsUtil (i, adj, visited);
20+  }
21+  return ;
22+ }
23+ 
24+ 
25+ int  main () {
26+  int  nodes, edges, x, y;
27+  cout << " Input number of nodes and edges: " 
28+  cin >> nodes >> edges;
29+  cout << " Input edges: \n " 
30+  vector< vector<int > > adj (nodes+1 );
31+  for (int  i = 1 ; i <= edges; i++) {
32+  cin >> x >> y;
33+  adj[x].push_back (y);
34+  adj[y].push_back (x);
35+  } 
36+  dfs (nodes, adj);
37+  return  0 ;
38+ }
                         You can’t perform that action at this time. 
           
                  
0 commit comments