C++ 宽度优先搜索代码中需要澄清,因为输出不';这与我的理解不符
我从techiedelight找到了这段代码[https://www.techiedelight.com/breadth-first-search/]对于BFS 代码如下:C++ 宽度优先搜索代码中需要澄清,因为输出不';这与我的理解不符,c++,algorithm,graph-theory,breadth-first-search,C++,Algorithm,Graph Theory,Breadth First Search,我从techiedelight找到了这段代码[https://www.techiedelight.com/breadth-first-search/]对于BFS 代码如下: #include <iostream> #include <queue> #include <vector> using namespace std; // Data structure to store graph edges struct Edge { int src, de
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// Data structure to store graph edges
struct Edge {
int src, dest;
};
// Class to represent a graph object
class Graph
{
public:
// construct a vector of vectors to represent an adjacency list
vector<vector<int>> adjList;
// Graph Constructor
Graph(vector<Edge> const &edges, int N)
{
// resize the vector to N elements of type vector<int>
adjList.resize(N);
// add edges to the undirected graph
for (auto &edge: edges)
{
adjList[edge.src].push_back(edge.dest);
adjList[edge.dest].push_back(edge.src);
}
}
};
// Perform BFS recursively on graph
void recursiveBFS(Graph const &graph, queue<int> &q,
vector<bool> &discovered)
{
if (q.empty())
return;
// pop front node from queue and print it
int v = q.front();
q.pop();
cout << v << " ";
// do for every edge (v -> u)
for (int u : graph.adjList[v])
{
if (!discovered[u])
{
// mark it discovered and push it into queue
discovered[u] = true;
q.push(u);
}
}
recursiveBFS(graph, q, discovered);
}
// Recursive C++ implementation of Breadth first search
int main()
{
// vector of graph edges as per above diagram
vector<Edge> edges = {
{1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 9},
{5, 10}, {4, 7}, {4, 8}, {7, 11}, {7, 12}
// vertex 0, 13 and 14 are single nodes
};
// Number of nodes in the graph
int N = 15;
// create a graph from edges
Graph graph(edges, N);
// stores vertex is discovered or not
vector<bool> discovered(N, false);
// create a queue used to do BFS
queue<int> q;
// Do BFS traversal from all undiscovered nodes to
// cover all unconnected components of graph
for (int i = 0; i < N; i++) {
if (discovered[i] == false)
{
// mark source vertex as discovered
discovered[i] = true;
// push source vertex into the queue
q.push(i);
// start BFS traversal from vertex i
recursiveBFS(graph, q, discovered);
}
}
return 0;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 7 6 9 10 11 13
然而,我不明白如果顶点0、13和14不在图形中,它是如何进入输出的?此外,根据我的理解,该输入的BFS输出与我的理解不同,应如下所示:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// Data structure to store graph edges
struct Edge {
int src, dest;
};
// Class to represent a graph object
class Graph
{
public:
// construct a vector of vectors to represent an adjacency list
vector<vector<int>> adjList;
// Graph Constructor
Graph(vector<Edge> const &edges, int N)
{
// resize the vector to N elements of type vector<int>
adjList.resize(N);
// add edges to the undirected graph
for (auto &edge: edges)
{
adjList[edge.src].push_back(edge.dest);
adjList[edge.dest].push_back(edge.src);
}
}
};
// Perform BFS recursively on graph
void recursiveBFS(Graph const &graph, queue<int> &q,
vector<bool> &discovered)
{
if (q.empty())
return;
// pop front node from queue and print it
int v = q.front();
q.pop();
cout << v << " ";
// do for every edge (v -> u)
for (int u : graph.adjList[v])
{
if (!discovered[u])
{
// mark it discovered and push it into queue
discovered[u] = true;
q.push(u);
}
}
recursiveBFS(graph, q, discovered);
}
// Recursive C++ implementation of Breadth first search
int main()
{
// vector of graph edges as per above diagram
vector<Edge> edges = {
{1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 9},
{5, 10}, {4, 7}, {4, 8}, {7, 11}, {7, 12}
// vertex 0, 13 and 14 are single nodes
};
// Number of nodes in the graph
int N = 15;
// create a graph from edges
Graph graph(edges, N);
// stores vertex is discovered or not
vector<bool> discovered(N, false);
// create a queue used to do BFS
queue<int> q;
// Do BFS traversal from all undiscovered nodes to
// cover all unconnected components of graph
for (int i = 0; i < N; i++) {
if (discovered[i] == false)
{
// mark source vertex as discovered
discovered[i] = true;
// push source vertex into the queue
q.push(i);
// start BFS traversal from vertex i
recursiveBFS(graph, q, discovered);
}
}
return 0;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 7 6 9 10 11 13
有人能解释我的怀疑是对的还是错的吗?如果有人能澄清我的疑问,这会很有帮助。for循环从0到N(15),因此您目前有一种非常精细的方法来打印图形中的所有节点。例如,您可以使用union find来标识连接节点的群集。@Botje是图形的正确顺序(显示输出的方式)。我知道节点13,14来自N=15。它打印的顺序是否正确?在本例中看起来是这样,但通常不能依赖于邻接列表中节点的顺序,也不能依赖于图中节点的顺序。这就是为什么我说你最好用BFS做一些有用的事情,而不仅仅是打印节点。你正在执行4个BFS搜索,根为0、1、13和14。我还认为BFS的递归本质上是浪费——有一个队列,为什么需要(程序)堆栈?从我的观点来看,递归的主要优点是自由堆栈…@tucuxi如果我从代码中删除队列,它将如何成为BFS,然后有效地成为DFS,对吗?