简体   繁体   中英

Implementing a Graph DFS in C++, pointers issue

Sorry in advance, but some variable names and such have strange names, as I am not a native English speaker, I am also pretty new to programming.

So, I was doing a project with graphs topology sorting and similar, but I can't get DFS to work, I know that I am probably losing data because of usage of pointers in there(?), but I won't be using this adjacency list in my program any further anyway. The problem is just to get right result which in this case is when the vertex was entered (d[]) and left (f[]) but while in recursion my pointers go crazy, sometimes when I go back in recurrence (and apparently nothing else happens), I think at least because it's the first time I am using debugging function. I am sitting at this for like 8 hours already (not my first problem, but I managed to solve some, that's why code looks so ugly), and I was sitting with this debugger and didn't make any progress in over an hour, so I decided to ask, my first time using this website, I hope you can help me, and when I am a bit better I will definitely return the favor, here's the code:

#include <iostream>
#include <cstdlib>
#include <time.h>
struct m_sasiedztwa
{
    int s;
    int** m;
    m_sasiedztwa(int a,float b) : s(a)
    {
        m = new int*[s];
        for(int i = 0; i < s; ++i)  m[i] = new int[s];
        for(int j=0; j<s; ++j)
            for(int k=0; k<s; ++k) if(j!=k) m[j][k]=((rand()%100)>(100*b))? 0 : 1; else m[j][k]=0;

    }
    ~m_sasiedztwa()
    {
        delete[] m;
    }
};
struct lista
{
    int key;
    lista *next;
};
struct l_nast
{
    int s;
    lista** arr;
    l_nast(int** m, int a) : s(a)
    {
        lista *curr,*prev;
        arr = new lista*[s];
        for(int i=0;i<s;++i)
        {
            arr[i] = new lista;
            curr = arr[i];
            prev=curr;
            prev->key=-1;
            for(int j=0;j<s;++j)
            {
                if(m[i][j]==1) {curr->next= new lista;curr->key=j;prev=curr;curr=curr->next;}

            }
            prev->next=nullptr;
        }

    }

~l_nast() {delete[] arr;}
};


//Here is the issue

bool *Nowy;
int c;
int* d,*f;
void DFS(int j,l_nast l_a)
{

            Nowy[j]=false;
            d[j]=c++;
            std::cout<<"Am I here yet..."<<j<<" "<<c<<std::endl;
                while((l_a.arr[j]!=nullptr)&&(l_a.arr[j]->key!=-1))
                {
                    std::cout<<"checking "<<(l_a.arr[j]->key)<<"...\n";
                    if(Nowy[l_a.arr[j]->key])
                    {

                    DFS(l_a.arr[j]->key,l_a);

                    }

                    if(l_a.arr[j]->next!=nullptr)                               //And I think this may be the problem, but I honestly don't know
                        l_a.arr[j]=l_a.arr[j]->next;
                            else break;
                }

            f[j]=c++;std::cout<<"Yohoo!"<<j<<" "<<c<<std::endl;

}




//Untill there

using namespace std;



int main()
{
    srand (time(NULL));
    for(int q=5; q<6; q+=5)
    {

        m_sasiedztwa a = m_sasiedztwa(q, 0.2);
        m_sasiedztwa b = m_sasiedztwa(q, 0.4);

        l_nast l_a = l_nast(a.m,q);
        l_nast l_b = l_nast(b.m,q);
      /*
           for(int i=0; i<q; ++i)
            {
                for(int j=0; j<q; ++j)
                {
                    cout << a.m[i][j] << " ";
                }
                cout<<endl;
            }
            cout<<endl;
*/

        Nowy = new bool [q];
        d = new int [q];
        f = new int [q];
        c=0;
    for (int i = 0; i < q; i++)
        Nowy[i] = true;
/*for(int qq=0;qq<q;qq++)
  while((l_a.arr[qq]!=nullptr))
  {
      cout<<l_a.arr[qq]->key<<endl;
      l_a.arr[qq]=l_a.arr[qq]->next;

  }
*/

        for(int j=0;j<q;j++)
        {

            if(Nowy[j]) DFS(j,l_a);

        }



        a.~m_sasiedztwa();
        b.~m_sasiedztwa();

        l_a.~l_nast();
        l_b.~l_nast();
    }

    return 0;
}

As I said it's not pretty, sorry for troubling you, again what I need help with is to get function DFS to properly result with d[] which is a table if integers which indicate when the vertex was visited, and f[] - table when the vertex was taken from the stack, just ordering 1,2,3..., the problem is - it breaks in the middle, sometimes it does like 7/10 sometimes just 2/10 and it breaks, of course, it will have to work for bigger graphs as well. The pointers are lost and it tries to check Nowy[some big number there] and program crashes.

So, I used struct badly and made many mistakes, thanks to some comments I decided to use vector of vectors for adj matrix and vector of forward_list for adj list Here is what changed m_sasiedztwa struct

struct m_sasiedztwa
{
    int s;
    std::vector<std::vector<int>> m;
    m_sasiedztwa(int a,float b) : s(a), m(s, std::vector<int>(s,0))
    {
        for(int j=0; j<s; ++j)
            for(int k=0; k<s; ++k)
                if(j!=k)
                    m[j][k]=((rand()%100)>(100*b))? 0 : 1;
     }
};

l_nast struct:

struct l_nast
{
    int s;
    std::vector<std::forward_list<int>> arr;
    l_nast(std::vector<std::vector<int>> m, int a) : s(a), arr(s)
    {
        for(int i=0;i<s;++i)
        {
            auto it = arr[i].before_begin();
            for(int j=0;j<s;++j)
            {
                if(m[i][j]==1) {it = arr[i].emplace_after(it,j);}

            }
        }

    }

};

and the DFS:

void DFS(int j,l_nast l_a)
{

            Nowy[j]=false;
            d[j]=c++;
            std::cout<<"Visiting "<<j<<"as "<<c<<std::endl;
        auto x=l_a.arr[j].begin();

        for(auto& x: l_a.arr[j])
        {
                    if(Nowy[x])
                    {

                    DFS(x,l_a);

                    }

        }
            f[j]=c++;std::cout<<"Leaving "<<j<<"as "<<c<<std::endl;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM