Realization of multi-source shortest path Floyd algorithm ———— matlab

Source: Internet
Author: User

Freud (Floyd) algorithm is an algorithm for finding the shortest path between vertices in a given weighted graph. The algorithm is named after one of the founders, 1978 Turing Award winner, and Stanford University professor of computer science Robert Floyd.


Basic ideas

by Floyd calculating the shortest path of each vertex in Figure g= (v,e), you need to introduce a matrix s, the element in Matrix S a[i][j] represents the distance from vertex i (vertex i) to Vertex J (J Vertex).

Assuming that the number of vertices in Figure g is n, you need to update the Matrix S n times. Initially, the distance from vertex A[i][j] in matrix S is the weight of vertex i to vertex j, or a[i][j]=∞ if I and J are not adjacent. Next, the matrix S is updated n times. On the 1th update, if the distance of "a[i][j" > "A[i][0]+a[0][j]" (a[i][0]+a[0][j) means "the distance between the 1th vertices between I and J"), then update a[i][j] is "a[i][0]+a[0][j". Similarly, on the K update, if "a[i][j" Distance ">" a[i][k]+a[k][j] ", then update a[i][j] to" a[i][k]+a[k][j ". After updating n times, the operation is complete!

The MATLAB code functions are as follows:

function [Dist,mypath]=myfloyd (a,sb,db);% input: A-adjacency matrix (AIJ) refers to the distance between I and J, which can be the marking of the sb-starting point of the forward direction; the marking of the db-end point% output: dist-shortest distance; mypath-Shortest Path N=size (a,1); Path=zeros (n); for I=1:nfor j=1:nif A (i,j) ~=infpath (i,j) =j; %j is the follow-up point of I endendendfor k=1:nfor i=1:nfor j=1:nif A (i,j) >a (i,k) +a (k,j) A (i,j) =a (i,k) +a (k,j);p ath (i,j) =path (i,k); Endendendenddist=a (sb,db); MYPATH=SB; T=sb;while T~=dbtemp=path (t,db); Mypath=[mypath,temp];t=temp;endreturn

  

Realization of multi-source shortest path Floyd algorithm ———— matlab

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.