My data set is a list of Edge
s which are passed as a pair of integers. Based on that, I have the following graph implementation for BFS. Could someone please review my code and comment on errors/omissions/efficiency and other points.
#include <queue>
#include <iostream>
#include <vector>
class Graph {
private:
int num_of_vertices;
std::vector<int>* Adj;
public:
Graph(int V){
this->num_of_vertices = V;
Adj = new std::vector<int>[V];
}
void addEdge(std::vector<std::pair<int, int>* > edge_list)
{
for(auto it = edge_list.begin(); it != edge_list.end(); it++) {
std::pair<int, int>* p = *it;
Adj[p->first].push_back(p->second);
}
}
void BFS(int start)
{
bool* visited = new bool[this->num_of_vertices]();
for(int i=0;i<num_of_vertices;i++)
visited[i]=false;
std::queue<int> queue;
queue.push(start);
std::vector<int>::iterator i;
while(!queue.empty())
{
start = queue.front();
visited[start] = true;
queue.pop();
for (i = Adj[start].begin(); i != Adj[start].end(); i++)
{
if (!visited[*i])
queue.push(*i);
}
}
}
};