From 4800d8ab1b89961f26fb89e7826d9095a8fa4000 Mon Sep 17 00:00:00 2001 From: 17-Vicky <56847891+17-Vicky@users.noreply.github.com> Date: Tue, 22 Oct 2019 02:18:12 +0530 Subject: [PATCH] graph representation --- Data Structures/graph_representation.cpp | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Data Structures/graph_representation.cpp diff --git a/Data Structures/graph_representation.cpp b/Data Structures/graph_representation.cpp new file mode 100644 index 0000000..55006b5 --- /dev/null +++ b/Data Structures/graph_representation.cpp @@ -0,0 +1,41 @@ + +#include +using namespace std; + +// A utility function to add an edge in an +// undirected graph. +void addEdge(vector adj[], int u, int v) +{ + adj[u].push_back(v); + adj[v].push_back(u); +} + +// A utility function to print the adjacency list +// representation of graph +void printGraph(vector adj[], int V) +{ + for (int v = 0; v < V; ++v) + { + cout << "\n Adjacency list of vertex " + << v << "\n head "; + for (auto x : adj[v]) + cout << "-> " << x; + printf("\n"); + } +} + +// Driver code +int main() +{ + int V = 5; + vector adj[V]; + addEdge(adj, 0, 1); + addEdge(adj, 0, 4); + addEdge(adj, 1, 2); + addEdge(adj, 1, 3); + addEdge(adj, 1, 4); + addEdge(adj, 2, 3); + addEdge(adj, 3, 4); + printGraph(adj, V); + return 0; +}