-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndirected_Graph.cpp
More file actions
246 lines (244 loc) · 4.81 KB
/
Undirected_Graph.cpp
File metadata and controls
246 lines (244 loc) · 4.81 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
242
243
244
245
246
#include <iostream>
#include<vector>
using namespace std;
vector<vector<int>> graph;
void addVertex()
{
cout<<"Nodes before adding vertex: "<<graph.size()<<endl;
graph.push_back({});
cout<<"Nodes after adding vertex: "<<graph.size()<<endl;
}
void addEdge(int node1,int node2)
{
if(node1+1>graph.size())
{
cout<<"Node you want to create edge from does not exist"<<endl;
return;
}
else
{
if(node2+1>graph.size())
{
cout<<"Node you want to create edge to does not exist"<<endl;
return;
}
else
{
graph[node1].push_back({node2});
graph[node2].push_back({node1});
}
}
}
void print(int node)
{
int i;
for(int i=0;i<graph[node].size();i++)
{
cout<<node<<"-";
cout<<graph[node][i]<<endl;
}
cout<<endl;
}
int find_next_node(int n,bool visited[])
{
int i;
int flag=0;
for(i=0;i<graph[n].size();i++)
{
if(visited[graph[n][i]]==false)
{
flag=1;
break;
}
}
if(flag==0)
return -1;
return graph[n][i];
}
void dfs_traversal(int n,bool visited[graph.size()],vector<int> traverse_dfs)
{
//cout<<"The value of the vertex is: "<<n<<endl;
if(traverse_dfs.size()==0)
return;
else
{
int next;
next=find_next_node(n,visited);
if(next==-1)
{
traverse_dfs.pop_back();
dfs_traversal(traverse_dfs[traverse_dfs.size()-1],visited,traverse_dfs);
}
else
{
cout<<next<<"->";
visited[next]=true;
traverse_dfs.push_back(next);
dfs_traversal(next,visited,traverse_dfs);
}
}
}
void dfs(int n,bool visited[])
{
vector<int> traverse_dfs;
visited[n]=true;
traverse_dfs.push_back(n);
cout<<n<<"->";
dfs_traversal(n,visited,traverse_dfs);
}
void bfs_traversal(int n,bool visited[],vector<int> traversal_bfs)
{
if(traversal_bfs.size()==0)
{
return;
}
else
{
for(int i=0;i<graph[n].size();i++)
{
if(visited[graph[n][i]]==false)
{
cout<<graph[n][i]<<"->";
traversal_bfs.push_back(graph[n][i]);
visited[graph[n][i]]=true;
}
}
traversal_bfs.erase(traversal_bfs.begin());
if(traversal_bfs.size()!=0)
{
bfs_traversal(traversal_bfs[0],visited,traversal_bfs);
}
}
}
void bfs()
{
int n;
cout<<"Enter the node you want to start dfs from"<<endl;
cin>>n;
if(n>=graph.size())
{
cout<<"Enter a valid choice"<<endl;
bfs();
return;
}
else
{
bool visited[graph.size()];
vector<int> traverse_bfs;
for(int i=0;i<graph.size();i++)
{
visited[i]=false;
}
visited[n]=true;
traverse_bfs.push_back(n);
cout<<n<<"->";
bfs_traversal(n,visited,traverse_bfs);
}
}
void connected_components(bool visited[])
{
int count=0;
for(int i=0;i<graph.size();i++)
{
if(!visited[i])
{
count++;
dfs(i,visited);
cout<<endl;
}
}
cout<<"The number of connected components are: "<<count;
}
void check_cycles(bool visited[])
{
if(!visited[])
for(int i=0;i<graph.size();i++)
{
}
}
int main() {
cout<<"Welcome to graph api";
cout<<endl<<"------------------------------------"<<endl;
while(1)
{
int ch;
cout<<"What operation do you want to do?"<<endl;
cout<<"1)Add a node/vertex"<<endl;
cout<<"2)Add an edge to a node"<<endl;
cout<<"3)Print the edges of a node"<<endl;
cout<<"4)Depth first search"<<endl;
cout<<"5)Breadth first search"<<endl;
cout<<"6)Connected components"<<endl;
cout<<"7)Check if the graph has cycles"<<endl;
cin>>ch;
bool visited[graph.size()];
vector<int> traverse_dfs;
for(int i=0;i<graph.size();i++)
{
visited[i]=false;
}
switch(ch)
{
case 1:
{
addVertex();
break;
}
case 2:
{
int node1,node2;
cout<<"Enter node1"<<endl;
cin>>node1;
cout<<"Enter node2"<<endl;
cin>>node2;
addEdge(node1,node2);
break;
}
case 3:
{
int node;
cout<<"Enter the node you want to print the adjacent vertex for: "<<endl;
cin>>node;
print(node);
break;
}
case 4:
{
int n;
cout<<"Enter the node you want to start dfs from"<<endl;
cin>>n;
while(n>=graph.size())
{
cout<<"Enter a valid choice"<<endl;
cout<<"Enter the node you want to start dfs from"<<endl;
cin>>n;
}
dfs(n,visited);
cout<<endl;
break;
}
case 5:
{
bfs();
cout<<endl;
break;
}
case 6:
{
connected_components(visited);
cout<<endl;
break;
}
case 7:
{
check_cycles(visited);
cout<<endl;
break;
}
default:
{
cout<<"Enter a valid option"<<endl;
}
}
}
}