I have been learning graphs from geeksforgeeks.org .I have understood what BFS,DFS etc. are,so the problem is not with the algorithm ,but the problem is with the format of their implementation. For every algorithm ,they have used some different format,so i cannot decide upon a single format , so can someone give proper implementation of these ?
Here is what i am thinking to implement(from geeksforgeeks)
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void BFS(int s); // prints BFS traversal from a given source s
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
So, does someone has a better implementation?.
Also, what will be the perks of using adjacency list over adjacency matrix,i mean what will be efficient in contests?