Dijkstra算法

假设有一无向图如上所示,用一个二维数组来保存顶点和边

path = [[1, 2, 7], [1, 3, 9], [1, 6, 14], [2, 4, 15], [2, 3, 10], [3, 4, 11], [3, 6, 2], [4, 5, 6], [5, 6, 9]]

        首先创建邻接矩阵

def build_graph(path):
    """创建邻接矩阵 """
    max_size = 0
    for i in range(len(path)):
        max_size = max(max_size, max(path[i][:2]))
    max_size += 1
    mat = [[0] * max_size for _ in range(max_size)]

    for i in range(max_size):
        for j in range(max_size):
            if i != j:
                mat[i][j] = 99999

    for i in range(len(path)):
        tmp_row = path[i][0]
        tmp_col = path[i][1]
        weight = path[i][2]
        mat[tmp_row][tmp_col] = weight
        mat[tmp_col][tmp_row] = weight
    return mat

import numpy as np

print(np.array(build_graph(path)))

打印这个邻接矩阵

[[    0 99999 99999 99999 99999 99999 99999]
 [99999     0     7     9 99999 99999    14]
 [99999     7     0    10    15 99999 99999]
 [99999     9    10     0    11 99999     2]
 [99999 99999    15    11     0     6 99999]
 [99999 99999 99999 99999     6     0     9]
 [99999    14 99999     2 99999     9     0]]

核心代码:

def shortest_path(vertex, graph):
    size = len(graph)
    distance = graph[vertex]
    goal = [0] * size
    goal[vertex] = 1
    shortest_vertex = vertex

    for i in range(1, size):

        shortest_dis = 99999
        for j in range(1, size):
            if goal[j] == 0 and shortest_dis > distance[j]:
                shortest_dis = distance[j]
                shortest_vertex = j
        goal[shortest_vertex] = 1

        for j in range(size):
            if goal[j] == 0 and distance[shortest_vertex] + graph[shortest_vertex][j] < distance[j]:
                distance[j] = distance[shortest_vertex] + graph[shortest_vertex][j]
    return distance[1:]

完整代码:

path = [[1, 2, 7], [1, 3, 9], [1, 6, 14], [2, 4, 15], [2, 3, 10], [3, 4, 11], [3, 6, 2], [4, 5, 6], [5, 6, 9]]


def build_graph(path):
    """创建邻接矩阵 """
    max_size = 0
    for i in range(len(path)):
        max_size = max(max_size, max(path[i][:2]))
    max_size += 1
    mat = [[0] * max_size for _ in range(max_size)]

    for i in range(max_size):
        for j in range(max_size):
            if i != j:
                mat[i][j] = 99999

    for i in range(len(path)):
        tmp_row = path[i][0]
        tmp_col = path[i][1]
        weight = path[i][2]
        mat[tmp_row][tmp_col] = weight
        mat[tmp_col][tmp_row] = weight
    return mat

def shortest_path(vertex, graph):
    size = len(graph)
    distance = graph[vertex]
    goal = [0] * size
    goal[vertex] = 1
    shortest_vertex = vertex

    for i in range(1, size):

        shortest_dis = 99999
        for j in range(1, size):
            if goal[j] == 0 and shortest_dis > distance[j]:
                shortest_dis = distance[j]
                shortest_vertex = j
        goal[shortest_vertex] = 1

        for j in range(size):
            if goal[j] == 0 and distance[shortest_vertex] + graph[shortest_vertex][j] < distance[j]:
                distance[j] = distance[shortest_vertex] + graph[shortest_vertex][j]
    return distance[1:]

weight = build_graph(path)
print(shortest_path(1, weight))

输出:

[0, 7, 9, 20, 20, 11]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

athrunsunny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值