version 1.0.0.0 (1.32 KB) by
Michael Kleder

Gives the shortest node-to-node distance along the edges of a graph for all node combinations.

3 Downloads

Updated 26 Dec 2007

No License

ALLSPATH - solve the All Pairs Shortest Path problem

Rapidly returns the shortest node-to-node distance along the edges of a graph, for all nodes in the graph.

USAGE: B = allspath(A)

A = input distance matrix between nodes

B = shortest path distance matrix between all nodes

Notes:

(1) For a graph with n nodes, A is an n-by-n distance matrix giving the distances between adjacent nodes. Since the distance from point i to point j is the same as the distance from point j to point i, A must be a symmetric matrix

(2) The distance from a node to itself can either be entered as zero or infinity. (Either will produce a correct result.) This means that the diagonal elements of the matrix A must all be either zero or infinity.

(3) The distance between nodes that are not adjacent to each other must be entered as either zero or infinity. (Either will produce a correct result.) This means that the (i,j) and (j,i) elements of A, where i and j are non-adjacent nodes, must all be either zero or infinity.

(4) If the input graph is not "connected," meaning that some nodes cannot be reached from other nodes no matter how many edges are traversed, then the distances between nodes that cannot be connected will be returned as infinite.

(5) Distances between a node and itself are returned as zero.

(6) This function codifies an original algorithm created by the author.

(7) No warranties; use at your own risk.

(8) Written by Michael Kleder, October 2005.

Michael Kleder (2021). "All Pairs Shortest Path" Graph Solver (https://www.mathworks.com/matlabcentral/fileexchange/8808-all-pairs-shortest-path-graph-solver), MATLAB Central File Exchange. Retrieved .

Created with
R14SP2

Compatible with any release

**Inspired:**
Dijkstra's Minimum Cost Path Algorithm

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Suchetana GuptaDoes this also return the exact path visited? Mine is a weighted network

Xuan Vinh NguyenI think Matlab now provides a similar function:

http://www.mathworks.com.au/help/bioinfo/ref/graphallshortestpaths.html

MRRsrinadhawesome,simple to use

srinadhIs there a way to find the path also along with the distance?

thanks

UleWorks nice for small graphs (100 nodes), but if the graph becomes a bit larger (say, 500 nodes) it takes forever and effectively freezes my laptop...

Joseph KirkThank you for putting in the effort to update the file with the new example for those of us lacking the Statistics Toolbox. :)

However, I still had some difficulty testing it. References were made to variables 'a' and 'b' that did not exist. I was able to run the example when I added the following code after "% distance matrix:"

>> k=(1:10); a=k(ones(1,10),:); b=a';

The AuthorThank you. PDIST does require the Statistics Toolbox, so I have removed it from the example and uploaded a replacement submission where the example does not use PDIST.

Joseph KirkThe example didn't work for me...

??? Undefined function or method 'pdist' for input arguments of type 'double'.

Do I need a special toolbox for this?

John SWorks great for me, < 200-ish nodes. Thanks!

David LeamerWhat a beautifully compact algorithm! Works fast for up to about 200-250 nodes. For more than that it works but uses a lot of memory. This is really*great for smallish (about 200 nodes) investigational type problems that I'm working on right now.

Dead BeatDoesn't work, fails with a "??? Out of memory. Type HELP MEMORY for your options." even tho my graph has 600 nodes & I have 1GB of RAM!

Kyaw TunIt give out of memory error even though my graph has only 702 nodes. :-)

giuseppe dThere are a limit whenever we have a large graph. Out of memory proble...

Somebody tried?

G

giuseppe dBut what happens it the graph is not undirected? I think it doesn't work...

LIU JerryIt is indeed useful for my research!