• 图搜索算法详解


    图搜索算法是计算机科学中用于在图数据结构中查找特定节点或路径的一类算法。本文将介绍几种常见的图搜索算法,包括深度优先搜索(DFS)、广度优先搜索(BFS)、Dijkstra 算法、A* 算法和最小生成树算法。

    1. 深度优先搜索(DFS)

    深度优先搜索是一种递归的图搜索算法,它从图的某个节点开始,沿着一条路径尽可能深地搜索,直到到达最深的节点,然后再回溯到前面的节点继续搜索。DFS 的过程类似于树的前序遍历。实例:在一个迷宫中寻找从起点到终点的路径。

    1. def dfs(graph, start, end, path=[]):
    2. path = path + [start]
    3. if start == end:
    4. return path
    5. for node in graph[start]:
    6. if node not in path:
    7. new_path = dfs(graph, node, end, path)
    8. if new_path:
    9. return new_path
    10. return None

    2. 广度优先搜索(BFS)

    广度优先搜索是一种非递归的图搜索算法,它从图的某个节点开始,逐层地向外扩展搜索,先访问离起始节点最近的节点,然后依次访问距离更远的节点。BFS 的过程类似于树的层序遍历。

    例: 在一个社交网络中查找两个人之间的最短关系链。

    1. from collections import deque
    2. def bfs(graph, start, end):
    3. queue = deque([(start, [start])])
    4. while queue:
    5. node, path = queue.popleft()
    6. if node == end:
    7. return path
    8. for neighbor in graph[node]:
    9. if neighbor not in path:
    10. queue.append((neighbor, path + [neighbor]))
    11. return None

    3.Dijkstra 算法

    Dijkstra 算法用于计算图中单源最短路径。它通过维护一个优先队列来选择当前最短路径的节点,直到所有节点都被访问。

    例: 在一个城市地图中寻找从起点到终点的最短路径。

    1. import heapq
    2. def dijkstra(graph, start):
    3. distances = {node: float('inf') for node in graph}
    4. distances[start] = 0
    5. queue = [(0, start)]
    6. while queue:
    7. current_distance, current_node = heapq.heappop(queue)
    8. if current_distance > distances[current_node]:
    9. continue
    10. for neighbor, weight in graph[current_node].items():
    11. distance = current_distance + weight
    12. if distance < distances[neighbor]:
    13. distances[neighbor] = distance
    14. heapq.heappush(queue, (distance, neighbor))
    15. return distances

    4. A* 算法

    A* 算法是一种启发式搜索算法,通常用于寻找图中两个节点之间的最短路径。它结合了广度优先搜索和启发式评估函数,以更高效地搜索路径。

    例: 在一个地图中寻找从起点到终点的最短路径。

    1. import heapq
    2. def astar(graph, start, end):
    3. open_list = [(0, start)]
    4. closed_list = set()
    5. while open_list:
    6. g, node = heapq.heappop(open_list)
    7. if node == end:
    8. return g
    9. closed_list.add(node)
    10. for neighbor, weight in graph[node].items():
    11. if neighbor not in closed_list:
    12. f = g + weight + heuristic(neighbor, end)
    13. heapq.heappush(open_list, (f, neighbor))
    14. return float('inf')
    15. def heuristic(node, end):
    16. # 估计从节点到终点的距离
    17. pass

    5. 最小生成树算法

    最小生成树算法用于在图中找到一棵包含所有节点且边权值之和最小的树。常见的最小生成树算法包括 Prim 算法和 Kruskal 算法。

    例: 在一个网络通信系统中,选择最小成本的通信线路。

    1. def prim(graph):
    2. tree = []
    3. visited = set()
    4. start = next(iter(graph))
    5. visited.add(start)
    6. while len(visited) < len(graph):
    7. edge = min((weight, node1, node2) for node1 in visited for node2, weight in graph[node1].items() if node2 not in visited)
    8. tree.append(edge)
    9. visited.add(edge[1])
    10. visited.add(edge[2])
    11. return tree

    应用场景

    • 路径搜索:在迷宫、地图或网络中查找最短路径。
    • 连通性检测:检测图中是否存在从一个节点到另一个节点的路径。
    • 拓扑排序:对有向无环图进行排序。
    • 最短路径:使用 Dijkstra 算法或 A* 算法。
    • 最小生成树:构建网络通信、电力输送或交通规划中的最优路径。

    结语

    图搜索算法是解决各种问题的重要工具,它们在计算机科学的许多领域中都有广泛的应用。不同的算法有不同的适用场景和性能特点,选择合适的算法可以提高问题解决的效率和质量。

  • 相关阅读:
    Leetcode 40. 组合总和 II
    慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
    OpenHarmony应用程序包整体说明
    c/c++一个指针delete两次的后果
    【论文阅读】MobileNetV4 - Universal Models for the Mobile Ecosystem
    SpringBoot 2.X 快速掌握
    C/C++ 深入浅出C++模板(上)
    同事都说有SQL注入风险,我非说没有
    Linux内核源码分析 (B.x)Linux页表的映射
    2023 年KPI (KPI:Key Performance Indicator) review
  • 原文地址:https://blog.csdn.net/AIBB_520/article/details/137948748