[英]Implementing a Graph DFS in C++, pointers issue
預先抱歉,但有些變量名稱和諸如此類的名稱很奇怪,因為我的母語不是英語,我對編程也很陌生。
所以,我正在做一個圖形拓撲排序和類似的項目,但我無法讓 DFS 工作,我知道我可能會因為使用指針而丟失數據(?),但我不會使用無論如何,我的程序中的這個鄰接列表。 問題只是為了得到正確的結果,在這種情況下是當頂點被輸入 (d[]) 和左 (f[]) 但在遞歸時我的指針會發瘋,有時當我再次返回時(顯然沒有否則會發生),我認為至少因為這是我第一次使用調試功能。 我已經坐了大約 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;
}
正如我所說它不漂亮,很抱歉打擾你,我需要幫助的是讓函數 DFS 正確地產生 d[] 這是一個表,如果整數表示訪問頂點的時間,而 f[] - 表當頂點從堆棧中取出時,只是訂購 1,2,3...,問題是 - 它在中間斷裂,有時它像 7/10 有時只是 2/10 並且它斷裂,當然,它也必須適用於更大的圖表。 指針丟失了,它試圖檢查 Nowy [那里的一些大數字] 和程序崩潰。
所以,我使用 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
{
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);}
}
}
}
};
和 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.