Matalab代码 实现 Dijkstra求 有向图及无向图之间,任意两点之间的最短路径

来源:互联网 发布:ps4 pro 优化的游戏 编辑:程序博客网 时间:2024/05/04 08:45
<span style="font-family: Arial, Helvetica, sans-serif;">%% Dijkstra </span>
function minWeightMatrix=shortestPath(G,nodeNum)minWeightMatrix=zeros(nodeNum,nodeNum);for i=1:nodeNum %获取每个顶点到其它节点最短路径权值,Dijkstra 源点遍历    D=G(i,:);   %初始化节点到各个邻居节点的权值    D(D==0)=inf;    tag=zeros(1,nodeNum); %0 为对应位置节点未求出最大路径    tag(i)=1;    while sum(tag,2)~=nodeNum        noTag=find(tag==0); %还未获得最短距离的节点        minValue=min(D(noTag));        if minValue~=inf            tempIndex=find(D==minValue); %找出候选节点,更新值            tag(tempIndex)=1;            % 将候选节点去掉            for candidateNode=1:length(tempIndex)                noTag(noTag==tempIndex(candidateNode))=[];            end            % 修改源点到各个未标记的顶点间的距离            for j=1:length(noTag) %对所有未获得最短距离的节点进行更新                minTemp=D(noTag(j));                for k=1:length(tempIndex)                    if minTemp>(minValue+G(tempIndex(k),noTag(j)))&&G(tempIndex(k),noTag(j))~=0                        minTemp=minValue+G(tempIndex(k),noTag(j));                    end                end                D(noTag(j))=minTemp;            end                    else            break;        end    end    minWeightMatrix(i,:)=D;endminWeightMatrix(isinf(minWeightMatrix))=0;minWeightMatrix=minWeightMatrix-diag(diag(minWeightMatrix));

测试数据:

clear;clc;%% 测试数据%有向图范例G=[0 0 10 0 30 100;0 0 5 0 0 0;0 0 0 50 0 0;0 0 0 0 0 10;0 0 0 20 0 60;0 0 0 0 0 0];%% 无向图范例G=[0   0 10  0  30 100;   0   0  5  0  0  0;   10  5  0 50  0  0;   0   0 50 0   20 10;   30  0  0 20  0  60;   100 0  0 10  60  0];minPath=shortestPath(G,6);disp('测试完毕');


0 0
原创粉丝点击