简体   繁体   English

深度优先搜索过早终止

[英]Depth-First-Search terminating prematurely

In this question I asked some questions about implementing my own graph-creating data structure. 这个问题中,我问了一些有关实现自己的图形创建数据结构的问题。 Now that it is finally working I am implementing basic DFS and BFS. 现在它终于可以工作了,我正在实现基本的DFS和BFS。 My BFS is working with a queue class that I also created myself. 我的BFS正在使用我自己创建的队列类。 My DFS however is not working, and I can't seem to figure out why. 但是,我的DFS无法正常工作,而且我似乎无法弄清楚原因。 It only works starting on the first node and finding the first adjacent node ("USA"). 它仅在第一个节点开始并找到第一个相邻节点(“ USA”)时有效。 Any other combination returns the NULL pointer (so no 'real' run-time errors). 任何其他组合都将返回NULL指针(因此不会出现“实际”运行时错误)。 Below is the code, where I deleted unrelated routines and content that is already working. 下面是代码,在这里我删除了不相关的例程和已经正常工作的内容。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

template<class T>
class Node{
    T data;
    vector<Node<T>*> adjacent;
    public:
        bool visited;
        int n;
        Node(T initData) : data(initData), visited(false),n(0){}
        void addAdjacent(Node<T>& other){//call from within GraphCl class. push_back in adjacent}
        T getData(){return data;}
        Node<T>* getEdge(int edgeNum){return adjacent[edgeNum];}
};

template<class T>
class GraphCl{
    int n;
    vector<Node<T>*> nodes;
    public:
        GraphCl(int size=0): n(size) {//[...]}
        ~GraphCl(){//[...]}
    void addNode(T input){//push_back new node to nodes vector}
    void addEdge(int baseNode, int edgeNode){//push_back edgeNode adres in adjacent vector}
    Node<T>* getNodeAddress(int idx){return nodes[idx];}
};

template<class T>
Node<T>* dfs(Node<T>* rootNode, T key){
    if(rootNode->visited == false){
        rootNode->visited = true;
        if (rootNode->getData() == key){
            return rootNode;
        }
        for(int i=0;i<rootNode->n;i++){
            if(dfs(rootNode->getEdge(i),key) != NULL){
                return dfs(rootNode->getEdge(i),key);
            }
        }
    }
    return NULL;
}

int main(){
    GraphCl<string> *myGraph = new GraphCl<string>();
    myGraph->addNode("USA");//0
    myGraph->addNode("Australia");//1
    myGraph->addNode("The_Netherlands");//2
    //more nodes added here ...
    myGraph->addNode("Indonesia");
    myGraph->addNode("Canada");
    myGraph->addNode("Czech_Republic");
    myGraph->addEdge(0,1);
    myGraph->addEdge(0,4);
    myGraph->addEdge(2,10);
    //more edges added here ...
    myGraph->addEdge(3,1);
    myGraph->addEdge(11,12);
    myGraph->addEdge(6,11);
    Node<string> *rootNode = myGraph->getNodeAddress(4);
    cout << dfs<string>(rootNode, "USA")->getData();
    return 0;
}

Is there something crucially wrong with the DFS function that one can spot immediately? DFS功能是否存在可以立即发现的严重错误? Cannot figure it out. 无法弄清楚。

This is the problem : 这就是问题 :

if(dfs(rootNode->getEdge(i),key) != NULL){
                return dfs(rootNode->getEdge(i),key);
            }

You are calling dfs twice here on the same node. 您在同一节点上两次调用dfs The second time you call it, rootNode->visited will be true. 第二次调用它时, rootNode->visited将为true。 In this situation, your code returns NULL . 在这种情况下,您的代码将返回NULL To fix it, change the if block to 要修复它,请将if块更改为

Node<T>* val = dfs(rootNode->getEdge(i),key);
if(val != NULL){
                    return val;
                }

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

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