-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_representation.cpp
52 lines (42 loc) · 966 Bytes
/
graph_representation.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
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define rep(i, a, b) for(int i=a; i<b; i++)
const int N = 1e5+2, MOD = 1e9+7;
vvi adjl(N);
int main() {
// Adjacency Matrix implementation
int n, m;
cin>>n>>m;
vvi adjm(n+1, vi(n+1, 0));
rep(i, 0, m) {
int x, y;
cin>>x>>y;
adjm[x][y] = 1;
adjm[y][x] = 1;
}
cout<<"adjacency matrix of above graph:"<<endl;
rep(i, 1, n+1) {
rep(j, 1, n+1)
cout<<adjm[i][j]<<" ";
cout<<endl;
}
cout<<endl<<endl;
// Adjacency List implementation
cin>>n>>m;
rep(i, 0, m) {
int x, y;
cin>>x>>y;
adjl[x].push_back(y);
adjl[y].push_back(x);
}
cout<<"adjacency list of the above graph:"<<endl;
rep(i, 1, n+1) {
cout<<i<<" -> ";
for(int x: adjl[i])
cout<<x<<" ";
cout<<endl;
}
return 0;
}