-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Implementation.cpp
More file actions
55 lines (46 loc) · 1.09 KB
/
Graph_Implementation.cpp
File metadata and controls
55 lines (46 loc) · 1.09 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
#include<bits/stdc++.h>
using namespace std;
//A simple Representation Of graph using Adjacency List
void addEdge(vector<int> adj[], int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int> adj[], int Vertex) {
for (int v = 0; v < Vertex ; v++) {
cout << "Adjacency List of Vertex "
<< "\n head" << "(" << v << ")";
for (auto x : adj[v])
cout << "-->" << x;
cout << "\n";
}
}
int main() {
int vertex = 5;
vector<int> adj[vertex];
//Draw Graph For Understanding Purpose
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 2, 4);
addEdge(adj, 2, 5);
addEdge(adj, 3, 5);
addEdge(adj, 3, 4);
addEdge(adj, 4, 5);
printGraph(adj, vertex);
return 0;
}
/* Output
Adjacency List of Vertex
head(0)-->1-->3-->4
Adjacency List of Vertex
head(1)-->0-->2-->4
Adjacency List of Vertex
head(2)-->1-->3-->4-->5
Adjacency List of Vertex
head(3)-->0-->2-->5-->4
Adjacency List of Vertex
head(4)-->0-->1-->2-->3-->5
*/