Saturday, 10 August 2013

Find paths with maximum length between two nodes

Find paths with maximum length between two nodes

I want to find paths with maximum length between two nodes in weighted
directed graph.I search for it and the best code that I found was the
following code. I find some similar questions but I don't find for my case
(paths with smaller than specified length).
I use this code to extract all paths between two nodes but I want to
extract only paths with smaller than maximum length.How must I change the
algorithm. My adjacency matrix is so large and this code is not efficient
because it search all paths.The input of this function is wt=adjacency
matrix, startnode, endnode and the output is cell with two column first
column contain paths and the second one contain the cost(1->2 has cost 1
and 1->3->4 has cost 2).
function [paths] = allpaths(wt, startnode, endnode)
lastpath = [startnode]; %We begin with the path containing just the startnode
costs = [0]; %The cost of this path is zero because we haven't yet crossed
any edges
paths = {zeros(0,1),zeros(0,1)}; %The set of solution paths is empty (I'm
assuming startnode!=endnode)
N = size(wt,1); %#Obtain the number of nodes in the graph
assert(N==size(wt,2)); %#Assert that the adjacency matrix is a square matrix
for i = 2 : N
%#Creates a matrix with a row for each path and a 1 in a column where
there's a possible move from the last visited node in a path to this
column
nextmove = wt(lastpath(:, i - 1), :) ~= 0;
% #Zero out any nodes we've already visited
d = diag(1:size(lastpath,1));
nrows = d * ones(size(lastpath));
inds = sub2ind(size(nextmove), reshape(nrows,[],1),
reshape(lastpath,[],1));
nextmove(inds) = false;
%# If there are no more available moves we're done
if nextmove == 0
break;
end%if
% #For each true entry in our nextmove matrix, create a new path from
the old one together with the selected next move
nextmoverow = d * nextmove;
nextmovecol = nextmove * diag(1:N);
rowlist = reshape(nonzeros(nextmoverow),[],1);
collist = reshape(nonzeros(nextmovecol),[],1);
nextpath = [lastpath(rowlist,:), collist];
% # Compute the costs of the new set of paths by adding the old ones
to the cost of each newly traversed edge
inds = sub2ind([N,N],nextpath(:, i-1),nextpath(:,i));
costs = costs(rowlist) + wt(inds);
%# For any path finishing on the end node, add it to the return list
(and it's corresponding cost)
reachedend = nextpath(:,i) == endnode;
paths = [paths; {nextpath(reachedend, :)},{costs(reachedend)}];
%#Then remove it from the list of paths still being explored
lastpath = nextpath(~reachedend, :);
costs = costs(~reachedend);
% #If there are no more paths, we're done
if isempty(lastpath)
break;
end%if
end%for
end%function

No comments:

Post a Comment