[英]BFS and DFS of a graph in C++
我一直在嘗試對圖表進行BFS和DFS處理。 我已經嘗試了所有方法,但仍然無法弄清楚我的算法出了什么問題。 請幫我解決一下這個。
我將帶有的頂點作為向量發送,該向量指向圖的頂點指向bfs和bfs,以便遍歷它並尋找正確的值。 我對此並不陌生,嘗試解決它時遇到很多問題。 如果有人可以瀏覽我的代碼,看看我的算法在哪里出錯,那將是很好的。 我希望能有新的發現。
它確實顯示了輸出,但是是錯誤的!
這是我的圖表輸入值:: 5 1、5 2、5 3、1 4、1 6
這里1是5的邊緣,2是5的邊緣,3是5的邊緣,依此類推....
這是我得到的輸出:對於BFS:5、1、2、3、4、6
對於DFS:5、4、3、2、1、5
這是我的算法:
#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
您的BFS方法無法正常工作,因為您要向隊列中添加的所有頂點均亂序。 您應該將while ( queue.size > 0 )
循環的內部替換為:
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 << " ";
目前,您添加了頂點1的鄰居,然后添加了頂點2的鄰居,甚至沒有引用您假定的起點s。 您只需要添加當前隊列頂部哪個頂點的鄰居即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.