BFS 和 DFS 的图形在C++
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。您只需要添加当前位于队列前面的任何顶点的邻居。
相关文章:
- C++图形类指针混淆
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 这是实现图形的坏方法吗
- 打印 ONNXRUNTIME::图形没有模型
- 提升如何在图形可视化中写入边缘的权重?
- Doxygen - 如何在不生成图形的情况下生成文本调用关系结果
- C++ 中的图形菜单
- 将图形属性与 std::unique_ptr 捆绑在一起
- 从流到邻接列表的向量读取图形
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 直接显示手动图形内存泄漏
- Directx 12 :在两个进程之间共享图形内存
- Microsoft C++ 用于图形 API 的 Rest SDK
- 如何将到达图形视图右侧(末端)的QGraphicsPixmapItem移动到左侧(开始)侧(就像在贪吃蛇游戏中发生的事情
- 图形初学者C++(跳过控制台)?
- 在 cpp 中最简单的图形实现
- 提升图形库:资源受限的最短周期
- 使用 stl 库创建图形是如何工作的?