- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用维基百科的伪代码在 C++ 中编写了这段 BFS 代码。该函数有两个参数 s,t。其中 s 是源节点,t 是目标,如果目标是 fount,则搜索返回目标本身,否则返回 -1。这是我的代码:
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
struct vertex{
vector<int> edges;
bool visited;
};
int dist = 0;
int BFS(vertex Graph[],int v,int target){
deque<int> Q;
Q.push_front(v);
Graph[v].visited = true;
while(!Q.empty()){
int t = Q.back();
Q.pop_back();
if(t == target){
return t;
}
for(unsigned int i = 0;i < Graph[t].edges.size();i++){
int u = Graph[t].edges[i];
if(!Graph[u].visited){
Graph[u].visited = true;
Q.push_front(u);
}
}
}
return -1;
}
int main(){
int n;
cin >> n;
vertex Graph[n];
int k;
cin >> k;
for(int i = 0;i < k; i++){
int a,b;
cin >> a >> b;
a--;
b--;
Graph[a].edges.push_back(b);
Graph[b].edges.push_back(a);
}
for(int i = 0;i < n; i++){
Graph[i].visited = false;
}
int s,t;
cin >> s >> t;
cout << BFS(Graph,s,t);
}
我在维基百科上读到过这个:
Breadth-first search can be used to solve many problems in graph theory, for example:
Finding the shortest path between two nodes u and v (with path length measured by number > > of edges)
我如何更改我的 BFS 函数以返回从 s 到 t 的最短路径,如果不存在路径则返回 -1?
最佳答案
根据定义,广度优先搜索先访问距离起点 d
的所有节点,然后再访问距离 d+1
的任何节点。因此,当您以广度优先顺序遍历图时,第一次遇到目标节点时,您已经通过最短路径到达了那里。
内森 S.'答案是正确的,但我希望这个答案能提供更多关于为什么这样做的直觉。 Paul Dinh 的评论也是正确的;您可以非常简单地修改 Nathan 的答案以跟踪距离而不是实际路径。
关于c++ - 如何使用 BFS 找到两个节点之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171038/
我有一个无向图,我从中重新绘制了相同的图,但采用树状结构(具有层次)。我知道广度优先搜索 (BFS) 算法的工作原理,但我不确定如何从图 --> 树进行转换。 在这里,在 this Wikipedia
只要我没记错,UCS 和 BFS 是一样的,唯一的区别是它不是扩展最浅的节点,而是扩展路径成本最低的节点。 (也为此使用 PriorityQueue 而不是 Queue)所以我复制了我的 BFS 代码
我知道 Dijkstra 的算法、Floyd-Warshall 算法和 Bellman-Ford 算法,用于查找图中 2 个顶点之间的最便宜路径。 但是当所有边的成本都相同时,最便宜的路径是边数最少的
我正在寻找一个代码来找到有向图中的最短路径 a 。 有什么地方可以找到吗? (可以基于BFS) 最佳答案 使用Erlang Digraph Library和函数 get_short_path/3,它采
这个问题已经有答案了: What is the point of the diamond operator (<>) in Java? (7 个回答) 已关闭 6 年前。 我是java初学者,我有BF
我想修改下面的代码以动态从文件中获取数据并运行 BFS。我尝试过循环,但我坚持如何使用匿名对象动态连接节点。 Node nA=new Node("101"); Node nB=new
我正在尝试实现 BFS 来查找学习某门类(class)之前所需的所有先决条件。我的public List computeAllPrereqs(String courseName)方法是我的代码困惑的地
我正在尝试编写一个程序来查找节点 B 是否属于从节点 A 开始的子树。我用 C 编写了代码,并自行实现了队列机制,因为我使用 BFS 来遍历树。问题是我的代码遇到无限循环,说我的队列已满,甚至没有。
我已经制作了 BFS 算法的并行版本,现在我正在尝试序列化相同的算法以了解加速情况。我的代码是这样的: #include #include #include struct Node {
我尝试根据我的研究在 JAVA 中实现 BFS 算法,但我有点困惑,我不确定我是在检查节点是否是目标,还是在将节点添加到探索列表中适当的地方。代码如下: frontier.add(nodes.getF
请帮助我理解我的代码做错了什么。我试图使用 BFS 获得最短路径来解决问题,但它要么给我 -1 要么 2。它应该给我 6 作为答案。我究竟做错了什么?这就是问题所在: 给定一个棋盘,找到马从给定来源到
我最近在解决一个 bfs 问题,其中每个节点都是数组元素的不同排列。但是我无法想出一个合适的数据结构来跟踪扩展树中的访问节点。通常,节点是不同的字符串,因此我们可以只使用映射将节点标记为已访问,但在上
我有一个文件夹结构中的元素列表: /文件夹/myfile.pdf /folder/subfolder1/myfile.pdf /文件夹/子文件夹2/myfile.pdf /folder/subfold
我已经实现了一个 BFS 算法来检测图中的循环,这是以下代码: void hasCycle(node *root,string start){ if(
真的很难弄清楚如何修复我的代码。我知道显然存在错误,因为它没有运行,但我不确定错误到底是什么,也不确定如何修复它们。任何帮助/见解将不胜感激。谢谢!! struct vertices { in
我在图中有代表城镇的顶点。我试图找到从 A 点到 B 点的最短路径。 我创建了一个图形类。 struct Edge{ string name; vector v; Edge(
所以我正在构建这棵树,它有 1..* 个节点,其中每个节点都有一个列表,该列表本身可以有 1..* 个节点。 树的前三层我可以构建得很好,但如果我不编写所有愚蠢的级别,它就不会发挥更多作用。解决方案当
我正在研究 Perfect Squares - LeetCode Perfect Squares Given a positive integer n, find the least number o
我是图论新手,目前只学过图论中的BFS和Disjoint sets。如果在给定的无向连通图中存在一个循环,我可以使用 BFS 找到它吗?我的意图是打印循环中的所有顶点。提前致谢。 最佳答案 是的,如果
我在矩阵上实现 bfs 时遇到问题。似乎我的代码只检查起始节点的子节点。 我的目标是找到从“B”到“H”的最短路径。我还认为我的代码需要大量修改。先感谢您! #include #include #
我是一名优秀的程序员,十分优秀!