简体   繁体   English

在 C++ 中实现图形 DFS,指针问题

[英]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.所以,我正在做一个图形拓扑排序和类似的项目,但我无法让 DFS 工作,我知道我可能会因为使用指针而丢失数据(?),但我不会使用无论如何,我的程序中的这个邻接列表。 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.问题只是为了得到正确的结果,在这种情况下是当顶点被输入 (d[]) 和左 (f[]) 但在递归时我的指针会发疯,有时当我再次返回时(显然没有否则会发生),我认为至少因为这是我第一次使用调试功能。 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:我已经坐了大约 8 个小时(不是我的第一个问题,但我设法解决了一些问题,这就是代码看起来如此丑陋的原因),我坐在这个调试器上,一个多小时没有取得任何进展,所以我决定问一下,我第一次使用这个网站,希望你能帮助我,等我好了一点我一定会回报的,代码如下:

#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.正如我所说它不漂亮,很抱歉打扰你,我需要帮助的是让函数 DFS 正确地产生 d[] 这是一个表,如果整数表示访问顶点的时间,而 f[] - 表当顶点从堆栈中取出时,只是订购 1,2,3...,问题是 - 它在中间断裂,有时它像 7/10 有时只是 2/10 并且它断裂,当然,它也必须适用于更大的图表。 The pointers are lost and it tries to check Nowy[some big number there] and program crashes.指针丢失了,它试图检查 Nowy [那里的一些大数字] 和程序崩溃。

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 很糟糕,犯了很多错误,多亏了一些评论,我决定使用向量向量作为 adj 矩阵,使用 forward_list 向量作为 adj 列表这是改变 m_sasiedztwa 结构的内容

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: l_nast 结构:

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:和 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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