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.

  • 相关阅读:
    分区调整教程
    ArcServer10.1系列产品之ArcGIS Web Adaptor (IIS)
    初次登录github
    java中的关键字
    《构建之法》第二章学习有感
    《构建之法》第一章节学后有感
    第一次写博客
    HashMap 扩容分析
    ArrayList的扩容机制
    古籍研究社系列第6部《迟来的翅膀》读后感……吗?
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6953097.html
Copyright © 2011-2022 走看看