-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphs.cpp
322 lines (260 loc) · 7 KB
/
Graphs.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
template <typename T>
//Graph implementation
/*Nodes, Edges(Object is mapped to a list/vector)
1.map<node datatype,list<node datatype>>
2.vector<vector<int>>
n-->nodes m-->edges. Tree--> n nodes, n-1edges
-Sum of degrees in a graph is 2m
-Coloring: No two adj.nodes can have the same color
-
-A graph is called bipartite if you adj have same color a bipartite graph is not possible if we have a cycle with odd no. of edges
*/
/*A graph can be represented as:
-vector<int> adj[n]
-int adj[n][n]
-vector<pair<int,int>> edges
-map<int,list<int>>*/
class graph{
public:
unordered_map<T, list<T>> adj;
void addEdge(T u,T v, bool direction){
//direction=0-->undirected graph
//create edge from u to v;
adj[u].push_back(v);
if(direction==0) adj[v].push_back(u);
}
void printAdj(){
for(auto i: adj){
cout<<i.first<<"->";
for(auto j: i.second){
cout<<j<< " ";
}
cout<<endl;
}
}
};
/*int main(){
int n,m;
cin>>n>>m;
graph<int> g;
rep(i,0,m){
int u,v;
cin>>u>>v;
g.addEdge(u,v,0);
}
g.printAdj();
return 0;
}
/* vector<int> adj(n)
cin>>n>>m;
rep(i,0,m){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
*/
//DFS is implementes by recursion
/*const int N= 1e5+2;
bool vis[N];
vector<int> adj[N];
void dfs(int node){
}
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++) vis[i]=false;
int x,y;
for(int i=0;i<m;i++){
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
return 0;
}*/
//BFS
/*notation: adj[currentnode] evaluates all elements connected to current node*/
// Function to perform Breadth First Search on a graph
// represented using adjacency list
/*void bfs(vector<vector<int> >& adjList, int startNode,
vector<bool>& visited)
{
// Create a queue for BFS
queue<int> q;
// Mark the current node as visited and enqueue it
visited[startNode] = true;
q.push(startNode);
// Iterate over the queue
while (!q.empty()) {
// Dequeue a vertex from queue and print it
int currentNode = q.front();
q.pop();
cout << currentNode << " ";
// Get all adjacent vertices of the dequeued vertex
// currentNode If an adjacent has not been visited,
// then mark it visited and enqueue it
for (int neighbor : adjList[currentNode]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
// Function to add an edge to the graph
void addEdge(vector<vector<int> >& adjList, int u, int v)
{
adjList[u].push_back(v);
}
int main()
{
// Number of vertices in the graph
int vertices = 5;
// Adjacency list representation of the graph
vector<vector<int> > adjList(vertices);
// Add edges to the graph
addEdge(adjList, 0, 1);
addEdge(adjList, 0, 2);
addEdge(adjList, 1, 3);
addEdge(adjList, 1, 4);
addEdge(adjList, 2, 4);
// Mark all the vertices as not visited
vector<bool> visited(vertices, false);
// Perform BFS traversal starting from vertex 0
cout << "Breadth First Traversal starting from vertex "
"0: ";
bfs(adjList, 0, visited);
return 0;
}*/
//DFS using recursion
// Function to perform DFS on a graph
void dfs(vector<vector<int>>& adjList, int node, vector<bool>& visited) {
// Mark the current node as visited
visited[node] = true;
cout << node << " ";
// Explore all unvisited neighbors
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
dfs(adjList, neighbor, visited);
}
}
}
int main() {
// Number of vertices in the graph
int vertices = 5;
// Adjacency list representation of the graph
vector<vector<int>> adjList(vertices);
// Add edges to the graph
adjList[0].push_back(1);
adjList[0].push_back(2);
adjList[1].push_back(3);
adjList[1].push_back(4);
adjList[2].push_back(4);
// Vector to keep track of visited nodes
vector<bool> visited(vertices, false);
// Perform DFS starting from vertex 0
cout << "Depth First Traversal starting from vertex 0: ";
dfs(adjList, 0, visited);
return 0;
}
//DFS using stack
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
// Function to perform DFS on a graph using a stack
void dfs(vector<vector<int>>& adjList, int startNode, vector<bool>& visited) {
// Create a stack for DFS
stack<int> stk;
// Push the start node onto the stack and mark it as visited
stk.push(startNode);
visited[startNode] = true;
while (!stk.empty()) {
// Pop a vertex from the stack and print it
int currentNode = stk.top();
stk.pop();
cout << currentNode << " ";
// Get all adjacent vertices of the popped vertex
// If an adjacent vertex has not been visited, then push it onto the stack and mark it as visited
for (int neighbor : adjList[currentNode]) {
if (!visited[neighbor]) {
stk.push(neighbor);
visited[neighbor] = true;
}
}
}
}
int main() {
// Number of vertices in the graph
int vertices = 5;
// Adjacency list representation of the graph
vector<vector<int>> adjList(vertices);
// Add edges to the graph
adjList[0].push_back(1);
adjList[0].push_back(2);
adjList[1].push_back(3);
adjList[1].push_back(4);
adjList[2].push_back(4);
// Vector to keep track of visited nodes
vector<bool> visited(vertices, false);
// Perform DFS starting from vertex 0
cout << "Depth First Traversal starting from vertex 0: ";
dfs(adjList, 0, visited);
return 0;
}
//DFS
// C++ program to print DFS traversal from
// a given vertex in a given graph
#include <bits/stdc++.h>
using namespace std;
// Graph class represents a directed graph
// using adjacency list representation
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
// Function to add an edge to graph
void addEdge(int v, int w);
// DFS traversal of the vertices
// reachable from v
void DFS(int v);
};
void Graph::addEdge(int v, int w)
{
// Add w to v’s list.
adj[v].push_back(w);
}
void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
// Function call
g.DFS(2);
return 0;
}