Python实现教程:平面最短路径算法

简介: Python实现教程:平面最短路径算法

在图论中,最短路径问题是寻找图中两个顶点之间的最短路径。在平面图中,这个问题尤其有趣,因为它可以应用于现实世界中的路网、机器人路径规划、游戏编程等领域。本文将通过几个Python代码示例,介绍几种常用的平面最短路径算法。

示例1:Dijkstra算法

Dijkstra算法是一种广泛应用于单源最短路径问题的算法。下面是一个简单的Python示例来实现该算法。

import heapq

def dijkstra(graph, start):
    distances = {vertex: float('infinity') for vertex in graph}
    distances[start] = 0
    priority_queue = [(0, start)]

    while priority_queue:
        current_distance, current_vertex = heapq.heappop(priority_queue)

        if current_distance > distances[current_vertex]:
            continue

        for neighbor, weight in graph[current_vertex].items():
            distance = current_distance + weight

            if distance < distances[neighbor]:
                distances[neighbor] = distance
                heapq.heappush(priority_queue, (distance, neighbor))

    return distances

# 图表示为邻接列表
graph = {
    'A': {'B': 1, 'C': 4},
    'B': {'A': 1, 'C': 2, 'D': 5},
    'C': {'A': 4, 'B': 2, 'D': 1},
    'D': {'B': 5, 'C': 1}
}

print(dijkstra(graph, 'A'))

在这个例子中,我们使用一个优先队列(通过Python的heapq模块实现)来保持距离最小的顶点的顺序。

示例2:A*算法

A*算法是一种在图中找到最短路径的启发式算法。它用一个估计函数来评价通过该节点到达终点的估计最短路径。

import heapq

class Node:
    def __init__(self, name, h_value):
        self.name = name
        self.h_value = h_value
        self.g_value = float('infinity')
        self.f_value = float('infinity')
        self.parent = None

    def __lt__(self, other):
        return self.f_value < other.f_value

def a_star(graph, start, end):
    open_set = []
    closed_set = set()
    start_node = graph[start]
    end_node = graph[end]
    start_node.g_value = 0
    start_node.f_value = start_node.h_value

    heapq.heappush(open_set, start_node)

    while open_set:
        current_node = heapq.heappop(open_set)
        closed_set.add(current_node)

        if current_node == end_node:
            path = []
            while current_node:
                path.append(current_node.name)
                current_node = current_node.parent
            return path[::-1]

        for child in current_node.children:
            if child in closed_set:
                continue
            tentative_g_value = current_node.g_value + current_node.children[child]

            if tentative_g_value < child.g_value:
                child.parent = current_node
                child.g_value = tentative_g_value
                child.f_value = child.g_value + child.h_value

                if child not in open_set:
                    heapq.heappush(open_set, child)

    return None

# 创建图
graph = {
    'A': Node('A', 10),
    'B': Node('B', 8),
    'C': Node('C', 5),
    'D': Node('D', 7),
    'E': Node('E', 3),
    'F': Node('F', 0),
}

# 定义邻接关系和启发函数值
graph['A'].children = {graph['B']: 3, graph['C']: 1}
graph['B'].children = {graph['A']: 3, graph['D']: 4}
graph['C'].children = {graph['A']: 1, graph['D']: 1, graph['E']: 2}
graph['D'].children = {graph['B']: 4, graph['C']: 1, graph['F']: 5}
graph['E'].children = {graph['C']: 2, graph['F']: 3}
graph['F'].children = {graph['D']: 5, graph['E']: 3}

path = a_star(graph, 'A', 'F')
print("Path from A to F:", path)

A*算法通过启发函数来优化搜索过程,通常用于路径规划和图形游戏编程。

示例3:Bellman-Ford算法

Bellman-Ford算法可以计算图中的单源最短路径,它也可以处理图中的边权重为负数的情况。

def bellman_ford(graph, start):
    distance = {vertex: float('infinity') for vertex in graph}
    distance[start] = 0

    for _ in range(len(graph) - 1):
        for vertex in graph:
            for neighbor in graph[vertex]:
                if distance[vertex] + graph[vertex][neighbor] < distance[neighbor]:
                    distance[neighbor] = distance[vertex] + graph[vertex][neighbor]

    # 检测负权重循环
    for vertex in graph:
        for neighbor in graph[vertex]:
            if distance[vertex] + graph[vertex][neighbor] < distance[neighbor]:
                print("Graph contains a negative weight cycle")
                return None

    return distance

graph = {
    'A': {'B': -1, 'C': 4},
    'B': {'C': 3, 'D': 2, 'E': 2},
    'C': {},
    'D': {'B': 1, 'C': 5},
    'E': {'D': -3}
}

print(bellman_ford(graph, 'A'))

Bellman-Ford算法的一个重要特点是它可以检测负权重的循环。

总结

在本文中,我们介绍了三种平面最短路径算法:Dijkstra算法、A*算法和Bellman-Ford算法,并提供了详细的Python代码示例。这些算法在不同的应用场景下各有优势,选择何种算法主要取决于图的特性、是否存在负权边以及是否需要启发式搜索。掌握这些算法,你将能够在各种需要路径规划的场景中找到高效的解决方案。


目录
相关文章
|
4月前
|
算法 搜索推荐 JavaScript
基于python智能推荐算法的全屋定制系统
本研究聚焦基于智能推荐算法的全屋定制平台网站设计,旨在解决消费者在个性化定制中面临的选择难题。通过整合Django、Vue、Python与MySQL等技术,构建集家装设计、材料推荐、家具搭配于一体的一站式智能服务平台,提升用户体验与行业数字化水平。
|
4月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
186 1
|
4月前
|
存储 监控 算法
监控电脑屏幕的帧数据检索 Python 语言算法
针对监控电脑屏幕场景,本文提出基于哈希表的帧数据高效检索方案。利用时间戳作键,实现O(1)级查询与去重,结合链式地址法支持多条件检索,并通过Python实现插入、查询、删除操作。测试表明,相较传统列表,检索速度提升80%以上,存储减少15%,具备高实时性与可扩展性,适用于大规模屏幕监控系统。
172 5
|
5月前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
854 19
|
5月前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
261 26
|
5月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
297 0
|
5月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
418 0
|
5月前
|
机器学习/深度学习 编解码 算法
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
499 4
|
5月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
702 4
|
5月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
314 3

推荐镜像

更多