狄克斯特拉算法

本文讲解了如何在加权图中应用狄克斯特拉算法,包括术语解释(加权图、环、负权边),并以换钢琴为例,详细介绍了算法的四个步骤。特别指出负权边需要使用Bellman-Ford算法。通过实例演示了如何使用散列表记录权重、成本和父节点。
摘要由CSDN通过智能技术生成

本章内容:

  1. 介绍加权图
  2. 介绍狄克斯特拉算法:计算加权图的最短路径
  3. 介绍“环”

使用狄克斯特拉算法

狄克斯特拉算法包含4个步骤:

  1. 找出最短时间内到达的节点
  2. 更新该节点的邻点权重
  3. 重复1,2直到对所有的节点都采取以上步骤
  4. 计算最终路径(找到到每个点的最短耗时)

术语

加权图
在这里插入图片描述


在这里插入图片描述

无向图中,每条边就是一个环。狄克斯特拉算法只适用于有向无环图。(DAG)

换钢琴

在这里插入图片描述

第一步:找到最近节点,海报。找出图中最便宜的节点,并确保没有到该节点的更便宜的路径!
第二步:计算该节点前往各个邻居的开销
列出父节点,最后根据父节点倒推路径。

负权边

在这里插入图片描述

负权变不能用狄克斯特拉算法。只能用Bellman-Ford算法实现。

实现

依然以该图为例
在这里插入图片描述

需要三个散列表来记录:

graph={}#记录权重
costs={}#更新
parents={}#更新
graph['start']={}
graph['start']['A']=6
graph['start']['B']=2
graph['A']={}
graph['A']['end']=1
graph['B']={}
graph['B']['A']=3
graph['B']['end']=5
graph['end']={}
processed=[]#记录处理过的节点

在这里插入图片描述

def find_lowest_cost_node(costs):
    lowest_cost=float("inf")
    lowest_cost_node=None
    for node in costs:
        cost=costs[node]
        if cost<lowest_cost and node not in processed:
            lowest_cost=cost#更新
            lowest_cost_node=node
    return lowest_cost_node

costs['A']=6
costs['B']=2
costs['end']=float("inf")
node=find_lowest_cost_node(costs)#找出未处理点中最小开销的点
while node is not None:
    cost=costs[node]
    neighbors=graph[node]
    for n in neighbors.keys():
        new_cost=cost+neighbors[n]
        if costs[n]>new_cost:
            costs[n]=new_cost
            parents[n]=node
    processed.append(node)
    node=find_lowest_cost_node(costs)

costs
parents
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
狄克斯特拉算法(Dijkstra's algorithm)是一种用于解决单源最短路径问题的经典算法。它可以找到从一个顶点到其他所有顶点的最短路径。 以下是狄克斯特拉算法的Java实现: ```java import java.util.*; public class DijkstraAlgorithm { private static final int INF = Integer.MAX_VALUE; public static void dijkstra(int[][] graph, int start) { int n = graph.length; int[] dist = new int[n]; boolean[] visited = new boolean[n]; Arrays.fill(dist, INF); dist[start] = 0; for (int i = 0; i < n - 1; i++) { int minDist = INF; int minIndex = -1; for (int j = 0; j < n; j++) { if (!visited[j] && dist[j] < minDist) { minDist = dist[j]; minIndex = j; } } visited[minIndex] = true; for (int j = 0; j < n; j++) { if (!visited[j] && graph[minIndex][j] != 0 && dist[minIndex] != INF && dist[minIndex] + graph[minIndex][j] < dist[j]) { dist[j] = dist[minIndex] + graph[minIndex][j]; } } } // 打印最短路径 System.out.println("顶点\t最短距离"); for (int i = 0; i < n; i++) { System.out.println(i + "\t" + dist[i]); } } public static void main(String[] args) { int[][] graph = { {0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; dijkstra(graph, 0); } } ``` 这段代码实现了狄克斯特拉算法,通过传入一个邻接矩阵表示的图和起始顶点,计算出从起始顶点到其他所有顶点的最短路径,并打印出最短距离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值