0

The question is to calculate the length of the shortest path to each vertex starting from 0 to all other vertices and store them inside a array and then print that array. I have written the following code but the code is giving me SIGSEGV in all of the online compliers

   #include<bits/stdc++.h>
using namespace std;

class Graph
{
    private :
    int v;
    vector<int>*adj;
    
    public:
      Graph(int v){
          this->v = v;
          adj = new vector<int>[v];
      }
      
      void insert(int x,int y)
      {
         adj[x].push_back(y);
         adj[y].push_back(x);
      }
      
      void print()
      {
        for(int i=0;i<v;i++)
        {
         for(int j=0;j<adj[i].size();j++)
          cout<<adj[i][j]<<" ";
          
          cout<<endl;
        }
      }
      
      void shortestPath(int source)
      {
          cout<<"v here is "<<v<<endl;
        bool visited[v];
        int dist[v];
        for(int i=0;i,v;i++)
         visited[i] = false;
        
        for(int i=0;i<v;i++)
         dist[i] = INT_MAX;
         
        queue<int>q;
        
        q.push(0);
        visited[0] = true;
        dist[0] = 0;
        
        while(!q.empty())
        {
            cout<<"i am here"<<endl;
          int j = q.front();
           q.pop();
           
          for(int i : adj[j])
          {
             if(!visited[i])
             {
                 visited[i] = true;
                 dist[i] = dist[j]+1;
                 q.push(i);
             }
          }
          
        }
  
       cout<<"the output array for the shortest path is"<<endl;        
        for(int i=0;i<v;i++)
          cout<<dist[i]<<" ";
          
      }
};


int main()
{
    Graph g(4);
    g.insert(0,1);
    g.insert(1,2);
    g.insert(2,3);
    g.insert(0,2);
    g.insert(1,3);
    
    g.shortestPath(0);
    
}

please help me identify my mistake my code basically has an adjacency list for my graph thanks a lot in advance.

  • Perchance, what is your intent with `for (int i = 0; i, v; i++)` ? Specifically the conditional statement of that loop, i.e. `i,v` ? What did you think that actually does? Shouldn't that be `i < v` ? – WhozCraig Jun 13 '21 at 05:30
  • There is a comma instead of < in the first hour for loop of shortestPath. What is the screen output? – Sebastian Jun 13 '21 at 05:34
  • that was just a typo thanks – Aakash Tiwari Jun 13 '21 at 05:38
  • No reason for a pointer to a vector, just use a vector instead. That fies the memory leak you have since there's no destructor in your class to clean things up. – Retired Ninja Jun 13 '21 at 05:42
  • A memory leak does not lead to a SIGSEGV! – Sebastian Jun 13 '21 at 05:55
  • 1
    Obligatory links to [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Using both together can get really bad. When you have a mystery error in your code, removing these two lines should be among the first things you try. – user4581301 Jun 13 '21 at 06:11
  • 1
    *"that was just a typo"* - Belaying the mystery of how one introduces a transitive typo when simply copy/pasting real code from your source editor to the question editor, that "typo" will cause an infinite loop that eventually breaches `visited`, invokes undefined behavior, and ultimately crashes your process. Fixing it, [the code works](https://godbolt.org/z/YvMns8onq). So I'd say it's relevant, and will assume the root of your problem unless you specify an updated source in your question showing otherwise. – WhozCraig Jun 13 '21 at 06:14
  • 1
    You aren't getting anywhere near death with this little input, but `bool visited[v];` is a variable length array, a very effective way to cause Stack Overflow because you lose control of the amount of data being allocated from Automatic storage. – user4581301 Jun 13 '21 at 06:18

0 Answers0