1
- #include < bits/stdc++.h>
2
- using namespace std ;
3
-
4
- int v; // No. of Vertices
5
-
6
- void Add_edge (vector<int > adj[], int a, int b)
7
- {
8
- adj[a].push_back (b);
9
- }
10
-
11
- /* ************************************ ALGORITHM FOR BREADTH FIRST SEARCH ******************************************************/
12
-
13
- // Take the empty queue and bool type array (visit) initialise with FALSE.
14
- // Push the starting node in the queue and set the value TRUE for this node in visited array.
15
- // Pop out the front node of the queue and print the node.
16
- // Push the adjacent node of pop node in queue which are not visited. Set the value TRUE in visited array of adding node.
17
- // Repeat step 3 and 4 until the queue becomes empty.
18
-
19
- void bfs (int n, vector<int >adj[])
20
- {
21
- vector<bool > visit (v,false );
22
- queue<int > q;
23
- q.push (n);
24
- visit[n]=true ;
25
- while (!q.empty ())
26
- {
27
- n=q.front ();
28
- cout<<n<<" " ;
29
- q.pop ();
30
- for (int i=0 ;i<adj[n].size ();i++)
31
- {
32
- if (!visit[adj[n][i]])
33
- {
34
- q.push (adj[n][i]);
35
- visit[adj[n][i]]=true ;
36
- }
37
- }
38
- }
39
- }
40
-
41
- /* ************************************ ALGORITHM FOR DEPTH FIRST SEARCH ******************************************************/
42
-
43
- // Take the empty stack and bool type array (visit) initialise with FALSE.
44
- // Push the starting node in the stack and set the value TRUE for this node in visited array.
45
- // Pop the top node from the stack and print that node.
46
- // Push the adjacent node of pop node in the stack which is not visited. Set the value TRUE in visited array of adding node.
47
- // Repeat step 3 and 4 until the stack becomes empty.
48
-
49
-
50
- void dfs (int n, vector<int > adj[])
51
- {
52
- vector<bool > visit (v,false );
53
- stack<int > s;
54
- s.push (n);
55
- visit[n]=true ;
56
- while (!s.empty ())
57
- {
58
- n=s.top ();
59
- cout<<n<<" " ;
60
- s.pop ();
61
- for (int i=0 ;i<adj[n].size ();i++)
62
- {
63
- if (!visit[adj[n][i]])
64
- {
65
- s.push (adj[n][i]);
66
- visit[adj[n][i]]=true ;
67
- }
68
- }
69
- }
70
-
71
- }
72
-
73
- int main ()
74
- {
75
- cout<<" Enter the no. of vertices : " ;
76
- cin>>v;
77
- vector<int > adj[v];
78
- vector<bool > visit (v,false );
79
- Add_edge (adj,0 ,1 );
80
- Add_edge (adj,0 ,2 );
81
- Add_edge (adj,1 ,2 );
82
- Add_edge (adj,2 ,0 );
83
- Add_edge (adj,2 ,3 );
84
- Add_edge (adj,3 ,3 );
85
-
86
- int temp;
87
- cout<<" Choose the vertex from you want to start traversing : " ;
88
- cin>>temp;
89
- cout<<" \n BFS traversal is" <<" " ;
90
- bfs (temp,adj);
91
- cout<<" \n DFS traversal is" <<" " ;
92
- dfs (temp,adj);
93
-
94
- return 0 ;
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ int v; // No. of Vertices
5
+
6
+ void Add_edge (vector<int > adj[], int a, int b)
7
+ {
8
+ adj[a].push_back (b);
9
+ }
10
+
11
+ /* ************************************ ALGORITHM FOR BREADTH FIRST SEARCH ******************************************************/
12
+
13
+ // Take the empty queue and bool type array (visit) initialise with FALSE.
14
+ // Push the starting node in the queue and set the value TRUE for this node in visited array.
15
+ // Pop out the front node of the queue and print the node.
16
+ // Push the adjacent node of pop node in queue which are not visited. Set the value TRUE in visited array of adding node.
17
+ // Repeat step 3 and 4 until the queue becomes empty.
18
+
19
+ void bfs (int n, vector<int >adj[])
20
+ {
21
+ vector<bool > visit (v,false );
22
+ queue<int > q;
23
+ q.push (n);
24
+ visit[n]=true ;
25
+ while (!q.empty ())
26
+ {
27
+ n=q.front ();
28
+ cout<<n<<" " ;
29
+ q.pop ();
30
+ for (int i=0 ;i<adj[n].size ();i++)
31
+ {
32
+ if (!visit[adj[n][i]])
33
+ {
34
+ q.push (adj[n][i]);
35
+ visit[adj[n][i]]=true ;
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+ /* ************************************ ALGORITHM FOR DEPTH FIRST SEARCH ******************************************************/
42
+
43
+ // Take the empty stack and bool type array (visit) initialise with FALSE.
44
+ // Push the starting node in the stack and set the value TRUE for this node in visited array.
45
+ // Pop the top node from the stack and print that node.
46
+ // Push the adjacent node of pop node in the stack which is not visited. Set the value TRUE in visited array of adding node.
47
+ // Repeat step 3 and 4 until the stack becomes empty.
48
+
49
+
50
+ void dfs (int n, vector<int > adj[])
51
+ {
52
+ vector<bool > visit (v,false );
53
+ stack<int > s;
54
+ s.push (n);
55
+ visit[n]=true ;
56
+ while (!s.empty ())
57
+ {
58
+ n=s.top ();
59
+ cout<<n<<" " ;
60
+ s.pop ();
61
+ for (int i=0 ;i<adj[n].size ();i++)
62
+ {
63
+ if (!visit[adj[n][i]])
64
+ {
65
+ s.push (adj[n][i]);
66
+ visit[adj[n][i]]=true ;
67
+ }
68
+ }
69
+ }
70
+
71
+ }
72
+
73
+ int main ()
74
+ {
75
+ cout<<" Enter the no. of vertices : " ;
76
+ cin>>v;
77
+ vector<int > adj[v];
78
+ vector<bool > visit (v,false );
79
+ Add_edge (adj,0 ,1 );
80
+ Add_edge (adj,0 ,2 );
81
+ Add_edge (adj,1 ,2 );
82
+ Add_edge (adj,2 ,0 );
83
+ Add_edge (adj,2 ,3 );
84
+ Add_edge (adj,3 ,3 );
85
+
86
+ int temp;
87
+ cout<<" Choose the vertex from you want to start traversing : " ;
88
+ cin>>temp;
89
+ cout<<" \n BFS traversal is" <<" " ;
90
+ bfs (temp,adj);
91
+ cout<<" \n DFS traversal is" <<" " ;
92
+ dfs (temp,adj);
93
+
94
+ return 0 ;
95
95
}
0 commit comments