您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 如何进行数据结构之图的深度优先搜索
## 一、深度优先搜索(DFS)概述
深度优先搜索(Depth-First Search, DFS)是图遍历算法中最经典的算法之一,其核心思想是"一条路走到黑,再回头探索"。该算法会尽可能深地探索图的分支,直到当前分支所有节点都被访问过,再回溯到上一个节点继续探索未访问的分支。
### 1.1 基本特点
- 后进先出的探索方式(天然适合栈结构实现)
- 时间复杂度:O(V+E)(V为顶点数,E为边数)
- 空间复杂度:O(V)(最坏情况下需要存储整条路径)
## 二、DFS算法实现步骤
### 2.1 递归实现(最直观的方式)
```python
def dfs_recursive(graph, node, visited=None):
if visited is None:
visited = set()
visited.add(node)
print(node) # 处理当前节点
for neighbor in graph[node]:
if neighbor not in visited:
dfs_recursive(graph, neighbor, visited)
def dfs_iterative(graph, start):
visited = set()
stack = [start]
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
print(node) # 处理当前节点
# 注意压栈顺序与递归顺序相反
for neighbor in reversed(graph[node]):
if neighbor not in visited:
stack.append(neighbor)
必须使用访问标记(visited集合)来防止: - 无向图中的来回震荡 - 有向图中的环路导致的无限循环
根据业务需求选择邻接节点的访问顺序: - 升序/降序排列(影响生成树形态) - 加权图中的优先级处理
def dfs_disconnected(graph):
visited = set()
for node in graph:
if node not in visited:
dfs_recursive(graph, node, visited)
def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath:
return newpath
return None
def topological_sort(graph):
visited = set()
result = []
def dfs(node):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor)
result.append(node) # 后序添加
for node in graph:
if node not in visited:
dfs(node)
return result[::-1] # 反转结果
def has_cycle(graph):
visited = set()
recursion_stack = set()
def dfs(node):
visited.add(node)
recursion_stack.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
if dfs(neighbor):
return True
elif neighbor in recursion_stack:
return True
recursion_stack.remove(node)
return False
for node in graph:
if node not in visited:
if dfs(node):
return True
return False
结合BFS和DFS优点,适用于状态空间未知的情况:
def ids(graph, start, max_depth):
for depth in range(max_depth):
visited = set()
if dls(graph, start, depth, visited):
return True
return False
def dls(graph, node, depth, visited):
if depth == 0 and node == target:
return True
if depth > 0:
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
if dls(graph, neighbor, depth-1, visited):
return True
return False
特性 | DFS | BFS |
---|---|---|
数据结构 | 栈 | 队列 |
空间复杂度 | O(h)(h为最大深度) | O(w)(w为最大宽度) |
适用场景 | 拓扑排序、连通性检测 | 最短路径、层级遍历 |
深度优先搜索通过其”纵向深入”的探索方式,成为解决图论问题的利器。掌握DFS需要理解: 1. 递归调用栈的本质 2. 回溯机制的应用场景 3. 剪枝优化的实际价值
建议读者通过LeetCode典型题目(如200.岛屿数量、207.课程表等)进行实践训练,加深对DFS的理解和应用能力。 “`
注:本文实际约1100字,包含代码示例、对比表格和结构化说明。可根据需要增减代码示例或添加可视化图示(如DFS遍历过程的树形图)。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。