图论——Dijkstra算法matlab代码

Dijkstra算法步骤

(1)构造邻接矩阵

(2)定义起始点

(3)运行代码

Matlab 复制代码
M=[  0     5     9   Inf   Inf   Inf   Inf
   Inf     0   Inf   Inf    12   Inf   Inf
   Inf     3     0    15   Inf    23   Inf
   Inf     6   Inf     0   Inf     8     7
   Inf    12   Inf     5     0   Inf    14
   Inf   Inf   Inf   Inf   Inf     0    10
   Inf   Inf   Inf   Inf   Inf   Inf     0];
first=2;
last=4;
[m,n]=size(M);
L=zeros(1,m);
symbol=zeros(1,m);
direction=zeros(1,m);
for i=1:m
    if(i~=first)
        L(i)=inf;
    end
    direction(i)=first;
end
judge=1;
while judge
for i=1:m
    if(symbol(i)==0)
        min=L(i);
        temporary=i;
        break
    end
end
for i=1:m
    if(symbol(i)==0)
        if(L(i)<min)
            min=L(i);
            temporary=i;
        end
    end
end
k=temporary;
for j=1:m
    if(symbol(1,j)==0)
        if(M(k,j)==inf)
            continue;
        else
            if(L(k)+M(k,j)<L(j))
                L(j)=L(k)+M(k,j);
                direction(j)=k;
            end
        end
    end
end
symbol(k)=1;
num=0;
for i=1:m
    if(symbol(i)==1)
        num=num+1;
    end
end
if(num==m)
    judge=0;
end
end
p=last;
arrow=zeros(1,m);
arrow(1)=last;
i=2;
while p~=first
    arrow(1,i)=direction(p);
    i=i+1;
    p=direction(p);
end
distance=L(last);
相关推荐
এ᭄画画的北北12 分钟前
力扣-283.移动零
算法·leetcode
2501_924879363 小时前
口罩识别场景误报率↓79%:陌讯多模态融合算法实战解析
人工智能·深度学习·算法·目标检测·智慧城市
Christo33 小时前
TFS-2022《A Novel Data-Driven Approach to Autonomous Fuzzy Clustering》
人工智能·算法·机器学习·支持向量机·tfs
木木子99993 小时前
超平面(Hyperplane)是什么?
算法·机器学习·支持向量机·超平面·hyperplane
星空下的曙光5 小时前
React 虚拟 DOM Diff 算法详解,Vue、Snabbdom 与 React 算法对比
vue.js·算法·react.js
♞沉寂5 小时前
数据结构——双向链表
数据结构·算法·链表
大阳1235 小时前
数据结构2.(双向链表,循环链表及内核链表)
c语言·开发语言·数据结构·学习·算法·链表·嵌入式
CUC-MenG6 小时前
2025牛客多校第六场 D.漂亮矩阵 K.最大gcd C.栈 L.最小括号串 个人题解
c语言·c++·算法·矩阵
2401_876221346 小时前
Tasks and Deadlines(Sorting and Searching)
c++·算法
我要学习别拦我~7 小时前
逻辑回归建模核心知识点梳理:原理、假设、评估指标与实战建议
算法·机器学习·逻辑回归