简体   繁体   中英

BFS and DFS of a graph in C++

I have been trying to do a BFS and DFS of my graph. I have tried everything but still cannot figure out what is wrong with my algorithm. Please help me with this.

I send the vertices with is my vector that points to the vertexes of my graph to the bfs and bfs in order to go through it and seek for the correct values. I am new to this and am having a lot of problem trying to solve it. It would be nice if someone could go through my code and see where I am wrong in my algorithm. I hope a fresh pair of eyes would be able to detect the problem.

It does show the output but is wrong!

This is my input values for graph:: 5 1, 5 2, 5 3, 1 4, 1 6

Here 1 is the edge of 5, 2 is the edge of 5, 3 is the edge of 5 and so on....

And this is the output I get: for BFS: 5,1,2,3,4,6

for DFS: 5,4,3,2,1,5

Here is my algorithm for it:

#ifndef SORT_HPP
#define SORT_HPP

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <vector>
#include <stack>
#include <algorithm>
#include "clsVertex.hpp"
#include "clsFileGraph.hpp"
#include "clsGraph.hpp";

class bfs : public clsGraph{
    int vert;

public:
    bfs(int s, vector<clsVertex*> verticies); //prints BFS traversal from a given source

};

class dfs: public clsGraph{
    int vert;
    list<int> adj;

public:
    dfs(int s, vector<clsVertex*> verticies);   //prints DFS traversal from a given source

};

bfs::bfs(int s, vector<clsVertex*> verticies){

    bool *visited = new bool[verticies.size()]; //creates a new boolean array of the size of the graph

    for (int i = 0; i < verticies.size(); i++){ //loops till the end of the graph
        int x = verticies[i]->ID;   //gets the value of each vertex
            //cout << "The val: " << verticies[i]->ID << endl;
            visited[x] = false; //marks that vertex as unvisited i.e: visited = false
    }

    list<int> queue;    //creates a queue
    visited[s] = true;  //marks the starting point as visited
    queue.push_back(s); //adds the starting point to the queue
    cout << endl << "The breath first sort is as follows:-" << endl << endl;
    while (queue.size() != 0){  //loops until the size of the queue is 0 
        for (int i = 0; i < verticies.size(); i++){ //loops 
            int y = verticies[i]->edges.size();
            for (int j = 0; j < y; j++){
                int z = verticies[i]->edges[j]->ID;

                if (visited[z]== false){
                    visited[z] = true;
                    queue.push_back(z);
                }

            }
        }
        cout << s << ",";
        queue.pop_front();
        if (queue.size() == 0)
            goto here;
        s = queue.front();

    }
    here:
    cout << ID << " " << graphType << endl;
    for (int i = 0; i < verticies.size(); i++){
        cout << verticies[i]->ID << "->";
        for (int j = 0; j < verticies[i]->edges.size(); j++){
            cout << verticies[i]->edges[j]->ID << endl;
        }
    }

    cout << endl << endl << "Done" << endl << endl;
}




// DFS traversal of the vertices reachable from v. It uses recursive DFSUtil()
dfs::dfs(int s, vector<clsVertex*> verticies)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[verticies.size()]; //creates a new boolean array of the size of the graph

    for (int i = 0; i < verticies.size(); i++){ //loops till the end of the graph
        int x = verticies[i]->ID;   //gets the value of each vertex
        //cout << "The val: " << verticies[i]->ID << endl;
        visited[x] = false; //marks that vertex as unvisited i.e: visited = false
    }
    stack <int> depth;
    visited[s] = true;
    depth.push(s);
    //cout << s << ",";
    while (depth.size() != 0){  //loops until the size of the queue is 0 
        for (int i = 0; i < verticies.size(); i++){ //loops 
            int y = verticies[i]->edges.size();
            for (int j = 0; j < y; j++){
                int z = verticies[i]->edges[j]->ID;

                if (visited[z] == false){
                    visited[z] = true;
                    depth.push(z);
                }

            }
        }
        cout << s << ",";
        depth.pop();
        if (depth.size() == 0)
            goto there;
        s = depth.top();

    }
there:
    cout << "done";

}
#endif

Your BFS method isn't working because you're adding vertices to your queue all out of order. You should replace the inside of your while ( queue.size > 0 ) loop with something like:

s = queue.pop_front();
for ( int i = 0; i < vertices[s]->edges.size(); i++ ) {
    int tmp = vertices[s]->edges[i]->ID;
    if ( !visited[tmp] ) {
        visited[tmp] = true;
        queue.push_back(tmp);
    }
}
cout << s << " ";

As you have it at the moment, you add vertex 1's neighbours, then vertex 2's, and don't even reference your supposed starting point s. You need to add only the neighbours of whichever vertex is currently at the front of your queue.

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