graph theory - Matlab Network Average path length -


i've created erdős random graph of format n n symmetric matrix attribute sparse. i'm trying work out average path length using following code:

% compute average path length network - average shortest path % inputs: adjl - matrix of weights/distances between nodes % outputs: average path length: average of shortest paths between every 2 edges % note: works directed/undirected networks % gb, december 8, 2005  function l = ave_path_length(adj)  n=size(adj,1);  dij = [];  i=1:n; dij=[dij; simple_dijkstra(adj,i) ]; end  l = sum(sum(dij))/(n^2-n); % sum , average across diagonal 

it uses dijkstra algorithm in following link: http://strategic.mit.edu/docs/matlab_networks/simple_dijkstra.m

however, matlab becomes busy , don't result. tips appreciated.

you growing matrix, in dij starts off empty , appended each loop, size changing. in matlab, can result in significant slowdowns in running time , in worst cases, matlab hang.

presuming know final size of dij (from cursory examination, believe should n x n), should preallocate. in case, zeros work:

dij = zeros(n); i=1:n     dij(n,:) = simple_dijkstra(adj,i); end 

if open original code in matlab editor, see little orange underline under dij within loop. indicates warning. hover on , should warning:

the variable 'dij' appears change size on every loop iteration. consider preallocating speed.

even case you're not 100% sure of final size, can better over-allocate maximum possible size, , trim down unused portion.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -