Content Source: Graph theory algorithm and its realization of MATLAB (Beijing Aerospace Press)
P34
"Algorithmic use"
The maximum reliable path between two vertices in a graph is obtained.
The code is as follows:
1%algorithm for maximum reliable path2%calling the Floyd file3function [P p f]=P_PATHF (A)4%f=0 means find a way, or f=15%Initialize6 7[M n]=size (A);8f=0;9b=zeros (m,n);Ten%convert the original matrix One forI=1: M A forj=1: N - ifA (I,J) >0&a (I,J) <1 -B (I,J) =-log (A (i,j)); theElseIf A (i,j) = =0 -B (I,J) =inf; - End - End + End -% using warshall-Floyd algorithm to find the shortest way + A[P d]=Floyd (B); at ifd<inf -p=1; - forI=1:(Length (P)-1) -P=p*a (P (i), P (i+1));%calculate the good probability of the shortest path - End - p; in Else -p=0; top=0; +f=1; - End the
Where the function Floyd code is as follows:
1function [P,u] =Floyd (W)2%W represents the weight matrix3%p = Shortest Path4%U represents the shortest-circuit right and5n=length (W);6u=W;7m=1;8 9 whileM<=n%determine if the stop condition is metTen forI=1: N One forj=1: N A ifU (i,j) >u (i,m) +U (m,j) -U (i,j) =u (i,m) +u (M,J); %Update Dij - End the End - End -m=m+1; - End +U=u (1, n); - +%output Least-shorted vertices AP1=zeros (1, n); atk=1; -P1 (k) =N; -V=ones (1, N) *inf; -kk=N; - whilekk~=1 - forI=1: N inV1, i) =u (1, KK)-W (I,KK); - ifV1, i) ==u (1, i) toP1 (k +1)=i; +kk=i; -k=k+1; the End * End $ EndPanax Notoginsengk=1; -Wrow=find (p1~=0); the forJ=length (wrow):(-1):1 +P (k) =P1 (Wrow (j)); Ak=k+1; the End + -End
Check:
a=[1 0.8 0 0 0.2 ; 0.4 1 0.7 0 0; 0 0.8 1 0.5 0; 0.6 0 0 1 0.4; 0.3 0 0.7 0.5 1]; [P P F]=P_PATHF (A)
Operation Result:
P = 1 5= 0.2000= 0
algorithm and MATLAB implementation of "shortest circuit" for maximum reliable path