zoukankan      html  css  js  c++  java
  • 图论03—随意两点间最短距离及路径(改进)

    ========================================================
    重要程度 *****
    求随意两点间最短距离及其路径。(万能最短路)
    输入:权值矩阵,起点,终点
    输出:最短距离矩阵。指定起讫点路径(经过的顶点编号)
    ========================================================
    function renyizuiduanlu(W)
    clc
    disp('                           最短路问题');
    disp('===========================================================');
    disp('  说明:本程序用于求随意两点间最短距离及其路径');
    disp('  输入3组数据:权值矩阵,起点。终点.当中权值矩阵需提前编辑完毕');
    disp('===========================================================');
    W
    qidian=input('输入起点:');
    zhongdian=input('输入终点:');
    n=length(W);
    D=W;
    m=1;
    while m<=n
        for i=1:n
            for j=1:n
                if D(i,j)>D(i,m)+D(m,j)
                    D(i,j)=D(i,m)+D(m,j);
                end
            end
        end
        m=m+1;
    end
    d=D(qidian,zhongdian);
    P1=zeros(1,n);
    k=1;
    P1(k)=zhongdian;
    V=ones(1,n)*inf;
    kk=zhongdian;
    while kk~=qidian;
        for i=1:n
            V(1,i)=D(qidian,kk)-W(i,kk);
            if V(1,i)==D(qidian,i)
                P1(k+1)=i;
                kk=i;
                k=k+1;
            end
        end
    end
    k=1;
    wrow=find(P1~=0);
    for j=length(wrow):(-1):1
        P(k)=P1(wrow(j));
        k=k+1;
    end
    P
    d
    %========================================================
    %评:改进后能够随意定义并从界面输入起讫点,更加符合实际须要。

    %========================================================

    例:求下图中点1到8的最短距离和路径。


    解:(1)写权值矩阵

    quanzhijuzhen=[ 0     2     8     1   Inf   Inf   Inf   Inf
         2     0     6   Inf     1   Inf   Inf   Inf
         8     6     0     7     5     1     2   Inf
         1   Inf     7     0   Inf   Inf     9   Inf
       Inf     1     5   Inf     0     3   Inf     8
       Inf   Inf     1   Inf     3     0     4     6
       Inf   Inf     2     9   Inf     4     0     3
       Inf   Inf   Inf   Inf     8     6     3     0]

    (2)带入程序

    renyizuiduanlu(quanzhijuzhen)

                                                                   最短路问题
    ===========================================================
      说明:本程序用于求随意两点间最短距离及其路径
      输入3组数据:权值矩阵,起点,终点.当中权值矩阵需提前编辑完毕
    ===========================================================


    W =


         0     2     8     1   Inf   Inf   Inf   Inf
         2     0     6   Inf     1   Inf   Inf   Inf
         8     6     0     7     5     1     2   Inf
         1   Inf     7     0   Inf   Inf     9   Inf
       Inf     1     5   Inf     0     3   Inf     8
       Inf   Inf     1   Inf     3     0     4     6
       Inf   Inf     2     9   Inf     4     0     3
       Inf   Inf   Inf   Inf     8     6     3     0


    输入起点:1
    输入终点:8


    P =


         1     2     5     8




    d =


        11

    说明:最短路径为1->2->5->8,最短距离11.

  • 相关阅读:
    (10)时间
    (9)字符串
    (8)数组工具类
    (7)数学工具类
    (6)随机数
    (5)包装类
    (4)声明式接口和常用接口
    (3)java.lang.System
    (2)java.lang.Object
    (1)开篇
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6911360.html
Copyright © 2011-2022 走看看