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<