使用dfs和bfs进行路径查找

时间:2016-08-25 16:45:20

标签: c++ algorithm depth-first-search breadth-first-search

我的无向图是这样的: -

{0,1,1,0,0,  
 1,0,0,1,0,  
 1,0,0,1,0,  
 0,1,1,0,1,  
 0,0,0,1,0};  

我想找到两个节点之间的路径,使用bfs和dfs作为我的作业 当我执行bfs我只是打印应该是最小路径的节点,但它显示01234但我想要的是0134或0234,当我执行dfs它给出01324.
我的bfs代码: -

#include <iostream>
#include<queue>

using namespace std;

int main(){
int matrix[5][5]={0,1,1,0,0,
                  1,0,0,1,0,
                  1,0,0,1,0,
                  0,1,1,0,1,
                  0,0,0,1,0};



//int nodes[3] = {5,6,7};
int visited[5] = {0,0,0,0,0};
int inNode,pathNode;
cout<<"Enter Initial Node: ";
cin>>inNode;
cout<<"Enter Path Node: ";
cin>>pathNode;
int traceBack[5],rec=0;
queue<int> myqueue;
myqueue.push(inNode);
int node = myqueue.front();
visited[inNode] = 1;
//cout<<node<<"\n";


while(!myqueue.empty()){
    int s = myqueue.front();
    myqueue.pop();

    for(int i =0 ; i<5;i++){
        if(matrix[s][i] == 1 && visited[i] == 0){
            myqueue.push(i);
            visited[i] = 1;
            traceBack[rec] = i;
            rec++;
        }

    }
}
    cout<<inNode;
    int j = 0;
    while(traceBack[j]!=pathNode){
        cout<<"->"<<traceBack[j];
        j++;
    }
    cout<<"->"<<pathNode;
return 0;

}

我的dfs代码: -

#include<iostream>

using namespace std;

int visited[5]= {0,0,0,0,0};
int traceBack[5],rec=0;
int matrix[5][5]={0,1,1,0,0,
                      1,0,0,1,0,
                      1,0,0,1,0,
                      0,1,1,0,1,
                      0,0,0,1,0};

void dfs(int v){
    int i;
    visited[v]=1;
    for(i= 0 ; i<5;i++){
        if(matrix[v][i] && visited[i]==0){
            traceBack[rec] = i;
            rec++;
            cout<<i;
            dfs(i);
        }
    }
}
int main(){
    int inNode,pathNode;
    cout<<"Enter Initial Node: ";
    cin>>inNode;
    cout<<"Enter Path Node: ";
    cin>>pathNode;
    dfs(inNode);
    int k=0;
    while(traceBack[k]!=pathNode)
    {
        k++;
        cout<<traceBack[k];
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

问题是,对于BFS,您不能使用与用于DFS的相同方法进行追溯。您可以修改您的代码,如下所示:

while(!myqueue.empty()){
    int s = myqueue.front();
    myqueue.pop();

    for(int i =0 ; i<5;i++){
        if(matrix[s][i] == 1 && visited[i] == 0){
            myqueue.push(i);
            visited[i] = 1;
            traceBack[i] = s;
        }

    }
}

所以,现在,traceBack包含每个节点的父节点。要查找从节点4到0的路径:

int j = pathNode;
while(traceBack[j]!=0){
    cout<<"<-"<<traceBack[j];
    j++;
}
cout<<"<-"<<0;

答案 1 :(得分:1)

问题在于&#34; traceBack&#34;实际上并没有追溯到。它只包含访问节点的顺序,这不一定是您想要的路径。

你需要做什么?

当某个节点访问另一个节点i时,则traceBack [i] = s。为什么?因为它说我是从s访问的,所以每个节点都可以跟踪其追溯。 (你也初始化了traceBack [inNode] = -1,因为任何人都没有访问过这个节点)

现在,当算法完成时,以下代码将为您提供路径。 (它首先以相反的顺序获取路径,然后将其反转以获得正确的顺序)

int i = pathNode;

int path[1000];
int path_len = 0;

//this gives you the path in reverse order
while(traceBack[i] != -1){ // there is something to trace
    path[path_len++] = i;   
    i = traceBack[i];
}
path[path_len++] = inNode; // the inNode is left out in the while loop

//printing the path in right order
for(int j = path_len - 1; j >= 0; j—-){
    cout << path[j] << " -> ";
}