-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathgraphs_adjacency_list.cpp
43 lines (42 loc) · 1.01 KB
/
graphs_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
// Graphs Adjacency List implementation
// Program Author : Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
class Graph{
public:
int V;
list<int> *L; // a pointer to an array of linkedlist
Graph(int v){
V = v;
L = new list<int>[V]; // Array of LL
// there is a pointer to an array whose size is v and every object is list of integer
}
void addEdge(int u, int v, bool bidir = true){
L[u].push_back(v);
if(bidir){
L[v].push_back(u);
}
}
void printAdjacencyList(){
for(int i = 0; i < V; i++){
cout << i << "-> ";
for(auto vertex : L[i]){
cout << vertex << ", ";
}
cout << endl;
}
}
};
int main(){
//graph has 5 vertices numbered from 0-4
Graph g(5);
g.addEdge(0, 1);
g.addEdge(0, 4);
g.addEdge(1, 4);
g.addEdge(1, 3);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(4, 3);
g.printAdjacencyList();
return 0;
}