-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_represent.cpp
More file actions
56 lines (50 loc) · 1.22 KB
/
graph_represent.cpp
File metadata and controls
56 lines (50 loc) · 1.22 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
#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;
vi adj[N];
signed main(){
int n, m; // n = no. of nodes, m = no. of edges
cin>>n>>m;
// adjacency matrix
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 is given here: "<<endl;
rep(i,1,n+1){
rep(j,1,n+1)
cout<<adjm[i][j]<<" ";
cout<<endl;
}
// checking if an edge is present or not
if(adjm[3][7] == 1)
cout<<"Edge Present"<<endl;
else
cout<<"Edge NOT Present"<<endl;
cout<<endl<<endl;
// adjacency list
cin>>n>>m;
rep(i,0,m){
int x, y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
cout<<"Adjacency List given below: "<<endl;
rep(i,1,n+1){
cout<<i<<"->";
vector<int> :: iterator it;
for(it=adj[i].begin(); it!=adj[i].end(); it++)
{
cout<<*it<< " ";
}
cout<<endl;
}
return 0;
}