Spent some time implementing a graph as an adjacency list. Looking for suggestions on how to improve this implementation.
#include<vector>
#include<iostream>
class Graph{
public:
//n is the number of vertices in graph
Graph(int n)
{
N=n;
E=0;
//for(int i=0;i < n; ++i)
for(;n > 0; n--)
{
std::vector<int> row;
adjList.push_back(row);
}
}
//# of vertices
int Vertices()
{
return N;
}
//# of Edges
int Edges()
{
return E;
}
void addEdge(int v, int w)
{
adjList[v].push_back(w);
adjList[w].push_back(v);
++E;
}
void adjVertices(int v)
{
std::cout<<"Adjacent vertices of"<< v <<std::endl;
for(auto &x : adjList[v])
std::cout<< x << std::endl;
}
private:
int N;
int E;
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);
}