Dijkstra迪杰斯特拉算法Python版本

11 篇文章 0 订阅
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time : 2022/1/31 14:14
# @Author : Jin Echo
# @File : Dijkstra.py

import heapq
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import random


def dijkstra(adj, s):
    """

    :param adj: n*n的邻接矩阵
    :param s: 起点下标
    :return:
    """
    n = adj.shape[0]  # 顶点数
    pqueue = []
    heapq.heappush(pqueue, (0, s))  # <距离 起点>二元组
    visited = set()
    pre = np.arange(n)
    # 初始化
    distance = np.ones((n,)) * np.inf
    distance[s] = 0

    cnt = 0
    while len(visited) != n and len(pqueue):
        cnt += 1
        min_dis, min_node = heapq.heappop(pqueue)
        if min_node in visited:
            continue
        else:
            visited.add(min_node)
        neighbors = np.where((adj[min_node, :] < 1) & (adj[min_node, :] > 0))[0]  # 提取当前点的邻接点
        for neighbor in neighbors:
            if neighbor not in visited:
                if min_dis + adj[min_node][neighbor] < distance[neighbor]:
                    distance[neighbor] = min_dis + adj[min_node][neighbor]
                    heapq.heappush(pqueue, (distance[neighbor], neighbor))
                    pre[neighbor] = min_node
                    
    print(cnt)
    return pre, distance


def get_sequence(pre, node):
    """
    返回起点到指定节点的最短路径
    :param pre:
    :param node:
    """
    res = [node]
    while pre[node] != node:
        node = pre[node]
        res.append(node)
    res.reverse()
    return res


if __name__ == '__main__':
    np.random.seed(1)
    random.seed(1)
    n, m = 20, 4
    G = nx.barabasi_albert_graph(n, m)  # 生成图结构
    plt.figure(1)
    nx.draw(G, with_labels=True, font_weight='bold')

    edges_idx = np.array(G.edges)
    # print(edges_idx)
    num_edges = (n - m) * m  # 图中的边数,也等于edges_idx.shape[0]
    assert num_edges == edges_idx.shape[0]

    # 赋值边权
    adj = np.ones([n] * 2) * np.inf
    # adj[range(n), range(n)] = 0  # 主对角线为0
    adj[edges_idx[:, 0], edges_idx[:, 1]] = np.random.rand(num_edges)
    adj[edges_idx[:, 1], edges_idx[:, 0]] = adj[edges_idx[:, 0], edges_idx[:, 1]]

    s = 0
    pre, distance = dijkstra(adj, s)
    print(pre)
    print(distance)
    print(get_sequence(pre, n - 1))
    plt.show()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值