The shortest distance and path between any two points of the most short-circuit 02-1--in graph theory

Source: Internet
Author: User

========================================================
Find the shortest distance between any two points and their paths. (Universal shortest circuit)
Input: Weight matrix, start point, end point
Output: Shortest distance matrix, specifying commencement point path (elapsed vertex number)
Lay the groundwork for the shortest path to any other point
========================================================
function [P D]=liangdianzuiduanlu (W,qidian,zhongdian)
W
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

========================================================
Comment: The disadvantage is that the cohesion function is not good, the next "improvement of any two points between the shortest distance and road

This shortcoming will be solved by the "path".
========================================================

Example: Finding the shortest distance and path of midpoint 1 to 8


Solution: (1) Write Weight matrix

quanzhijuzhen=[ 0     2     8     1   INF   INF &N Bsp INF   INF
    &NBSP;2     0     6   INF     1   INF   INF & nbsp INF
     8     6     0     7     5     1     2   INF
     1   INF     7     0   INF   INF     9 &N Bsp INF
   inf     1     5   INF     0     3   INF   &NBSP ; 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) Bring in the procedure

[P D]=liangdianzuiduanlu (quanzhijuzhen,1,8)


P =


1 2 5 8




D =


11

Note: The path is 1->2->5->8 and the shortest distance is 11.


Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.

The shortest distance and path between any two points of the most short-circuit 02-1--in graph theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.