Monday, 17 November 2014

                           Graph - Adjacency List

#include<vector>

#include<iostream>


class Graph{


public:

     //n is the number of vertices in graph

     Graph(const int n): numberOfVertices(numerOfVertices),numberofEdges(0),adjList(n)

     {

         

     }

     //# of vertices

     const int Vertices()

     {

         return numberOfVertices;

     }

     //# of Edges

     const int Edges()

     {

          return numberofEdges;

     }

     void addEdge(int v, int w)

     {

          adjList[v].push_back(w);

          adjList[w].push_back(v);

          ++numberofEdges;

     }


     void  adjVertices(int v)

     {

        std::cout<<"Adjacent vertices of"<< v <<std::endl;

        for(auto &x : adjList[v])

           std::cout<< x << std::endl;

     }


private:

    int numberOfVertices;

    int numberofEdges;

    std::vector< std::vector<int> > adjList;

};


int main()

{

     Graph G(10);

     G.addEdge(3, 4);

     G.addEdge(0, 2);

     G.addEdge(0, 4);

     G.addEdge(0, 9);

     G.adjVertices(0);

     G.adjVertices(4);

}