dij

_ = float('inf')


def dijkstra(graph, n):
    dis = [0] * n
    flag = [False] * n
    pre = [0] * n
    flag[0] = True
    k = 0
    for i in range(n):
        dis[i] = graph[k][i]

    for j in range(n - 1):
        mini = _
        for i in range(n):
            if dis[i] < mini and not flag[i]:
                mini = dis[i]
                k = i
        if k == 0:  # 不连通
            return
        flag[k] = True
        for i in range(n):
            if dis[i] > dis[k] + graph[k][i]:
                dis[i] = dis[k] + graph[k][i]
                pre[i] = k
    #		print(k)
    return dis, pre

def get_realrouter(start,end,dis,pre):
    stack=[]
    dist=dis[end-1]
    a=pre[end-1]
    stack.append(end-1)
    stack.append(a)
    while a!=start-1:
        a = pre[a]
        stack.append(a)
    return stack,dist



if __name__ == '__main__':
    n = 6
    graph = [
        [0, 6, 3, _, _, _],
        [6, 0, 2, 5, _, _],
        [3, 2, 0, 3, 4, _],
        [_, 5, 3, 0, 2, 3],
        [_, _, 4, 2, 0, 5],
        [_, _, _, 3, 5, 0],
    ]
    dis, pre = dijkstra(graph, n)
    print(dis)
    print(pre)
    stack,dist=get_realrouter(1,6,dis,pre)
    print(stack,dist)
    while stack:
        print(stack.pop())

dij_第1张图片

dij_第2张图片

你可能感兴趣的:(dij)