-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph_Adjlist.cpp
More file actions
61 lines (49 loc) · 956 Bytes
/
Graph_Adjlist.cpp
File metadata and controls
61 lines (49 loc) · 956 Bytes
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
#include<iostream>
#include<list>
using namespace std;
class Graph{
int V;
list<int> *l;
public:
Graph(int v)
{
V = v;
//Array of linked Lists
l = new list<int>[v];
}
void addEdge(int u,int v,bool bidir=true)
{
l[u].push_back(v);
if(bidir)
{
l[v].push_back(u);
}
}
void printAdjList()
{
for(int i=0;i<V;i++)
{
cout<<i<<"->";
///l[i] is a linked list
for(int vertex: l[i])
{
cout<<vertex<<",";
}
cout<<endl;
}
}
};
int main()
{
//Graph has 5 vertices number from 0 to 4
Graph g(5);
g.addEdge(0,1);
g.addEdge(0,4);
g.addEdge(4,3);
g.addEdge(1,4);
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(1,3);
g.printAdjList();
return 0;
}