I was implementing cycle detection in an undirected graph using breadth-first search.
However, I seem to be getting the following error:
./Solution.cpp:23:27: error: no matching function for call to 'begin(std::vector*&)' for(auto nbr: adj)
I understand there have been similar questions asked on the platform before but being a beginner, I am still quite confused as to how to fix this in my code specifically. Here's the code:
class Solution {
public:
// Function to detect cycle in an undirected graph.
bool cyclicUsingDFS(int src, unordered_map<int,bool>& visited, vector<int> adj[])
{
queue<int> q;
unordered_map<int,bool> parent;
q.push(src);
visited[src]=true;
parent[src]=-1;
while(!q.empty())
{
int frontnode = q.front();
q.pop();
for(auto nbr: adj)
{
if(!visited[nbr])
{
q.push(nbr);
visited[nbr]=true;
parent[nbr]=frontnode;
}
if(visited[nbr] && nbr!=parent[frontnode]);
{
return true;
}
}
}
return false;
}
bool isCycle(int V, vector<int> adj[])
{
unordered_map<int, bool> visited;
for(int i=0; i<V; i++)
{
if(!visited[i])
{
cyclicUsingDFS(i, visited,adj);
}
}
}
};
Thanks :)