首页 > 其他分享 >Dijkstra Algorithm

Dijkstra Algorithm

时间:2022-11-22 14:03:46浏览次数:57  
标签:node lowest Algorithm graph costs Dijkstra cost 节点


与BFS不同的是每条路径多了权重

1.步骤:

  1. 找到最便宜的节点,即可在最短时间内前往的节点
  2. 对于该节点的邻居,检查是否有前往它们的更短路径,如果有,就更新其开销。
  3. 重复这个过程,直到对图中的每个节点都这样做了
  4. 计算最终路径。

2.注意

  • 只适用于有向无环图(directed acyclic graph, DAG)
  • 不适用于包含负权重边的图(Bellman-Ford algorithm)

3.实现

Dijkstra Algorithm_散列表

3.1 三个散列表和一个数组

根据有向图,需要三个散列表

Dijkstra Algorithm_sed_02

# the graph
graph = {}
graph["start"] = {}
# 散列表又包含散列表
graph["start"]["a"] = 6
graph["start"]["b"] = 2

graph["a"] = {}
graph["a"]["fin"] = 1

graph["b"] = {}
graph["b"]["a"] = 3
graph["b"]["fin"] = 5

graph["fin"] = {} # 终点没有邻居

Dijkstra Algorithm_数组_03

# the costs table
infinity = float("inf")
costs = {}
costs["a"] = 6
costs["b"] = 2
costs["fin"] = infinity

Dijkstra Algorithm_散列表_04

# the parents table
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["fin"] = None

Dijkstra Algorithm_数组_05


最后还需要一个数组记录处理过的节点

processed = []
3.2算法

Dijkstra Algorithm_sed_06

node = find_lowest_cost_node(costs) # 在未处理的节点中找出开销最小的节点 
while node is not None: # 在所有节点都被处理过后结束
cost = costs[node]
# Go through all the neighbors of this node.
neighbors = graph[node]
for n in neighbors.keys():
new_cost = cost + neighbors[n]
# If it's cheaper to get to this neighbor by going through this node...
if costs[n] > new_cost:
# ... update the cost for this node.
costs[n] = new_cost
# This node becomes the new parent for this neighbor.
parents[n] = node
# Mark the node as processed.
processed.append(node)
# Find the next node to process, and loop.
node = find_lowest_cost_node(costs)
3.3 找开销最低的节点
def find_lowest_cost_node(costs):
lowest_cost = float("inf")
lowest_cost_node = None
# Go through each node.
for node in costs:
cost = costs[node]
# If it's the lowest cost so far and hasn't been processed yet...
if cost < lowest_cost and node not in processed:
# ... set it as the new lowest-cost node.
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node


标签:node,lowest,Algorithm,graph,costs,Dijkstra,cost,节点
From: https://blog.51cto.com/u_13875041/5877901

相关文章

  • 堆排序优化版Dijkstra
    Dijkstra依旧基于贪心用堆排序动态维护剩余点中dist[]最小的点堆排序优化Dijkstra算法 稀疏图,用邻接表,稠密也可以 void add(int a,int b,int c){    e[i......
  • 朴素Dijkstra
     朴素dijkstra其实就是数据结构学的,很简单。 朴素dijkstra算法就是暴力枚举n个点,每次枚举找到离本次枚举点最近的点a,然后用点a来更新所有其他点的距离void d......
  • 数据结构 最短生成路径(BFS算法、Floyd(弗洛伊德)算法、Dijkstra算法)
    8.9、最短生成路径-BFS算法BFS算法只能处理无权图BFS算法的基本思想代码实现#include<stdio.h>#include<stdlib.h>#include<math.h>#defineMaxSize100#defin......
  • 【论文阅读】ICRA2021: VDB-EDT An Efficient Euclidean Distance Transform Algorith
    参考与前言Summary:浩哥推荐的一篇无人机下的建图andplanning实验Type:ICRAYear:2021论文链接:https://arxiv.org/abs/2105.04419youtubepresentationvideo:htt......
  • 朴素的dijkstra最短路径算法
    dijkstra算法适用于无负权图中求最短路径,时间复杂度为O(n^2+e),n为节点数,e为边数需要的数据:1.n行两列数组arr[n][2],第一列记录当前节点到出发点的最短距离,第二列记录当......
  • 启发式算法(Heuristic Algorithm)的理解(通俗版)
    有两类算法可以解决优化问题: 优化(算法):保证找到最佳解决方案(如果有足够的时间和资源)是把各种可能性都一一进行尝试,最终能找到问题的答案,但它是在很大的问题空间内,花费大......
  • Dijkstra最短路径算法
    概念是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法的策略,每次遍历到始点距离最近且未访问过......
  • Algorithm: Lecture 4. Divide-and-Conquer Homework
    author:Miyasakadate:2022-10-31title:"Algorithm:Lecture4.Divide-and-ConquerHomework"*Inthiswork,alltheindexofarraystartsby1.Question:Bin......
  • Python代做编程辅导:ECM1414 Data Structures and Algorithms
    全文链接:tecdat.cn/?p=29696IntroductionInsertSort和MergeSort是排序算法中两个最基础的算法,虽然实际中很难用到,但是作为排序的启蒙还是不错的。此次要求写出Insert......
  • Algorithm代做编程辅导:CS406 Greed Algorithm
    全文链接:tecdat.cn/?p=29699IntroductionGreedyAlgorithm,也就是贪心算法,局部最优化的算法,虽然可以快速得到解,但是这个解往往不会是全局最优解。不过这个算法的思想倒是......