-
Notifications
You must be signed in to change notification settings - Fork 2
/
adjacency_list.cpp
58 lines (48 loc) · 1.06 KB
/
adjacency_list.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
#include<iostream>
#include<vector>
using namespace std;
vector<vector<int>> adjList;
struct Edge {
int source, destination;
};
class Graph {
public:
Graph(int n) {
adjList.resize(n+1);
}
void addEdge(int src, int dest, int flag) {
adjList[src].push_back(dest);
if(flag) {
adjList[dest].push_back(src);
}
}
void printGraph(int n) {
for(int i = 0; i <= n; i++) {
cout << i << "-->";
for(auto j : adjList[i]) {
cout << j << " ";
}
cout << endl;
}
}
};
int main() {
int n;
cout << "Enter number of vertex : ";
cin >> n;
Graph graph(n);
int e;
cout << "Enter number of edges : ";
cin >> e;
int flag;
cout << "Enter 0 for directed 1 for undirected : ";
cin >> flag;
for(int i = 0; i < e; i++) {
int src, dest;
cout << "Enter source and destintion : ";
cin >> src >> dest;
graph.addEdge(src, dest, flag);
}
graph.printGraph(n);
return 0;
}