zoukankan      html  css  js  c++  java
  • 图论08—次短路的距离及路径

    %========================================================
    %次短路中的次。是指退而求其次,其意义不言而喻,本程序用于实现次短路求解。
    %========================================================
    function ciduanlu(W)
    clc
    qidian=input('输入起点:');
    zhongdian=input('输入终点:');
    [p1 d1]=liangdianzuiduanlu(W,qidian,zhongdian);
    D=d1;
    P=p1;

    n=length(p1);
    d2=inf;
    for i=1:(n-1)
        A=W;
        A(p1(i),p1(i+1))=inf;
        A(p1(i+1),p1(i))=inf;
        [m1 d1]=liangdianzuiduanlu(A,qidian,zhongdian);
        if d1<d2
            d2=d1;
            p2=m1;
        end
    end

    disp('最短路为:');
    p1=P
    d1=D
    disp('次短路为:');
    p2
    d2
    %========================================================
    %评:缺点是仅仅给出了次短路,而一般须要求解k短路,k>=2。
    %========================================================


    例:求下图中起点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)带入程序(格式整理后输出例如以下)

    >> ciduanlu(quanzhijuzhen)

    输入起点:1
    输入终点:8
    最短路为:
    p1 =
         1     2     5     8

    d1 =
        11

    次短路为:
    p2 =
         1     2     5     6     8

    d2 =
        12

    说明:最短路为1->2->5->8,距离12.次短路1->2->5->6->8,距离12.

  • 相关阅读:
    Security headers quick reference Learn more about headers that can keep your site safe and quickly look up the most important details.
    Missing dollar riddle
    Where Did the Other Dollar Go, Jeff?
    proteus 与 keil 联调
    cisco router nat
    router dhcp and dns listen
    配置802.1x在交换机的端口验证设置
    ASAv931安装&初始化及ASDM管理
    S5700与Cisco ACS做802.1x认证
    playwright
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6953097.html
Copyright © 2011-2022 走看看