博客
关于我
Dijkstra算法之matlab实现
阅读量:503 次
发布时间:2019-03-07

本文共 1379 字,大约阅读时间需要 4 分钟。

感谢“乐观的阿锡”博主分享了K最短路径算法的相关内容。在学习过程中,Dijkstra算法是一个非常实用的工具。为了方便使用,我们将其模块化实现,并注明出处以便引用。

Dijkstra算法,由Leonhard Euler提出的,它以其提出者的人名命名。理论部分可参考教材《最优化技术与数学建模》(董文永等编,清华大学出版社,2010年)。该算法代码已实现模块化处理,可直接调用。

代码注释:

  • 参数说明:netCostMatrix是n×n的矩阵,默认行为起点列为终点,断开路径时请赋值为无穷大。
  • 初始化:默认行为起点列为终点,初始化已到达矩阵全为0,起点距离为0,其余为无穷大。
  • 算法执行:通过松弛操作逐渐找到最短路径,使用路标标记当前最短路径节点。
  • 代码实现:

    function [pathout cost] = dijkstra(netCostMatrix, source, destination)    if ~is-empty(destination)        return [pathout cost]    else        return [destination_col, permanent_number]    end    m = size(netCostMatrix, 1);    n = size(netCostMatrix, 2);    cost = ones(m, 1);    distance = inf * ones(m, 1);    distance(source) = 0;    pathnode = zeros(m, 1);    count = 1;    while count <= m        u = find(min(distance), 1);        if distance(u) < cost(u)            cost(u) = distance(u);            pathnode(u) = 0;        else            pathnode(u) = 0;        end        for v = 1:m            if netCostMatrix(u,v) == inf                continue;            end            if distance(v) > cost(u) + netCostMatrix(u, v)                distance(v) = cost(u) + netCostMatrix(u, v);                pathnode(v) = u;            end            if distance(v) == cost(u) + netCostMatrix(u, v)                ...            end        end        count = count + 1;    end    if ~is-empty(destination)        ...    endend

    代码注释已完毕,为开发者提供清晰的使用指南。

    转载地址:http://txdjz.baihongyu.com/

    你可能感兴趣的文章
    Nginx 常用配置清单
    查看>>
    nginx 常用配置记录
    查看>>
    nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 源码完全注释(11)ngx_spinlock
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    Nginx 结合 consul 实现动态负载均衡
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置清单(一篇够用)
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    nginx+php的搭建
    查看>>
    nginx+tomcat+memcached
    查看>>
    nginx+Tomcat性能监控
    查看>>
    nginx+uwsgi+django
    查看>>
    Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
    查看>>