-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp6.cpp
More file actions
241 lines (222 loc) · 4.41 KB
/
Copy pathp6.cpp
File metadata and controls
241 lines (222 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include<iostream>
using namespace std;
class DFS {
public:
int top, f, r, x;
int** adjList;
int data[30], data1[30];
int visit[20];
int g[10][10];
void create();
void display();
void createList();
void displayList();
void dfs();
void bfs();
DFS() {
top = -1;
f = r = -1;
adjList = NULL;
}
int pop() {
if(top != -1)
{
int y = data[top];
top--;
return y;
}
return -1;
}
void push(int t) {
top++;
data[top] = t;
}
void enqueue(int t) {
if(f == -1 && r == -1)
{
f++;
r++;
data1[r] = t;
}
else
{
r++;
data1[r] = t;
}
}
int dequeue() {
if(f == -1 && r == -1)
return -1;
else
{
int y = data1[f];
if(f == r)
f = r = -1;
else
f++;
return y;
}
}
};
void DFS::create() {
cout<<"Number of nodes:\t";
cin>>x;
for(int i = 0; i < x; i++)
{
for(int j = 0; j < x; j++)
{
cout<<endl<<"Enter link status of graph from node:\t";
cin>>g[i][j];
}
}
}
void DFS::createList() {
cout << "Number of nodes:\t";
cin >> x;
adjList = new int*[x];
for (int i = 0; i < x; ++i)
{
adjList[i] = new int[x];
for (int j = 0; j < x; ++j)
{
adjList[i][j] = 0;
}
}
int connected, node;
for (int i = 0; i < x; i++)
{
cout << "\nEnter number of nodes connected to node " << i << ": ";
cin >> connected;
cout << "\nEnter the nodes connected to node " << i << ": ";
for (int j = 0; j < connected; j++)
{
cin >> node;
adjList[i][node] = 1;
}
}
}
void DFS::displayList()
{
for (int i = 0; i < x; i++)
{
cout << "\nNode " << i << " is connected to: ";
for (int j = 0; j < x; j++)
{
if (adjList[i][j] == 1)
{
cout << j << " ";
}
}
}
cout<<"\n";
}
void DFS::display()
{
cout<< " ";
for (int i = 0; i < x; i++)
{
cout<<" "<<i;
}
cout<<"\n";
for (int i = 0; i < x; i++)
{
cout<<i<<" |";
for (int j = 0; j < x; j++)
{
cout<<" "<< g[i][j];
}
cout<<"\n";
}
}
void DFS::dfs()
{
for(int i = 0; i < x; i++)
visit[i] = 0;
DFS s;
int v1;
cout<<"\nEnter starting node: ";
cin>>v1;
s.push(v1);
cout<<"DFS traversal is: ";
while(s.top != -1)
{
int v = s.pop();
if(visit[v] == 0)
{
cout<<" "<<v;
visit[v] = 1;
for(int i = x-1; i > -1; i--)
{
if(g[v][i] == 1 && visit[i] == 0)
{
s.push(i);
}
}
}
}
}
void DFS::bfs()
{
for(int i = 0; i < x; i++)
visit[i] = 0;
DFS s;
int v1;
cout<<"\nEnter starting node: ";
cin>>v1;
s.enqueue(v1);
cout<<"\nBFS traversal is: ";
while(s.f != -1 && s.r != -1)
{
int v = s.dequeue();
if(visit[v] == 0)
{
cout<<" "<<v;
visit[v] = 1;
for(int i = 0; i < x; i++)
{
if(adjList[v][i] == 1 && visit[i] == 0)
{
s.enqueue(i);
}
}
}
}
cout<<"\n";
}
int main()
{
DFS obj;
bool flag = true;
int choice;
while(flag)
{
cout<<"\n***YOUR CHOICES ARE****\n";
cout<<"\n1. Create Graph (Matrix) \n2. DFS Traversal (Using Matrix) \n3. Create Graph (List) \n4. BFS Traversal (Using List) \n5. Exit";
cout<<"\nEnter choice: ";
cin>>choice;
switch(choice)
{
case 1:
obj.create();
obj.display();
break;
case 2:
obj.dfs();
break;
case 3:
obj.createList();
obj.displayList();
break;
case 4:
obj.bfs();
break;
case 5:
flag = false;
break;
default:
cout<<"\nEnter Valid Choice!";
break;
}
}
return 0;
}