-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
76 lines (69 loc) · 1.52 KB
/
Graph.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
#include <iostream>
#include <bits/stdc++.h>
#include<math.h>
#define ll long long int
using namespace std;
int main(){
int v, e, x, y, i, j, w;
cin>>v>>e;
vector<vector<bool> > AdjMat(v, vector<bool> (v, 0));
vector<vector<int> > AdjList(v);
for(i=0;i<e;i++){
cin>>x>>y;
AdjList[--x].push_back(--y);
AdjList[y].push_back(x);
AdjMat[x][y]=1;
AdjMat[y][x]=1;
}
cout<<"Adjacency Matrix :\n";
for(i=0;i<v;i++){
for(int j=0;j<v;j++){
cout<<AdjMat[i][j]<<" ";
}
cout<<endl;
}
cout<<"Adjacency List :\n";
for(i=0;i<v;i++){
for(j=0;j<AdjList[i].size();j++){
cout<<AdjList[i][j]<<" ";
}
cout<<endl;
}
//Weighted and Directed
cin>>v>>e;
vector<vector<int> > AdjMatWt(v, vector<int> (v, 0));
vector<vector<pair<int, int> > > AdjListWt(v);
for(i=0;i<e;i++){
cin>>x>>y>>w;
AdjMatWt[--x][--y]=w;
AdjListWt[x].push_back(make_pair(y, w));
}
cout<<"Adjacency Matrix :\n";
for(i=0;i<v;i++){
for(j=0;j<v;j++){
cout<<AdjMatWt[i][j]<<" ";
}
cout<<endl;
}
cout<<"Adjacency List :\n";
for(i=0;i<v;i++){
for(j=0;j<AdjListWt[i].size();j++){
cout<<AdjListWt[i][j].first<<" "<<AdjListWt[i][j].second<<"\t";
}
cout<<endl;
}
}
/*
5 5
1 2
2 3
3 4
4 5
1 4
5 5
1 2 10
2 3 11
3 4 12
4 5 13
2 1 14
*/