# Breadth-first search (BFS)
Breadth-first search (BFS) is a strategy for searching in a graph.
The BFS begins at a root node and inspects all the neighboring nodes. Then for
each of those neighbor nodes in turn, it inspects their neighbor nodes which
were unvisited, and so on.
This image shows the order in which the nodes are expanded:
![image](http://upload.wikimedia.org/wikipedia/commons/3/33/Breadth-first-tree.svg)
Breadth-first search is usually used to find the shortest path between two nodes
in a graph (considering only the number of edges, it won't work for weighted graphs)
The steps are quite simple:
* Put the source node into a queue and mark it as visited
* Repeat until the queue is empty:
- Remove the least recently added node n
- Add each of n's unvisited neighbors to the queue and mark them as visited
## Comparing Breadth-first search and Depth-first search
Both algorithms are used with the same purpose: Search a node in a graph.
The BFS has the bonus advantage of finding the shortest path, while the DFS
makes no guarantees about that.
If finding the shortest path is not the goal, both DFS and BFS have advantages
and disadvantages, depending on the data that you are looking for.
A commomly used example is the search in a family tree. If the person that you are
looking for is alive, then we can assume that this person (node) will be in the bottom
of the graph. That means that the BFS would probably take longer to find this node
than the DFS. If the person is likely closer to the top, the BFS would be more efficient
is most cases.
没有合适的资源?快使用搜索试试~ 我知道了~
一些图形算法的Ruby实现_Ruby_下载.zip
共48个文件
rb:39个
md:9个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 7 浏览量
2023-04-30
10:27:10
上传
评论
收藏 24KB ZIP 举报
温馨提示
一些图形算法的Ruby实现_Ruby_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
一些图形算法的Ruby实现_Ruby_下载.zip (48个子文件)
ruby-graph-algorithms-master
connected_components
connected_components.rb 565B
graph.rb 179B
node.rb 164B
spec.rb 785B
README.md 734B
topological_sort
topological_sort.rb 537B
test.rb 557B
graph.rb 136B
node.rb 287B
README.md 577B
depth_first_search
graph.rb 281B
node.rb 164B
depth_first_search.rb 865B
spec.rb 746B
README.md 1KB
kruskal
edge.rb 287B
union_find.rb 442B
test.rb 963B
graph.rb 266B
node.rb 196B
kruskal.rb 398B
README.md 569B
kosaraju_strong_components
edge.rb 102B
depth_first_order.rb 482B
strong_components.rb 817B
graph.rb 336B
node.rb 196B
spec.rb 927B
README.md 615B
bellman_ford
edge.rb 266B
graph.rb 266B
node.rb 191B
spec.rb 903B
README.md 470B
bellman_ford.rb 930B
breadth_first_search
graph.rb 283B
node.rb 287B
breadth_first_search.rb 2KB
spec.rb 697B
README.md 2KB
dijkstra
dijkstra.rb 2KB
edge.rb 266B
priority_queue.rb 369B
test.rb 1KB
graph.rb 266B
node.rb 191B
README.md 372B
README.md 718B
共 48 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功