1.前言
最近在学习《交通网络均衡理论》这门课,我计划将其中的一些经典算法用Python实现,而后发布到这里来和大家交流,欢迎指正。
2.代码实现
'''
@Author: Michael 2022-09-16 21:57:27
This code is about Yen.
'''
from Dijkstra import Dijkstra
import copy
def Short_limit(mgraph, inf, start, end, passed:list, noaccess:list):
'''
mgraph 为临界距离矩阵(以列表储存)
inf 为设置的极大值
passed 为当前必须经过点
nopass 为当前禁止通行路段,以列表储存,其元素为二维列表,表示禁止通行的路段
path 为当前条件下最短路径,以列表储存
dis 为当前条件下最短路长度
此处start,end,passed,noaccess均为现实标号
'''
mgraph_copy_1 = copy.deepcopy(mgraph)
mgraph_copy_2 = copy.deepcopy(mgraph)
if len(passed) != 0:
start = passed[-1]
for i,j in noaccess:
mgraph_copy_2[i-1][j-1] = inf
for i in passed:
for j in range(len(mgraph)):
#此步保证无重复点
if i != passed[-1]:
mgraph_copy_2[i-1][j] = inf
mgraph_copy_2[j][i-1] = inf
path,dis = Dijkstra(mgraph_copy_2,start,end,inf)
path = passed[:-1]+path
for k in range(len(passed)-1):
dis += mgraph_copy_1[passed[k]-1][passed[k+1]-1]
return path, dis
def Init(mgraph,inf,start,end,k): #初始化
passed, noaccess, path, dis = [],[

本文介绍了一种使用Python实现的Yen算法,该算法能够找到从起点到终点的k条最短路径。通过具体示例展示了算法的工作流程,并讨论了可能的优化方向。

600

被折叠的 条评论
为什么被折叠?



