-4

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 :)

  • `vector adj[]` is almost never what you want to receive as an argument in modern C++ - it is really a `vector* adj` which means you have no idea how many elements it refers to – UnholySheep Jul 13 '23 at 13:52
  • 1
    please read about [mcve] and try to provide one. – 463035818_is_not_an_ai Jul 13 '23 at 13:52
  • What is that supposed to be: `vector adj[]` ? This is C-style array of `std::vector` of int, which decays to a pointer. You try to run a range loop over it, and it simply cannot tell what is the "beginning" or "ending" of your pointer. Go for "vector of vector" if you need 2-d array. – pptaszni Jul 13 '23 at 13:53
  • 1
    `vector adj[]` i know only 1 site that promotes this kind of data structure. Forget that it exists. Learn C++ from a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and/or proper courses. Don't trust so-called tutorial sites you find on the internet. Most of them are not worth what you pay for them – 463035818_is_not_an_ai Jul 13 '23 at 13:54
  • `parent[src]=-1` is probably not what you want. – Dorian Jul 13 '23 at 13:55
  • Plus your function says DFS while it should say BFS. – Dorian Jul 13 '23 at 13:57

1 Answers1

0

In this range-based for loop

for(auto nbr: adj)

the variable adj represenets a pointer due to the function declaration

bool cyclicUsingDFS(int src, unordered_map<int,bool>& visited, vector<int> adj[])

where the last parameter is adjusted by the compiler to

bool cyclicUsingDFS(int src, unordered_map<int,bool>& visited, vector<int> *adj)

So the function begin called in the range based for loop can not be applied to a pointer and the compiler issues an error.

You could simplify the code if instead of an array of vectors you used vector of vectors.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335