简体   繁体   中英

Path finding using dfs and bfs

my undirected graph is this:-

{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};  

i want to find the path between two nodes using bfs and dfs as my assignment.
when i perform bfs i simply print the nodes which should be minimum path but it shows 01234 but i want is 0134 or 0234 and when i perform dfs it gives 01324.
my bfs code:-

#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;

}

my dfs code:-

#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;
}

The problem is that the "traceBack" is not actually tracing back. It just contains the order in which nodes were visited, which is not necessarily the path that you want.

What you need to do?

When some node s accesses another node i, then traceBack[i] = s. Why? because it says that i was accessed from s, this way every node can follow its trace back . (you also initialize traceBack[inNode] = -1 since this node was not accessed by anybody)

Now, when the algorithm is finished the following code will give you the path. (It first gets the path in reverse order and then reverses it to get the correct order)

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] << " -> ";
}

You problem is, for BFS, you cannot use the same method to trace back as you use for DFS. You may modify your code as follow:

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;
        }

    }
}

So, now, traceBack contains the parent of each node. To find path from node 4 to 0:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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