From f043fa4ab2a2e873a7388c5f7db93eb0816eaa8b Mon Sep 17 00:00:00 2001 From: Kumar Rahul Date: Wed, 14 Apr 2021 16:36:42 +0530 Subject: [PATCH] Added to code to implement Graph in Data Structures --- Data Structure/Graph/Implementatio/README.md | 8 ++ Data Structure/Graph/Implementatio/graph.cpp | 98 ++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 Data Structure/Graph/Implementatio/README.md create mode 100644 Data Structure/Graph/Implementatio/graph.cpp diff --git a/Data Structure/Graph/Implementatio/README.md b/Data Structure/Graph/Implementatio/README.md new file mode 100644 index 000000000..639e25255 --- /dev/null +++ b/Data Structure/Graph/Implementatio/README.md @@ -0,0 +1,8 @@ +# Starting with graph +Implement a graph, add edges to it and print it. + +# Graph Representation Methods +Adjacency List method + +# Programming Language Used: +Cpp \ No newline at end of file diff --git a/Data Structure/Graph/Implementatio/graph.cpp b/Data Structure/Graph/Implementatio/graph.cpp new file mode 100644 index 000000000..de2c34c6c --- /dev/null +++ b/Data Structure/Graph/Implementatio/graph.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +// ********** graph implementation using array of vectors ************ + +class graph{ + public: + + int v; // will hold the no. of vertices + vector *adj; // A pointer of type Vector + + graph(int v){ + this->v=v; + adj=new vector[v]; // an array of vector is created dynamically + } + void add_edge(int e1, int e2, bool bidir=true){ + adj[e1].push_back(e2); + if(bidir){ + adj[e2].push_back(e1); + } + } + + void print(){ + for(int i=0; i"; + for(auto el: adj[i]){ + cout< *adj; // A pointer of type list + + graphL(int v){ + this->v=v; + adj=new list[v]; // An array of lists is created dynamically + } + + void add_edge(int e1, int e2, bool bidir=true){ + adj[e1].push_back(e2); + if(bidir){ + adj[e2].push_back(e1); + } + } + + void print(){ + for(int i=0; i"; + for(auto el: adj[i]){ + cout<