forked from rahul22mrk/hackoctoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadj_matrix_graph.cpp
More file actions
61 lines (49 loc) · 985 Bytes
/
adj_matrix_graph.cpp
File metadata and controls
61 lines (49 loc) · 985 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
class graph{
private:
int v;
int **adjmatrix;
public:
graph(int V);
void addEdge(int u, int v, bool bidir);
void print();
};
graph::graph(int V) {
this->v = V;
adjmatrix = new int*[v];
for (int i=0 ; i<v ; i++) {
adjmatrix[i] = new int[v];
}
for (int i = 0 ; i < v ; i++) {
for (int j = 0 ; j < v ; j++) {
adjmatrix[i][j] = 0;
}
}
}
void graph::addEdge(int u, int v, bool bidir = true) {
adjmatrix[u][v] = 1;
if (bidir) {
adjmatrix[v][u] = 1;
}
}
void graph::print() {
for (int i = 0 ; i < v ; i++) {
for (int j = 0 ; j < v ; j++) {
cout << adjmatrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
graph g(4);
g.addEdge(0, 1, false);
g.addEdge(0, 2);
g.addEdge(0, 3, false);
g.addEdge(1, 3);
g.addEdge(3, 2, false);
g.print();
return 0;
}