图的遍历
来源:互联网 发布:有关健身的软件 编辑:程序博客网 时间:2024/05/22 14:43
图的遍历
standford algorithm learning notes
广度优先
Breadth First Search(BFS)
To systematically explore the nodes of the graph beginning with the given starting vertex in layers.
Algorithm
BFS(graph G, start vertex s)
[all nodes initially unexplored]
- mark s as explored.
- let Q = queue data structure (FIFO), initialized with s.
- while Q
≠ ϕ :
- remove the first node from Q, call it v.
- for each edge(v, w):
if w is unexplored:
- mark w as explored
- add w to Q(at the end)
Basic properties
- at the end of BFS, v exposed
↔ G has a path from s to v. - running time of main while loop = O(
ns +ms ), wherens is number of nodes findable from s,ms is number of edges findable from s.
Application : shortest paths
Goal: Compute dist(v), the fewest number of edges on a path from s to v.
Extra code:
- initialize dist(v) =
{ 0, if v = s; or 1, if v≠ s - when considering edge (v, w):
- if w unexplored, then set dist(w) = dist(v) + 1
Claim: at termination, dist(v) = i
深度优先
Depth First Search(DFS)
- much more aggressive search than BFS where you immediately try to plunge as deep as you can only backtracking when absolutely necessary.
- preserve topological ordering in directed graph.
Aogorithm
Recursive version: DFS(graph G, start vertex s)
1. mark s as explored.
2. for every edge(s, v):
- if v unexplored:
- DFS(G, v)
Basic properties
- at the end of the algorithm, v marked as explored
↔ ∃ path from s to v in G.
Application: topological ordering
definition: A topological ordering of a directed graph G is a labelling F of G’s nodes such that :
1. the f(v)’s are the set {1,2,
2. (u, v)
topological sort via DFS:
DFS-loop(graph G)
1. mark s as explored.
2. current_label = n[to keep track of ordering]
3. for each vertex v∈ G:if v unexplored:
DFS(G, v)set f(v) = current_label
current_label−−
- 图:图的遍历(深度优先遍历、广度优先遍历)
- java图的遍历方式(深度遍历、广度遍历)
- java图的遍历方式(深度遍历、广度遍历)
- 图的遍历(深度遍历和广度遍历)
- 图的遍历:深度优先遍历和广度优先遍历
- 图的 深度遍历 广度遍历
- 图的深度遍历和广度遍历
- 图的遍历(深度优先遍历)
- 图的遍历:深度优先遍历
- 图的遍历:宽度优先遍历
- 图的广度遍历和深度遍历
- 图的深度遍历和广度遍历
- 图的遍历-深度优先遍历
- 图的遍历-广度优先遍历
- 图的dfs遍历和bfs遍历
- 图的深度遍历和广度遍历
- 图的遍历算法-马遍历棋盘
- 图的遍历算法-马遍历棋盘
- 下载小常识
- Android中SensorManager.getRotationMatrix函数:计算出旋转矩阵,
- iOS学习总结——多态
- java 数据结构之顺序表
- hdu 1247 Hat’s Words(从给的单词中找hat's word 并按字典序输出)
- 图的遍历
- 定义名字空间
- 数据类型
- 洗牌算法
- 金额转大写及日期转大写(java)
- 问题
- Samp免流软件以及地铁跑酷的自校验分析
- JavaScript数组排序,Array.prototype.sort()
- HDU 5313