我無法爲我的程序創建深度優先搜索。到目前爲止,我有一類邊緣和一類區域。我想將所有連接的邊存儲在我的區域的一個節點內。我可以通過我已經實現的getKey()函數來判斷是否有連接。如果兩個邊緣具有相同的鍵,則它們連接。對於下一個區域,我想在該區域內存儲另一組連接的邊,等等。但是,我沒有完全理解DFS,並且在實現它時遇到了一些麻煩。我不知道何時/何地再次調用DFS。任何幫助,將不勝感激!爲DFS創建鄰接列表
class edge
{
private:
int source, destination, length;
int key;
edge *next;
public:
getKey(){ return key; }
}
class region
{
edge *data;
edge *next;
region() { data = new edge(); next = NULL; }
};
void runDFS(int i, edge **edge, int a)
{
region *head = new region();
aa[i]->visited == true;//mark the first vertex as true
for(int v = 0; v < a; v++)
{
if(tem->edge[i].getKey() == tem->edge[v].getKey()) //if the edges of the vertex have the same root
{
if(head->data == NULL)
{
head->data = aa[i];
head->data->next == NULL;
} //create an edge
if(head->data)
{
head->data->next = aa[i];
head->data->next->next == NULL;
}//if there is already a node connected to ti
}
if(aa[v]->visited == false)
runDFS(v, edge, a); //call the DFS again
} //for loop
}