0

I tried to get the elements of a graph that are connected and the n print them in separated lines.But there seems to be some error in the code so please help me find it .I would be grateful if you could me where.It takes input of the no of vertices[range(0,V-1)]and edges then it gets all the separate islands in a set and then prints them...the error message is*" munmap_chunk(): invalid pointer: 0x00005633da4e3e88 ***"*

#include<bits/stdc++.h>
using namespace std;
void dfs(vector<int>*edges,int V,int*visited,unordered_set<int>*component,int index)
{
    component->insert(index);
    for(int i=0;i<edges[index].size();i++)
    {
        if(visited[edges[index][i]]==0)
        {  
            visited[edges[index][i]]=1;
            dfs(edges,V,visited,component,edges[index][i]);
        }
    }
}
unordered_set<unordered_set<int>*>*getComponents(vector<int>*edges,int V)
{
    int*visited=new int[V]();
    unordered_set<unordered_set<int>*>*components=new unordered_set<unordered_set<int>*>(); 
    for(int i=0;i<V;i++)
    {
        if(visited[i]==0)
        {
            visited[i]=1;
            unordered_set<int>*component=new unordered_set<int>;
            dfs(edges,V,visited,component,i);
            components->insert(component);
        }
    }
    return components;
}
int main()
{
    // cout<<"enter the no of vertices and the no of edges in your graph "<<endl;
    int V,E;
    cin>>V>>E;
    vector<int>*edges=new vector<int>[V+1];
    for(int i=0;i<E;i++)
    {
        int a,b;
        cin>>a>>b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    unordered_set<unordered_set<int>*>*components=getComponents(edges,V);
    unordered_set<unordered_set<int>*>::iterator it = components->begin();
    while (it != components->end()) {
        //cout<<"component::"<<it->begin()<<endl;
        unordered_set<int>* component = *it;
        unordered_set<int>::iterator it2 = component->begin();
        while (it2 != component->end()) {
            cout << *it2 << " ";
            it2++;
        }
        cout << endl;
        delete component;
        it++;
    }
    delete components;
    delete edges;

}
  • 3
    Why do you think there's an error? Do you get a compilation error? A runtime error? The wrong results? – Stephen Newell Apr 24 '20 at 12:39
  • 1
    when you already know that it is bad, why do you still do it? [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Apr 24 '20 at 12:41
  • 1
    What's with all the `new[]`? You already have containers, why are you still throwing in random `new[]` calls everywhere? That only leads to memory leaks and worse performance in this code – UnholySheep Apr 24 '20 at 12:43
  • To start, you prompt the user to enter "the no of vertices and the no of edges". But you actually read number of edges first, and then number of vertices. You likely end up with the two swapped, if users actually follow your directions. – Igor Tandetnik Apr 24 '20 at 12:48
  • 1
    `it.begin()` shouldn't compile. – Igor Tandetnik Apr 24 '20 at 12:51
  • thank you every one great help – nilotpal das Apr 24 '20 at 13:17
  • You've got a bad pointer. Run your code with `valgrind `or, if you're using clang/gcc, compile with address sanitization turned on. – Stephen Newell Apr 24 '20 at 13:24

0 Answers0